Spring配置——属性注入

1、set注入方式:

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

配置文件:



 class="pers.zhb.domain.Student">
  
  
  
 

测试类:

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 + '\'' +
                '}';
    }
}

配置文件:



 class="pers.zhb.domain.Student">
  
  
  
  
 
 class="pers.zhb.domain.Course">
  
 

测试类:

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类中创建一个构造函数。

配置文件:



 class="pers.zhb.domain.Student">
   
   
   
   
 
 class="pers.zhb.domain.Course">
  
 

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

 

 

 其他属性:

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

3、p名称空间方式

(1)导入p名称空间:

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

(2)配置文件:



       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 ">
  class="pers.zhb.domain.Student" p:sname="zhai"
        p:sex="nan" p:snum="1234" p:course-ref="course">
  
  class="pers.zhb.domain.Course">
   
  

测试:

 

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



       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 ">
class="pers.zhb.domain.Student">
 
 
 

 class="pers.zhb.domain.Course">
 

 class="pers.zhb.domain.Student">
  
  
  
  
 

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

5、复杂类型注入

(1)数组类型:

添加一个值:

 class="pers.zhb.domain.CollectionBean">
  
 

添加多个值:

 class="pers.zhb.domain.CollectionBean">
  
   
    你好
    加油
    努力
   
  
 

值加对象:

 class="pers.zhb.domain.CollectionBean">
  
   
    你好
    加油
    努力
    
   
  
 

第四个值为一个Student对象。

(2)List类型:

 class="pers.zhb.domain.CollectionBean">
  
    
     你好
     加油
     努力
     
    
  
 

(3)map类型:

 class="pers.zhb.domain.CollectionBean">
  
     
      
      
     
  
 

 

 (4)properties类型:

 class="pers.zhb.domain.CollectionBean">
  
   
    key
   
  
 

 

你可能感兴趣的:(Spring配置——属性注入)