Spring @Qualifier不工作,但是@Resource呢?

时间:2017-06-09 13:55:23

标签: java spring spring-mvc

我有以下代码:

' Reflection for VBScript via tlbinfo32.dll
'
' Patrick Strasser-Mikhail 2017-2019
' Ansgar Wiechers 2019
' https://stackoverflow.com/questions/14305750/list-object-methods-and-properties/44459670#44459670

Option Explicit

' Returns a String describing the passed object/variable on the first level, no recursion.
Function VariableInfo(obj)
    Const invokeKindPropertyGet = 0
    Const invokeKindFunction = 1
    Const invokeKindPropertyPut = 2
    Const invokeKindPropertyPutRef = 4

    If isEmpty(obj) Or _
       isNull(obj) _
    Then
        VariableInfo = TypeNameFromVarType(VarType(obj))
    ElseIf Not IsObject(obj) Then
        If Not isArray(obj) Then
            VariableInfo = TypeNameFromVarType(VarType(obj)) & ", Value: " & obj
        Else

            VariableInfo = TypeNameFromVarType(VarType(obj)) & "("
            Dim dimension
            Dim size

            On Error Resume Next
            Err.Clear

            For dimension = 1 To 10 ' deliberate limit to prevent infinite loop
                size = Ubound(obj, dimension) + 1
                If Err.Number <> 0 Then
                    Exit For
                End If
                If dimension > 1 Then
                    VariableInfo = VariableInfo & ","
                End If
                VariableInfo = VariableInfo & Ubound(obj, dimension)
            Next

            On Error Goto 0

            VariableInfo = VariableInfo & ")"
        End If
    ElseIf TypeName(obj) = "Nothing" Then
        VariableInfo = "Nothing (The Invalid Object)"
    Else
        Dim TLI
        Dim MemberInfo
        Dim TypeInfo
        Set TLI = CreateObject("TLI.TLIApplication")
        VariableInfo = "Object " & TypeName(obj)

        On Error Resume Next
        Err.Clear
        Set TypeInfo = TLI.InterfaceInfoFromObject(obj)

        If Err.Number <> 0 Then
            VariableInfo = VariableInfo & "; Error " & Err.Number
            VariableInfo = VariableInfo & ": " & Err.Description
            Err.Clear
            Exit Function
        End If

        For Each MemberInfo In TypeInfo.Members
            Dim Desc
            Desc = ""
            Select Case MemberInfo.InvokeKind
                Case InvokeKindFunction
                    If MemberInfo.ReturnType.VarType <> 24 Then
                        Desc = "  Function " & TypeNameFromVarType(MemberInfo.ReturnType.VarType)
                    Else
                        Desc = "  Sub"
                    End If

                    Desc = Desc & " " & MemberInfo.Name
                    Dim ParameterList
                    ParameterList = Array()
                    Dim Parameter
                    For Each Parameter In MemberInfo.Parameters
                        ReDim Preserve parameterList(UBound(ParameterList) + 1)
                        ParameterList(Ubound(parameterList)) = Parameter.Name
                    Next
                    Desc = Desc & "(" & Join(ParameterList, ", ") & ")"
                    'Set parameters = Nothing
                Case InvokeKindPropertyGet
                    Desc = "  Property " & MemberInfo.Name
                Case InvokeKindPropertyPut
                    Desc = "  Property (set/get) " & MemberInfo.Name
                Case InvokeKindPropertyPutRef
                    Desc = "  Property (set ref/get) " & MemberInfo.Name
                Case Else
                    Desc = "  Unknown member, InvokeKind " & MemberInfo.InvokeKind
            End Select
            VariableInfo = VariableInfo & vbNewLine & Desc
        Next

        Set TypeInfo = Nothing
        Set TLI = Nothing
    End If
End Function

