to bean via Spring Config I have a generic type that I injected into a service. Due to the way gener...">

Pass "HardCoded" Constructor Arg Class <T> to bean via Spring Config

I have a generic type that I injected into a service. Due to the way generic tools are implemented in Java, I need to have an arg constructor (or setterter) that contains information about the parameter class of a type type.

My question is: can I, by entering properties or specifying the arg constructor, pass an instance of the class with spring?

I know type T before runtime, so I know exactly what the Type parameter will be.

I thought it would look something like this:

<bean id="dataMartService" class="com.someclass">
    <constructor-arg value="java.lang.class<com.someotherclass>" />
</bean>

Do I really not understand how this should happen?

+5
source share
3 answers

Try:

<bean id="dataMartService" class="com.someClass">
    <constructor-arg>
        <value type="java.lang.Class">someotherclass</value>
    </constructor-arg>
</bean>
+7
source

Use spring el:

<constructor-arg value="#{ T(java.lang.Math) }" />

( spring 3.0)

, , spring , , . .

+7

You can use:

//for constructor
public CheckSpell(SpellCheker spellCheker){
         this.spellCheker=spellCheker;
}

//Use object reference as bean

    <bean id="textEditor" class="com.sring.spellCheck">
          <constructor-arg ref="spellChecker"/>
      </bean>

<bean id="spellCheker" class="com.spring.SpellCheker"/>
0
source

All Articles