关于Spring框架中的AnnotationConfigApplicationContext

时间:2019-06-01 19:06:55

标签: java spring spring-ioc

我编写了以下简单的独立spring应用程序:

Environment

我了解到,在main()中,我们正在创建一个新的Application上下文,然后创建Bean并最终调用test()方法。

我无法理解ApplicationContextConfigurableEnvironmentctx如何自动连接(以及连接到哪个bean?)

我观察到自动连线的Private Sub example() Dim ws As Worksheet Dim s As Range Dim lastcol as Long Dim lastrow as Long Dim h1 As Variant Dim a As String Dim b As String Dim c As String Dim d As String Dim e As String lastcol = ws.Cells(4, Columns.Count).End(xlToLeft).Column lastrow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row On Error Resume Next h1 = ws.Range("A4:Z4").Find("Header1").Offset(1, 0).Address(False, False) 'h2 to h6 - same as above a = "Header1" b = "Header2" c = "Header3" d = "Header4" e = "Header5" ws.Range(Range("A4:Z4").Find("Header0").Offset(0, 1), Range("A4:Z4").Find("Header6").Offset(0, -1)).Select With Selection For Each s In Selection 'Calc (Header1, Header2, Header3, Header4, Header5) - ALL If s.Value.a = True And s.Value.b = True And s.Value.c = True And s.Value.d = True And s.Value.e = True Then ws.Range(Range("A4:Z4").Find("Result %").Offset(1, 0), Cells(lastrow, lastcol)).Formula = _ "=IFERROR(SUM(" & h1 & "," & h2 & "," & h3 & "," & h4 & ", If(" & h5 & "/" & h6 & ">1%," & h6 & "*1%," & h5 & "))/ " & h6 & ", """") " 'Calc (Header1, Header2, Header3, Header4) - NO Header5 ElseIf s.Value.a = True And s.Value.b = True And s.Value.c = True And s.Value.d = True And s.Value.e = False Then ws.Range(Range("A4:Z4").Find("Result %").Offset(1, 0), Cells(lastrow, lastcol)).Formula = _ "=IFERROR(SUM(" & h1 & "," & h2 & "," & h3 & "," & h4 & ")/" & h6 & ", """") " 'Calc (Header1, Header2, Header3, Header5) - NO Header4 ElseIf s.Value.a = True And s.Value.b = True And s.Value.c = True And s.Value.d = False And s.Value.e = True Then ws.Range(Range("A4:Z4").Find("Result %").Offset(1, 0), Cells(lastrow, lastcol)).Formula = _ "=IFERROR(SUM(" & h1 & "," & h2 & "," & h3 & ", IF(" & h5 & "/" & h6 & ">1%," & h6 & "*1%," & h5 & "))/" & h6 & ", """") " ' Total of 15 conditions and formulas End If Next End With End Sub 获得了在main()中初始化的上下文。

基本上不能理解它们是如何自动接线的(以及它们的设置是什么?)

任何有助于理解这一点的方法都会有很大帮助。

1 个答案:

答案 0 :(得分:1)

任何用@Configuration注释的类也是Spring bean。这意味着MainDirver也是将在创建AnnotationConfigApplicationContext期间创建的spring bean。

在创建MainDirver bean之后,如果该字段用@Autowird注释,那么Spring将把其他bean注入其字段中。因此,在这种情况下,EnvironmentApplicationContextConfigurableEnvironment都注入了这个MainDirver bean。

P.S。您可以认为EnvironmentApplicationContextConfigurableEnvironment是必须创建的Spring基础结构bean,即使您没有使用@Configuration,{{1}来定义它们},@Service

相关问题