' Decode Type Number to something readable
Function TypeNameFromVarType(typeNr)
    Dim typeDetails
    set typeDetails = CreateObject("Scripting.Dictionary")

    typeDetails.add 0,  "vbEmpty (uninitialized variable)"
    typeDetails.add 1,  "vbNull (value unknown)"
    typeDetails.add 2,  "vbInteger" ' Short?
    typeDetails.add 3,  "vbLong" ' Integer?
    typeDetails.add 4,  "vbSingle"
    typeDetails.add 5,  "vbDouble"
    typeDetails.add 6,  "vbCurrency"
    typeDetails.add 7,  "vbDate"
    typeDetails.add 8,  "vbString"
    typeDetails.add 9,  "vbObject"
    typeDetails.add 10, "Exception"
    typeDetails.add 11, "vbBoolean"
    typeDetails.add 12, "vbVariant"
    typeDetails.add 13, "DataObject"
    typeDetails.add 14, "vbDecimal"
    typeDetails.add 17, "vbByte"
    typeDetails.add 18, "vbChar"
    typeDetails.add 19, "ULong"
    typeDetails.add 20, "Long" ' realy Long?
    typeDetails.add 24, "(void)"
    typeDetails.add 36, "UserDefinedType"

    If typeDetails.Exists(typeNr) Then
        TypeNameFromVarType = typeDetails(typeNr)
    ElseIf typeNr > 8192 Then
        TypeNameFromVarType = "vbArray{" & TypeNameFromVarType(typeNr - 8192) & "}"
    Else
        typeNameFromVarType = "Unknown Type " & typeNr
    End If
End Function

' Some nice example class to demonstrate all possible interfaces.
Class MyClass
    Dim Name_
    Dim Name2_

    Public Property Get Name
        Name = Name_
    End Property

    Public Property Let Name(ByVal Value)
      Name_ = Value
    End Property

    Public Property Let Name2(ByRef Value)
      Set Name2_ = Value
    End Property

    Sub TestSub()
        WScript.Echo "Test"
    End Sub

    Sub TestFunc(message)
        WScript.Echo "Test: " & message
    End Sub

    Sub TestFunc2(ByRef message)
        WScript.Echo "Test: " & message
    End Sub

    Function Add(first, second)
        Add = first + second
    End Function

    Function Substract(ByVal first, ByRef second)
        Add = first - second
    End Function
End Class

Sub testVariableInfo()
    Dim variable
    ' vbEmpty
    Wscript.Echo VariableInfo(variable)

    variable = Null
    Wscript.Echo VariableInfo(variable)

    Set variable = Nothing
    Wscript.Echo VariableInfo(variable)

    Dim MyObject
    Set MyObject = new MyClass
    Wscript.Echo VariableInfo(MyObject)
    Set MyObject = Nothing

    Dim TestA1(3, 7)
    Wscript.Echo VariableInfo(TestA1)
    Dim TestA2()
    Wscript.Echo VariableInfo(TestA2)

    Dim TestA3
    TestA3 = Array(4, 5, 6)
    Wscript.Echo VariableInfo(TestA3)
End Sub

testVariableInfo

以上代码无效。我收到一条错误消息,指出spring容器无法决定在@Qualifier("dateObjectMapper") private ObjectMapper mapper; @Autowired DefaultProjectTweetSearchProvider( Client client, ObjectMapper mapper) { this.client = client; this.mapper = mapper; } 的构造函数中使用哪个bean。如果我将ObjectMapper放在@Resource(name = "dateObjectMapper")字段上方,则可行。为什么它会在那种情况下起作用?我有2个mapper bean这样:

ObjectMapper

1 个答案:

答案 0 :(得分:1)

使用构造函数注入时,@Qualifier注释必须在参数上。

private ObjectMapper mapper;

@Autowired
DefaultProjectTweetSearchProvider(
        Client client,
        @Qualifier("dateObjectMapper") ObjectMapper mapper) {
    this.client = client;
    this.mapper = mapper;
}

请注意bean名称。你的例子:

@Bean
ObjectMapper dateObjectMapper() {
   // ... 
}

@Bean
@Primary
ObjectMapper defaultObjectMapper() {
   // ... 
}

将创建与@Bean方法同名的bean:dateObjectMapperdefaultObjectMapper