Spring依赖注入的几种方式

1.依赖注入

测试类:Person.java
创建配置文件:applicationContext-injection.xml
创建测试代码:InjectionTest.java

1.1 set方法注入

1.1.1基本类型值注入

使用value

  • 配置:
  <!-- value值为基本类型 -->
  <bean name="person"  class="com.bruceliu.spring.bean.Person" >
       <property name="name" value="jeck" />
       <property name="age" value="11"/>
  </bean>
  • 测试代码:
  @Test
  public  void  test1(){
        //TODO 测试基本数据类型注入数据
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-injection.xml");
        Person person = context.getBean("person", Person.class);
        System.out.println("person = " + person);
		//输出结果:------------> Person.Person
		//person = Person{name='jeck', age=11}
 }

1.2 引入类型值注入

  • 使用ref
    创建 Car.java:
public class Car {
    private String name;
    private String color;
    
    public Car() {
        super();
        System.out.println("Car的空参构造方法");
    }
    //getter、setter、toString
}
  • 修改Person.java,在Person中引入Car:
public class Person {

    private String name;
    private Integer age;
    private Car car;
 	//构造方法 getter setter toString方法 
}
  • 配置:利用ref属性给 person的car属性赋值
  <bean name="person1" class="com.bruceliu.spring.bean.Person">
      <property name="name" value="helen"></property>
      <property name="age" value="18"></property>
      <property name="car" ref="car"></property>
  </bean>
    
  <bean name="car" class="com.bruceliu.spring.bean.Car">
      <property name="name" value="MINI"></property>
      <property name="color" value="灰色" ></property>
  </bean>

1.2 构造函数注入

1.2.1 单个有参构造方法注入

  • 在Person中创建有参构造函数:
 public  Person(String  name , Car car){
        this.name = name;
        this.car = car;
        System.out.println("Person的有参构造方法:"+name+car);
 }
  • 配置:
