多种方式实现依赖注入

  1.Spring 使用setter访问器实现对属性的赋值,

  2.Spring 构造constructor方法赋值,

  3.接口注入

  4.Spring P命名空间注入直接量

  

 

 

setter访问器实现方式following

  实体类中设置属性的set访问器

1 public class Equip {
2     private String name;        //装备名称
3         public String getName() {
4         return name;
5     }
6     public void setName(String name) {
7         this.name = name;
8     }    
9 }

applicationContext.xml中使用

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xsi:schemaLocation="
 6         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
 8         ">
 9 
10  <bean id="armet" class="zym.game.entity.Equip">
11         <property name="name" value="战神头盔"/>
12     </bean>
13 </beans>

这样就实现了通过setter赋值     前台getBean("armet")的时候    该对象的name属性默认就是:战神头盔。

 

构造注入

使用设置注入时,Spring通过JavaBean的无参构造方法实例化对象。当我们编写了带参构造方法后,java虚拟机不会再提供默认的无参构造方法,建议自行添加一个无参构造方法。

 1 public class User implements Serializable {
 2     private String name;
 3     private String password;
    //car变量称为域属性
4 private Car car; 5 6 @Override 7 public String toString() { 8 return "用户名:"+this.getName()+"\t密码:"+this.getPassword(); 9 }; 10 11 12 public User() {} 13 }

带有域属性的注入方式:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:aop="http://www.springframework.org/schema/aop"
 6     xsi:schemaLocation="
 7         http://www.springframework.org/schema/beans 
 8         http://www.springframework.org/schema/beans/spring-beans.xsd
 9         http://www.springframework.org/schema/aop
10         http://www.springframework.org/schema/aop/spring-aop.xsd
11         ">
12 
13     <!-- 构造注入  域属性注入  -->
14     <bean id="user11" class="entity.User" scope="singleton">
15        <constructor-arg index="0" type="java.lang.String" value="zs"></constructor-arg>
16        <constructor-arg index="1" type="java.lang.String" value="pwd@123"></constructor-arg>
17        <constructor-arg index="2" type="entity.Car" ref="mycar"></constructor-arg>
18     </bean>
19 </beans>

 

P命名空间注入直接量

通过设置p:属性值    给类字段进行赋值

需要添加引用:xmlns:p="http://www.springframework.org/schema/p"
可以在pdf文档里找到

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:aop="http://www.springframework.org/schema/aop"
 6     xsi:schemaLocation="
 7         http://www.springframework.org/schema/beans 
 8         http://www.springframework.org/schema/beans/spring-beans.xsd
 9         http://www.springframework.org/schema/aop
10         http://www.springframework.org/schema/aop/spring-aop.xsd
11         ">
12         <!-- 命名空间注入 -->
13     <bean id="user2" class="entity.User" p:name="zym" p:password="zym@123"></bean>
14 
15 </beans>