spring中使用autowire特性时注意
2009-01-07 17:41
260 查看
spring中可以只用autowire="byName"或者"byType"来提供自动注入。但是大家有一点还需要注意,那就是它不支持基本数据类型(String,int,long等)的自动注入。例如
J***A类:
public class TestBean {
private String name;
private Long age;
private String fullName;
private TestBean2 testBean2;
public void setTestBean2(TestBean2 testBean2) {
this.testBean2 = testBean2;
}
public TestBean2 getTestBean2() {
return testBean2;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getFullName() {
return fullName;
}
public void setAge(Long age) {
this.age = age;
}
public Long getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
J***A类2:
public class TestBean2 {
public String getString(){
return "testB";
}
}
配置文件:
<beans default-autowire="byName">
<!-- login -->
<bean id="testBean" class="TestBean">
<property name="fullName">
<value>fullName</value>
</property>
</bean>
<bean id="name" class="java.lang.String">
<constructor-arg>
<value>hell</value>
</constructor-arg>
</bean>
<bean id="age" class="java.lang.Long">
<constructor-arg>
<value>20</value>
</constructor-arg>
</bean>
<bean id="testBean2" class="TestBean2"/>
</beans>
测试类:
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext t = new ClassPathXmlApplicationContext(
"test.xml");
TestBean bean = (TestBean) t.getBean("testBean");
System.out.println("name:" + bean.getName() + " age:" + bean.getAge()
+ " fullName :" + bean.getFullName() + " testBean2"
+ bean.getTestBean2().getString());
}
}
输出的结果是:
name:null age:null fullName :fullName testBean2:testB
spring源码中排除基本类型的代码(源码来自:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory类)在:
protected String[] unsatisfiedNonSimpleProperties(AbstractBeanDefinition mbd, BeanWrapper bw) {
Set result = new TreeSet();
PropertyValues pvs = mbd.getPropertyValues();
PropertyDescriptor[] pds = bw.getPropertyDescriptors();
for (int i = 0; i < pds.length; i++) {
if (pds[i].getWriteMethod() != null && !isExcludedFromDependencyCheck(pds[i]) &&
!pvs.contains(pds[i].getName()) && !BeanUtils.isSimpleProperty(pds[i].getPropertyType())) {
result.add(pds[i].getName());
}
}
return StringUtils.toStringArray(result);
}
请注意以上紫色部分。而这个unsatisfiedNonSimpleProperties方法都是在自动注入时调用。所以基本类型不会被注入。
J***A类:
public class TestBean {
private String name;
private Long age;
private String fullName;
private TestBean2 testBean2;
public void setTestBean2(TestBean2 testBean2) {
this.testBean2 = testBean2;
}
public TestBean2 getTestBean2() {
return testBean2;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getFullName() {
return fullName;
}
public void setAge(Long age) {
this.age = age;
}
public Long getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
J***A类2:
public class TestBean2 {
public String getString(){
return "testB";
}
}
配置文件:
<beans default-autowire="byName">
<!-- login -->
<bean id="testBean" class="TestBean">
<property name="fullName">
<value>fullName</value>
</property>
</bean>
<bean id="name" class="java.lang.String">
<constructor-arg>
<value>hell</value>
</constructor-arg>
</bean>
<bean id="age" class="java.lang.Long">
<constructor-arg>
<value>20</value>
</constructor-arg>
</bean>
<bean id="testBean2" class="TestBean2"/>
</beans>
测试类:
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext t = new ClassPathXmlApplicationContext(
"test.xml");
TestBean bean = (TestBean) t.getBean("testBean");
System.out.println("name:" + bean.getName() + " age:" + bean.getAge()
+ " fullName :" + bean.getFullName() + " testBean2"
+ bean.getTestBean2().getString());
}
}
输出的结果是:
name:null age:null fullName :fullName testBean2:testB
spring源码中排除基本类型的代码(源码来自:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory类)在:
protected String[] unsatisfiedNonSimpleProperties(AbstractBeanDefinition mbd, BeanWrapper bw) {
Set result = new TreeSet();
PropertyValues pvs = mbd.getPropertyValues();
PropertyDescriptor[] pds = bw.getPropertyDescriptors();
for (int i = 0; i < pds.length; i++) {
if (pds[i].getWriteMethod() != null && !isExcludedFromDependencyCheck(pds[i]) &&
!pvs.contains(pds[i].getName()) && !BeanUtils.isSimpleProperty(pds[i].getPropertyType())) {
result.add(pds[i].getName());
}
}
return StringUtils.toStringArray(result);
}
请注意以上紫色部分。而这个unsatisfiedNonSimpleProperties方法都是在自动注入时调用。所以基本类型不会被注入。
相关文章推荐
- spring中使用autowire特性时注意
- Spring 使用其他命名空间的时候要注意的事项
- 使用Spring的JndiObjectFactoryBean配置数据源时需要注意的问题
- spring使用注意事项
- Spring 声明式事务中使用注解@Transactional的方式、原理及注意事项
- Spring的属性文件properties使用注意
- 如何使用spring的autowire为servlet注入Bean
- 如何让不受spring管理的类具有spring自动注入的特性.--AutowireCapableBeanFactory
- 使用Spring和SpringMVC管理bean时要注意的一个小细节
- 如何使用spring @component 以及注意事项
- RabbitMQ在SpringBoot中使用的一些注意点
- 使用Spring的javamail注意的中文问题
- 使用Spring 2.5 和 Hibernate 3.2 开发MVC Web程序(基于annotation特性)
- hibernate 与spring结合,hibernatedaoSupport使用注意事项
- Spring中AutowireCapableBeanFactory的使用
- spring声明事务、annotation事务同时使用注意
- Spring事物隔离级别以及事物的七种传播特性详细介绍,以及应用当中需要注意的点
- [转]使用spring中的@Transactional注解时,可能需要注意的地方
- Spring多数据源使用注解事务绑定注意事项和解决办法
- 使用Spring AOP应该注意的一个小细节