Announcement Announcement Module
Collapse
No announcement yet.
how to provide constructor argument of class type Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • how to provide constructor argument of class type

    I am trying to use org.apache.mina.core.service.SimpleIoProcessorPool which takes IoProcessor type as argument like this.
    SimpleIoProcessorPool<NioSession> pool =
    new SimpleIoProcessorPool<NioSession>(NioProcessor.cla ss);

    I don't know how to provide constructor arg for this class in my application context file. If I give class name as value, it is a String and is wrong.

    <bean id="simpleIoProcessorPool" class="org.apache.mina.core.service.SimpleIoProces sorPool">
    <constructor-arg value="NioProcessor.class"/>
    </bean>

    At the same time, if I give ref="nioprocessor" and define bean id with Executor service like below [ except I did not provide Executor in constructor arg to keep it short], then it is injecting the new instance of NioProcessor which is not what I want.
    <bean id="simpleIoProcessorPool" class="org.apache.mina.core.service.SimpleIoProces sorPool">
    <constructor-arg ref="nioProcessor"/>
    </bean>

    <bean id="nioProcessor" class="org.apache.mina.transport.socket.nio.NioPro cessor">
    <constructor-arg></constructor-arg>
    </bean>

    thank you,

  • #2
    The following works:

    ClassConstructor.java
    Code:
    public class ClassConstructor {
    
    	public ClassConstructor(Class clazz)
    	{
    		System.out.println(clazz.getCanonicalName());
    	}
    	
    }
    DefaultClass.java
    Code:
    public class DefaultClass {
    
    }
    applicationContext.xml
    Code:
    <bean id="classConstructor" class="com.example.ClassConstructor">
    		<constructor-arg type="java.lang.Class" value="com.example.DefaultClass"></constructor-arg>
    	</bean>

    Comment


    • #3
      Thank you, that works very well.

      Comment

      Working...
      X