Spring的属性注入

72 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路

   在Spring中有多种类型的属性注入,如构造方法的方式属性注入、set方法的属性注入、set方法设置对象类型的属性注入、p名称空间的属性注入、spEL表达式的属性注入、集合类型属性注入。

构造方法的方式属性注入

1. 编写Student类

public class Student {
    private String name;
    private Integer age;
    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + ''' +
                ", age=" + age +
                '}';
    }
}

2. 配置applicationContext.xml,将Student交给Spring管理

<?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">
    <!--
   id:为自己起的名称
   class:为类的全路径
   init-method:对象创建时调用的初始化方法
   destory-method:对象销毁时调用的销毁方法
   scope:bean的作用范围,默认为singleton
    -->
    <!--开发人员张三的配置文件-->
    <bean id="userDao" class="com.ph.demo1.UserDaoOrcleImpl"
          init-method="initMethod" destroy-method="destoryMethod"
          factory-method="create"/>

    <bean id="student" class="com.ph.demo1.Student">
        <constructor-arg name="name" value="张三"/>
        <constructor-arg name="age" value="18"/>
    </bean>

    <!--引入开发人员李四的配置文件-->
    <import resource="applicationContext1.xml"/>
</beans>

3. 编写测试类

public class test {
 public static void main(String[] args){
     ApplicationContext applicationContext= new ClassPathXmlApplicationContext("applicationContext.xml");
     Student stu = (Student)applicationContext.getBean("student");
     System.out.println(stu);
 }
}

运行结果

图片1.png

set方法的属性注入

1. 编写Student类

public class Student {
    private String name;
    private Integer age;
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + ''' +
                ", age=" + age +
                '}';
    }
}

2. 配置applicationContext.xml

<?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">
    <!--
   id:为自己起的名称
   class:为类的全路径
   init-method:对象创建时调用的初始化方法
   destory-method:对象销毁时调用的销毁方法
   scope:bean的作用范围,默认为singleton
    -->
    <!--开发人员张三的配置文件-->
    <!--<bean id="userDao" class="com.ph.demo1.UserDaoOrcleImpl"-->
          <!--init-method="initMethod" destroy-method="destoryMethod"-->
          <!--factory-method="create"/>-->

    <bean id="student" class="com.ph.demo1.Student">
        <!--使用构造方法进行属性注入-->
        <!--<constructor-arg name="name" value="张三"/>-->
        <!--<constructor-arg name="age" value="18"/>-->
        <!--使用set方法进行属性注入-->
        <property name="name" value="张三"/>
        <property name="age" value="18"/>
    </bean>

    <!--引入开发人员李四的配置文件-->
    <!--<import resource="applicationContext1.xml"/>-->
</beans>

3. 编写测试类

public class test {
 public static void main(String[] args){
     ApplicationContext applicationContext= new ClassPathXmlApplicationContext("applicationContext.xml");
     Student stu = (Student)applicationContext.getBean("student");
     System.out.println(stu);
 }
}

运行结果

图片2.png

set方法设置对象类型的属性注入

1. 编写Cat类

public class Cat {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + ''' +
                '}';
    }
}

2. 编写Student类

public class Student {
    private String name;
    private Integer age;
    private Cat cat;//Cat类对象作为Student的属性
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + ''' +
                ", age=" + age +
                ", cat=" + cat +
                '}';
    }
}

3. 配置applicationContext.xml

<?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">
    <!--
   id:为自己起的名称
   class:为类的全路径
   init-method:对象创建时调用的初始化方法
   destory-method:对象销毁时调用的销毁方法
   scope:bean的作用范围,默认为singleton
    -->
    <!--开发人员张三的配置文件-->
    <!--<bean id="userDao" class="com.ph.demo1.UserDaoOrcleImpl"-->
          <!--init-method="initMethod" destroy-method="destoryMethod"-->
          <!--factory-method="create"/>-->
    <bean id="cat" class="com.ph.demo1.Cat">
        <property name="name" value="橘猫"/>
    </bean>
    <bean id="student" class="com.ph.demo1.Student">
        <!--使用构造方法进行属性注入-->
        <!--<constructor-arg name="name" value="张三"/>-->
        <!--<constructor-arg name="age" value="18"/>-->
        <!--使用set方法进行属性注入-->
        <property name="name" value="张三"/>
        <property name="age" value="18"/>
        <!--name为类中的对象属性,ref为配置的bean-->
        <property name="cat" ref="cat"/>
    </bean>

    <!--引入开发人员李四的配置文件-->
    <!--<import resource="applicationContext1.xml"/>-->
</beans>

4. 编写测试类

public class test {
 public static void main(String[] args){
     ApplicationContext applicationContext= new ClassPathXmlApplicationContext("applicationContext.xml");
     Student stu = (Student)applicationContext.getBean("student");
     System.out.println(stu);
 }
}

运行结果

图片3.png

p名称空间的属性注入

使用p名称空间则不需要写那么多的property

只需要在applicationContext.xml配置文件中加入****xmlns:** **p ="www.springframework.org/schema/p"

