Spring配置——属性注入

1、set注入方式:

(1)注入的为值类型(八大数据类型)的数据

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
 <bean name="student" class="pers.zhb.domain.Student">
  <property name="sname" value="zhai"></property>
  <property name="snum" value="20200210"></property>
  <property name="sex" value="nan"></property>
 </bean>
</beans>

测试类:

public class Test {
    public void test1(){
        ApplicationContext applicationContext=new
                ClassPathXmlApplicationContext("applicationContext.xml");//创建容器对象
        Student student=(Student)applicationContext.getBean("student");
        System.out.println(student);
    }
    public static void main(String[] args){
        Test test=new Test();
        test.test1();
    }
}

 (2)注入的为引用数据类型的数据:

Student对象:

package pers.zhb.domain;
public class Student {
    private String snum;
    private String sname;
    private String sex;
    private Course course;
    public Course getCourse() {
        return course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

    public Student(){
        System.out.println("Student对象创建了!");
    }
    public String getSnum() {
        return snum;
    }

    public void setSnum(String snum) {
        this.snum = snum;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
    @Override
    public String toString() {
        return "Student{" +
                "snum='" + snum + '\'' +
                ", sname='" + sname + '\'' +
                ", sex='" + sex + '\'' +
                ", course=" + course +
                '}';
    }

    public void destory(){
        System.out.println("我是销毁的方法!");
    }

    public void init(){
        System.out.println("我是初始化的方法!");
    }

}

Course对象:

public class Course {
    private String cname;
    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }
    @Override
    public String toString() {
        return "Course{" +
                "cname='" + cname + '\'' +
                '}';
    }
}

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
 <bean name="student" class="pers.zhb.domain.Student">
  <property name="sname" value="zhai"></property>
  <property name="snum" value="20200210"></property>
  <property name="sex" value="nan"></property>
  <property name="course" ref="course"></property>
 </bean>
 <bean name="course" class="pers.zhb.domain.Course">
  <property name="cname" value="算法设计与分析"></property>
 </bean>
</beans>

测试类:

public class Test {
    public void test1(){
        ApplicationContext applicationContext=new
                ClassPathXmlApplicationContext("applicationContext.xml");//创建容器对象
        Student student=(Student)applicationContext.getBean("student");
        System.out.println(student);
    }
    public static void main(String[] args){
        Test test=new Test();
        test.test1();
    }
}

 2、构造函数注入:

创建Student对象:

package pers.zhb.domain;
public class Student {
    private String snum;
    private String sname;
    private String sex;
    private Course course;
    public Student(String snum, String sname, String sex, Course course) {
        this.snum = snum;
        this.sname = sname;
        this.sex = sex;
        this.course = course;
    }
    public Course getCourse() {
        return course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

    public Student(){
        System.out.println("Student对象创建了!");
    }
    public String getSnum() {
        return snum;
    }

    public void setSnum(String snum) {
        this.snum = snum;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
    @Override
    public String toString() {
        return "Student{" +
                "snum='" + snum + '\'' +
                ", sname='" + sname + '\'' +
                ", sex='" + sex + '\'' +
                ", course=" + course +
                '}';
    }

    public void destory(){
        System.out.println("我是销毁的方法!");
    }

    public void init(){
        System.out.println("我是初始化的方法!");
    }

}

需要在Student类中创建一个构造函数。

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
 <bean name="student" class="pers.zhb.domain.Student">
   <constructor-arg name="sname" value="zhai"></constructor-arg>
   <constructor-arg name="snum" value="123456"></constructor-arg>
   <constructor-arg name="sex" value="nan"></constructor-arg>
   <constructor-arg name="course" ref="course"></constructor-arg>
 </bean>
 <bean name="course" class="pers.zhb.domain.Course">
  <property name="cname" value="算法设计与分析"></property>
 </bean>
</beans>

配置文件中的配置需要和构造函数中属性的配置一一对应。

 其他属性:

index:指定构造函数的参数的索引
type:指定构造函数参数的类型

3、p名称空间方式

(1)导入p名称空间:

  xmlns:p="http://www.springframework.org/schema/p"

(2)配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
  <bean name="student" class="pers.zhb.domain.Student" p:sname="zhai"
        p:sex="nan" p:snum="1234" p:course-ref="course">
  </bean>
  <bean name="course" class="pers.zhb.domain.Course">
   <property name="cname" value="电子技术"></property>
  </bean>
</beans>

测试:

 4、spel(Spring表达式语言)注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="student" class="pers.zhb.domain.Student">
 <property name="sname" value="zhang"></property>
 <property name="snum" value="11111"></property>
 <property name="sex" value="nv"></property>
</bean>
 <bean name="course" class="pers.zhb.domain.Course">
 <property name="cname" value="电子技术"></property>
</bean>
 <bean name="student1" class="pers.zhb.domain.Student">
  <property name="sname" value="#{student.sname}"></property>
  <property name="snum" value="#{student.snum}"></property>
  <property name="sex" value="#{student.sex}"></property>
  <property name="course" ref="course"></property>
 </bean>
</beans>

可以直接去取已经创建的对象的值。

5、复杂类型注入

(1)数组类型:

添加一个值:

 <bean name="arr" class="pers.zhb.domain.CollectionBean">
  <property name="arr" value="加油!!"></property>
 </bean>

添加多个值:

 <bean name="arr" class="pers.zhb.domain.CollectionBean">
  <property name="arr">
   <array>
    <value>你好</value>
    <value>加油</value>
    <value>努力</value>
   </array>
  </property>
 </bean>

值加对象:

 <bean name="arr" class="pers.zhb.domain.CollectionBean">
  <property name="arr">
   <array>
    <value>你好</value>
    <value>加油</value>
    <value>努力</value>
    <ref bean="student"></ref>
   </array>
  </property>
 </bean>

第四个值为一个Student对象。

(2)List类型:

 <bean name="cb" class="pers.zhb.domain.CollectionBean">
  <property name="li">
    <list>
     <value>你好</value>
     <value>加油</value>
     <value>努力</value>
     <ref bean="student"></ref>
    </list>
  </property>
 </bean>

(3)map类型:

 <bean name="map" class="pers.zhb.domain.CollectionBean">
  <property name="map">
     <map>
      <entry key="key" value="value"></entry>
      <entry key="student" value-ref="student"></entry>
     </map>
  </property>
 </bean>

 (4)properties类型:

 <bean name="prop" class="pers.zhb.domain.CollectionBean">
  <property name="properties">
   <props>
    <prop key="key">key</prop>
   </props>
  </property>
 </bean>

猜你喜欢

转载自www.cnblogs.com/zhai1997/p/12291752.html