I know how to use Auto Components Scanning and Consctuctor Injection individually. http://www.mkyong.com/spring/spring-auto-scanning-components/ http://www.dzone.com/tutorials/java/spring/spring-bean-constructor-injection-1.html
Is it possible to use AutoComponent Scanning with Constructor Injection? While using Auto Component Scanning spring framework scans all classes pointed "base-package"
and creates an instance of each by invoking each Constructor with no parameter. Lets say how to modify following class and related spring XML file.
package com.fb.common;
@Repository
public class Person {
private String name;
private int age;
public Person(String name, int age){
this.name=name;
this.age=age;
}
public String toString(){
return "Name: "+name+" Age:"+age;
}
}
XML file
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.fb.common" />
<!--
<bean id="person" class="com.fb.common.Person">
<constructor-arg type="java.lang.String" value="DefaultName"/>
<constructor-arg type="int" value="30"/>
</bean>
-->
</beans>