我知道如何单独使用自动组件扫描和构造函数注入。 http://www.mkyong.com/spring/spring-auto-scanning-components/ http://www.dzone.com/tutorials/java/spring/spring-bean-constructor-injection-1.html
是否可以将 AutoComponent Scanning 与构造函数注入一起使用?在使用自动组件扫描时,spring 框架会扫描所有指向的类"base-package"
,并通过调用不带参数的每个构造函数来创建每个类的实例。让我们说一下如何修改以下类和相关的 Spring XML 文件。
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 文件
<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>