<bean name="person" class="com.bruceliu.spring.bean.Person">
           <constructor-arg name="name" value="rose"/>
           <constructor-arg name="car"  ref="car"/>
 </bean>
 <!-- 构造函数car时候引入 -->
 <bean name="car"  class="com.bruceliu.spring.bean.Car" >
           <property name="name" value="mime"/>
           <property name="color" value="白色"/>
 </bean>
  • 测试:
  @Test
  public  void test2(){
		//TODO 测试参构造方法
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-injection.xml");

        Person person = context.getBean("person", Person.class);

        System.out.println(person);
	

index属性

  • 按参数索引注入
    参数名一致,但位置不一致时,使用 index
    例如以下两个构造函数(第二个是新添加):
    public Person(String name, Car car) {
        super();
        System.out.println("Person(String name, Car car)");
        this.name = name;
        this.car = car;
    }
    
    public Person(Car car, String name) {
        super();
        System.out.println("Person(Car car, String name)");
        this.name = name;
        this.car = car;
    }
  • 配置:使用 index 确定调用哪个构造函数
    <bean name="person2" class="com.bruceliu.spring.bean.Person">
        <constructor-arg name="name" value="helen" index="0"></constructor-arg>
        <constructor-arg name="car" ref="car" index="1"></constructor-arg>
    </bean>
  • 测试:
    重新执行第一步的测试用例,执行结果调用了第一个构造函数
type属性
  • 按参数类型注入
    参数名和位置一致,但类型不一致时,使用type
    例如以下两个构造函数(第二个是新添加):
    public Person(Car car, String name) {
        super();
        System.out.println("Person(Car car, String name)");
        this.name = name;
        this.car = car;
    }
    
    public Person(Car car, Integer name) {
        super();
        System.out.println("Person(Car car, Integer name)");
        this.name = name + "";
        this.car = car;
    }
  • 配置:使用type指定参数的类型
    <bean name="person2" class="com.bruceliu.spring.bean.Person">
        <constructor-arg name="name" value="988" type="java.lang.Integer"></constructor-arg>
        <constructor-arg name="car" ref="car" ></constructor-arg>
    </bean>
  • 测试:
    重新执行前面的测试用例,执行结果调用了第二个构造函数

1.3 p名称空间注入

导入p名称空间:
使用p:属性名 完成注入,走set方法
基本类型值: p:属性名=“值”
引入类型值: P:属性名-ref=“bean名称”

  • 配置:
//1.第一步配置文件中 添加命名空间p 
 xmlns:p="http://www.springframework.org/schema/p"

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"  
       xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">
  
       //使用 p命称空间进行赋值
        <bean name="person" class="com.bruceliu.spring.bean.Person"  p:name="人名" p:age="11"    p:car-ref="car">

       </bean>

       <bean name="car"  class="com.bruceliu.spring.bean.Car" >
           <property name="name" value="mime" />
           <property name="color" value="白色"/>
       </bean>
  
  • 测试:
@Test
public  void test2(){
	//TODO 测试p命名空间注入
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-injection.xml");
    Person person = context.getBean("person", Person.class);
    System.out.println(person);
 }

1.4 spel注入

spring Expression Language:spring表达式语言

  • 配置:
 <bean name="car"  class="com.bruceliu.spring.bean.Car" >
           <property name="name" value="mime" />
           <property name="color" value="白色"/>
  </bean>

   <!--利用spel引入car的属性 -->
   <bean  name="person1"  class="com.bruceliu.spring.bean.Person"  p:car-ref="car">
            <property name="name" value="#{car.name}"/>
            <property name="age" value="#{person.age}"/>

   </bean>
  • 测试
@Test
public  void test3(){
	//TODO 测试spel注入
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-injection.xml");
    Person person = context.getBean("person1", Person.class);
    System.out.println(person);
	
 }

1.5 复杂类型注入

创建配置文件:application-collection.xml
创建测试代码:CollectionTest.java
创建测试实体类:TestCollection

  • 创建TestCollection:
/**
 * arr list map properties的注入
 */
public class TestCollection {

    private Object [] arrs;
    private List<Object> list;
    private Map<String,Object> map;
    private Properties properties;

    public Object[] getArrs() {
        return arrs;
    }

    public void setArrs(Object[] arrs) {
        this.arrs = arrs;
    }

    public List<Object> getList() {
        return list;
    }

    public void setList(List<Object> list) {
        this.list = list;
    }

    public Map<String, Object> getMap() {
        return map;
    }

    public void setMap(Map<String, Object> map) {
        this.map = map;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    @Override
    public String toString() {
        return "TestCollection{" +
                "arrs=" + Arrays.toString(arrs) +
                ", list=" + list +
                ", map=" + map +
                ", properties=" + properties +
                '}';
    }
}
  • 配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


       <bean name="car" class="com.bruceliu.spring.bean.Car">
             <property name="name" value="保时捷"/>
             <property name="color" value="红色" />
       </bean>


       <bean name="testColl" class="com.bruceliu.spring.bean.TestCollection">

           <!-- 数组变量注入 -->
           <property name="arrs">
               <list>
                   <value>数组1</value>
                   <!--引入其他类型-->
                   <ref bean="car"/>
               </list>
           </property>

           <!-- 集合变量赋值-->
           <property name="list">
                <list>
                    <value>集合1</value>
                    <!--集合变量内部包含集合-->
                    <list>
                        <value>集合中的集合1</value>
                        <value>集合中的集合2</value>
                        <value>集合中的集合3</value>
                    </list>
                    <ref bean="car" />
                </list>
           </property>

           <!--map赋值 -->
           <property name="map">
               <map>
                   <entry key="car" value-ref="car" />
                   <entry key="name" value="保时捷" />
                   <entry key="age"  value="11"/>
               </map>

           </property>

           <!-- properties赋值 -->
           <property name="properties">
                <props>
                    <prop key="name">pro1</prop>
                    <prop key="age">111</prop>
                </props>
           </property>
       </bean>

</beans>
  • 测试:
 @Test
 public  void  test4(){
        //复杂类型注入练习
     ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-collection.xml");
     TestCollection textColl = context.getBean("testColl", TestCollection.class);
     System.out.println("testColl = " + textColl);
   }
发布了323 篇原创文章 · 获赞 88 · 访问量 2万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 精致技术 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览