配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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">
    <!--
   id:为自己起的名称
   class:为类的全路径
   init-method:对象创建时调用的初始化方法
   destory-method:对象销毁时调用的销毁方法
   scope:bean的作用范围,默认为singleton
    -->
    <!--开发人员张三的配置文件-->
    <!--<bean id="userDao" class="com.ph.demo1.UserDaoOrcleImpl"-->
          <!--init-method="initMethod" destroy-method="destoryMethod"-->
          <!--factory-method="create"/>-->
    <!--<bean id="cat" class="com.ph.demo1.Cat">-->
        <!--<property name="name" value="橘猫"/>-->
    <!--</bean>-->
    <!--cat使用p命名空间-->
    <bean id="cat" class="com.ph.demo1.Cat" p:name="橘猫"></bean>
    <!--student使用p命名空间-->
    <bean id="student" class="com.ph.demo1.Student"
          p:name="张三" p:age="18" p:cat-ref="cat">
    </bean>
    <!--<bean id="student" class="com.ph.demo1.Student">-->
        <!--<!–使用构造方法进行属性注入–>-->
        <!--<!–<constructor-arg name="name" value="张三"/>–>-->
        <!--<!–<constructor-arg name="age" value="18"/>–>-->
        <!--<!–使用set方法进行属性注入–>-->
        <!--<property name="name" value="张三"/>-->
        <!--<property name="age" value="18"/>-->
        <!--<!–name为类中的对象属性,ref为配置的bean–>-->
        <!--<property name="cat" ref="cat"/>-->
    <!--</bean>-->

    <!--引入开发人员李四的配置文件-->
    <!--<import resource="applicationContext1.xml"/>-->
</beans>

Cat类、student类、测试类及运行结果同“三、set方法设置对象类型的属性注入”

  • spEL表达式的属性注入

如果value是字符串类型,则格式为#{‘xxx’}

否则格式为#{xxx}

配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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">
    <!--
   id:为自己起的名称
   class:为类的全路径
   init-method:对象创建时调用的初始化方法
   destory-method:对象销毁时调用的销毁方法
   scope:bean的作用范围,默认为singleton
    -->
    <!--开发人员张三的配置文件-->
    <!--<bean id="userDao" class="com.ph.demo1.UserDaoOrcleImpl"-->
          <!--init-method="initMethod" destroy-method="destoryMethod"-->
          <!--factory-method="create"/>-->
    <bean id="cat" class="com.ph.demo1.Cat">
        <property name="name" value="橘猫"/>
    </bean>

    <bean id="student" class="com.ph.demo1.Student">
        <!--使用构造方法进行属性注入-->
        <!--<constructor-arg name="name" value="张三"/>-->
        <!--<constructor-arg name="age" value="18"/>-->
        <!--使用set方法进行属性注入-->
        <!--<property name="name" value="张三"/>-->
        <!--<property name="age" value="18"/>-->
        <!--name为类中的对象属性,ref为配置的bean-->
        <!--<property name="cat" ref="cat"/>-->
        <!--使用spEL表达式方法进行属性注入-->
        <property name="name" value="#{'张三'}"/>
        <property name="age" value="#{18}"/>
        <!--name为类中的对象属性,value为配置的bean-->
        <property name="cat" value="#{cat}"/>
    </bean>

    <!--引入开发人员李四的配置文件-->
    <!--<import resource="applicationContext1.xml"/>-->
</beans>

Cat类、student类、测试类及运行结果同“三、set方法设置对象类型的属性注入”

集合类型属性注入

1. 数组

Student类

public class Student {
    private String arr[];//定义数组变量
    public void setArr(String[] arr) {
        this.arr = arr;
    }

    @Override
    public String toString() {
        return "Student{" +
                "arr=" + Arrays.toString(arr) +
                '}';
    }
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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 id="student" class="com.ph.demo1.Student">
       <property name="arr">
           <list>
               <value>张三</value>
               <value>李四</value>
               <value>王五</value>
           </list>
       </property>
    </bean>
    
</beans>

测试类

public class test {
 public static void main(String[] args){
     ApplicationContext applicationContext= new ClassPathXmlApplicationContext("applicationContext.xml");
     Student stu = (Student)applicationContext.getBean("student");
     System.out.println(stu);
 }
}

运行结果

图片4.png

2. List集合

Student类

public class Student {
    private List list;

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

    @Override
    public String toString() {
        return "Student{" +
                "list=" + list +
                '}';
    }
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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 id="student" class="com.ph.demo1.Student">
       <property name="list">
           <list>
               <value>张三</value>
               <value>李四</value>
               <value>王五</value>
               <value>赵六</value>
           </list>
       </property>
    </bean>

</beans>

测试类

public class test {
 public static void main(String[] args){
     ApplicationContext applicationContext= new ClassPathXmlApplicationContext("applicationContext.xml");
     Student stu = (Student)applicationContext.getBean("student");
     System.out.println(stu);
 }
}

运行结果

图片5.png

3. Set集合

Student 类

public class Student {
   private Set set;

    public void setSet(Set set) {
        this.set = set;
    }

    @Override
    public String toString() {
        return "Student{" +
                "set=" + set +
                '}';
    }
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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 id="student" class="com.ph.demo1.Student">
       <property name="set">
           <set>
               <value>张三</value>
               <value>李四</value>
           </set>
       </property>
    </bean>

</beans>

测试类

public class test {
 public static void main(String[] args){
     ApplicationContext applicationContext= new ClassPathXmlApplicationContext("applicationContext.xml");
     Student stu = (Student)applicationContext.getBean("student");
     System.out.println(stu);
 }
}

运行结果

图片6.png

4. Map集合

Student 类

public class Student {
   private Map map;

    public void setMap(Map map) {
        this.map = map;
    }

    @Override
    public String toString() {
        return "Student{" +
                "map=" + map +
                '}';
    }
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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 id="student" class="com.ph.demo1.Student">
       <property name="map">
          <map>
              <entry key="zs" value="张三"></entry>
              <entry key="ls" value="李四"></entry>
              <entry key="ww" value="王五"></entry>
          </map>
       </property>
    </bean>

</beans>

测试类

public class test {
 public static void main(String[] args){
     ApplicationContext applicationContext= new ClassPathXmlApplicationContext("applicationContext.xml");
     Student stu = (Student)applicationContext.getBean("student");
     System.out.println(stu);
 }
}

运行结果

图片7.png