目录
一.setter注入(设置注入)
1.简单类型的set注入
2.引用类型的setter注入
二.构造注入
1.构造注入
2.按名称注入和按类型注入
3.多个配置文件的整合
一.setter注入(设置注入) 1.简单类型的set注入
package edu.tjdz.ba01; public class Student { private String name; private int age; public Student(){ System.out.println("Spring会调用类的无参数构造方法创建对象"); } public void setEmail(String email){ System.out.println("setEmail="+email); } public void setName(String name) { System.out.println("setName="+name); this.name = name.toUpperCase(); } public void setAge(int age) { System.out.println("setAge="+age); this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + ''' + ", age=" + age + '}'; } }
package edu.tjdz; import edu.tjdz.ba01.Student; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.Date; public class MyTest { @Test public void test01(){ System.out.println("===test01==="); String config = "ba01/applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(config); //从容器中获取Student对象 Student myStudent = (Student) ac.getBean("myStudent"); System.out.println("student对象="+myStudent); Date myDate = (Date) ac.getBean("mydate"); System.out.println("mydate="+myDate); } @Test public void test02(){ System.out.println("===test02==="); } }2.引用类型的setter注入
package edu.tjdz.ba02; public class School { private String name; private String address; public void setName(String name) { this.name = name; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "School{" + "name='" + name + ''' + ", address='" + address + ''' + '}'; } }
package edu.tjdz.ba02; public class Student { private String name; private int age; //声明一个引用类型 private School school; public Student(){ System.out.println("Spring会调用类的无参数构造方法创建对象"); } public void setName(String name) { System.out.println("setName="+name); this.name = name; } public void setAge(int age) { System.out.println("setAge="+age); this.age = age; } public void setSchool(School school) { System.out.println("setSchool="+school); this.school = school; } @Override public String toString() { return "Student{" + "name='" + name + ''' + ", age=" + age + ", school=" + school + '}'; } }
package edu.tjdz; import edu.tjdz.ba02.School; import edu.tjdz.ba02.Student; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.Date; public class MyTest02 { @Test public void test01(){ System.out.println("===test01==="); String config = "ba02/applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(config); //从容器中获取Student对象 Student myStudent = (Student) ac.getBean("myStudent"); System.out.println("student对象="+myStudent); } @Test public void test02(){ System.out.println("===test02==="); Student student = new Student(); student.setName("zhangsan"); student.setAge(20); School school = new School(); school.setName("天津电子"); school.setAddress("天津"); student.setSchool(school); System.out.println("Student=="+student); } }二.构造注入 1.构造注入
package edu.tjdz.ba03; public class School { private String name; private String address; public void setName(String name) { this.name = name; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "School{" + "name='" + name + ''' + ", address='" + address + ''' + '}'; } }
package edu.tjdz.ba03; public class Student { private String name; private int age; //声明一个引用类型 private School school; public Student(){ System.out.println("Spring会调用类的无参数构造方法创建对象"); } public Student(String myname,int myage,School myschool){ System.out.println("======Student有参数构造方法======="); //属性赋值 this.name=myname; this.age=myage; this.school=myschool; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void setSchool(School school) { this.school = school; } @Override public String toString() { return "Student{" + "name='" + name + ''' + ", age=" + age + ", school=" + school + '}'; } }
package edu.tjdz; import edu.tjdz.ba03.Student; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.File; public class MyTest03 { @Test public void test01(){ System.out.println("===test01==="); String config = "ba03/applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(config); //从容器中获取Student对象 Student myStudent = (Student) ac.getBean("myStudent3"); System.out.println("student对象="+myStudent); File myfile = (File) ac.getBean("myfile"); System.out.println("myfile的名字"+myfile.getName()); } @Test public void test02(){ System.out.println("===test01==="); String config = "ba03/applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(config); //从容器中获取Student对象 Student myStudent = (Student) ac.getBean("myStudent2"); System.out.println("student对象="+myStudent); } }
2.按名称注入和按类型注入
package edu.tjdz; import edu.tjdz.ba04.Student; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest04 { @Test public void test01(){ System.out.println("===test01==="); String config = "ba04/applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(config); //从容器中获取Student对象 Student myStudent = (Student) ac.getBean("myStudent"); System.out.println("student对象="+myStudent); } }3.多个配置文件的整合
package edu.tjdz; import edu.tjdz.ba06.Student; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest06 { @Test public void test01(){ System.out.println("===test01==="); String config = "ba06/total.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(config); //从容器中获取Student对象 Student myStudent = (Student) ac.getBean("myStudent"); System.out.println("student对象="+myStudent); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)