Results 1 to 3 of 3

Thread: how to provide constructor argument of class type

  1. #1
    Join Date
    Jan 2011
    Posts
    2

    Default 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. #2

    Default

    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>

  3. #3
    Join Date
    Jan 2011
    Posts
    2

    Default

    Thank you, that works very well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •