Spring bean注入方式

时间:2023-03-08 22:13:09

版权声明:本文为博主原创文章,如需转载请标注转载地址。

博客地址:http://www.cnblogs.com/caoyc/p/5619525.html 

Spring bean提供了3中注入方式:属性注入和构造方法注入

1、属性注入:

 <bean id="dept" class="com.proc.bean.Dept">
<property name="id" value="2"/>
<property name="name" value="信息部"></property>
</bean>

  属性注入方式,要求属性提供呢setXxx方法。上面提供的是普通属性注入,如果要注入对象属性,可以这样

 <bean id="user" class="com.proc.bean.User">
<property name="id" value="1" />
<property name="username" value="caoyc"></property>
<property name="dept" ref="dept"></property>
</bean>

  我们看到第三个属性dept,是一个Dept类型的属性,可以通过ref来引用一个已定义的Dept类型的dept对象。

 ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
User user=context.getBean("user", User.class);
System.out.println(user.getUsername());
System.out.println(user.getDept().getName());

  结果我们可以看到

caoyc
信息部

  除了可以使用ref来引用外部对象外,我们也可以在user对象内部声明一个Dept对象

 <bean id="user" class="com.proc.bean.User">
<property name="id" value="1" />
<property name="username" value="caoyc"></property>
<property name="dept">
<bean class="com.proc.bean.Dept">
<property name="id" value="2"/>
<property name="name" value="信息部"></property>
</bean>
</property>
</bean>

2、使用构造器注入

  假如,有一个User类

package com.proc.bean;

public class User {

    private int id;
private String username;
private int age;
private double slary; public User() { }
public User(int id, String username) {
this.id = id;
this.username = username;
} @Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", age=" + age
+ ", slary=" + slary + "]";
}
}

  

<bean id="user" class="com.proc.bean.User">
<constructor-arg value="1"/>
<constructor-arg value="caoyc"/>
</bean>

  这里使用的是: public User(int id, String username)构造方式 

  这里使用的是下标方式,这里省略了index属性。index属性从0开始。上面的代码相当于

 <bean id="user" class="com.proc.bean.User">
<constructor-arg index="0" value="1"/>
<constructor-arg index="1" value="caoyc"/>
</bean>

  假设有这样的两个构造方式

 public User(int id, String username, int age) {
this.id = id;
this.username = username;
this.age = age;
}
public User(int id, String username, double slary) {
this.id = id;
this.username = username;
this.slary = slary;
}

  配置bean

 <bean id="user" class="com.proc.bean.User">
<constructor-arg index="0" value="1" />
<constructor-arg index="1" value="caoyc"/>
<constructor-arg index="2" value="18"/>
</bean> <bean id="user2" class="com.proc.bean.User">
<constructor-arg index="0" value="1" />
<constructor-arg index="1" value="caoyc"/>
<constructor-arg index="2" value="1800"/>
</bean>

  测试代码

 ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
User user=context.getBean("user",User.class);
System.out.println(user); User user2=context.getBean("user2",User.class);
System.out.println(user2);

  输出结果

 User [id=1, username=caoyc, age=0, slary=18.0]
User [id=1, username=caoyc, age=0, slary=1800.0]

  这里都是调用的public User(int id, String username, double slary)这个构造函数。那么怎么调用public User(int id, String username, int age)这个构造函数呢?

   方法是,在这里我们需要使用到type属性,来指定参数的具体类型

 <bean id="user" class="com.proc.bean.User">
<constructor-arg index="0" value="1" />
<constructor-arg index="1" value="caoyc"/>
<constructor-arg index="2" value="18" type="int"/>
</bean>

  输出结果

 User [id=1, username=caoyc, age=18, slary=0.0]
User [id=1, username=caoyc, age=0, slary=1800.0]

【其它说明】

  1、如果value属性是基本属性直接使用

  2、如果valeu属性是其它类型,需要使用ref引用外部类型或使用内部定义方式

  3、如果value属性中包含了xml特殊字符,需要使用CDATA来。例如:

 <constructor-arg index="1">
<value><![CDATA[<caoyc>]]></value>
</constructor-arg>