Spring 2.5Annotation使用基本类型和${}

本文介绍如何在Spring2.5中解决基本类型依赖注入问题,通过结合XML配置和注解的方式实现,并利用PropertyPlaceholderConfigurer进行属性文件读取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近使用了Spring2.5 annotation风格的DI,发现竟然不支持基本类型的自动装配,更别说使用PropertyPlaceholderConfigurer了。既然不支持,只能想其它的办法,最后使用XML和annotation相结合的方式解决了这个问题。
[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:)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值