最近使用了Spring2.5 annotation风格的DI,发现竟然不支持基本类型的自动装配,更别说使用PropertyPlaceholderConfigurer了。既然不支持,只能想其它的办法,最后使用XML和annotation相结合的方式解决了这个问题。
[b]Bean定义[/b]
[b] XML配置[/b]
这样即使用了基本类型和PropertyPlaceholderConfigurer:)
[b]Bean定义[/b]
@Service
public class Service {
//默认是按Type装配,可以指定Qualifier使用name装配
@Autowired
@Qualifier("path")
private String path;
@Autowired
@Qualifier("pathId")
private Integer id;
public void go() {
}
}
[b] XML配置[/b]
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- 属性文件读入 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:config/other.properties</value>
</list>
</property>
</bean>
<!-- 修改为对应的packge -->
<context:component-scan base-package="com" />
<bean id="path" class="java.lang.String">
<constructor-arg type="java.lang.String" value="${path}" />
</bean>
<bean id="pathId" class="java.lang.Integer">
<constructor-arg type="java.lang.String" value="${path.id}" />
</bean>
</beans>
这样即使用了基本类型和PropertyPlaceholderConfigurer:)