平常的Java开发中,程序员在某个类中需要依赖其它类的方法。
通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理。
Spring提出了依赖注入的思想,即依赖类不由程序员实例化,而是通过Spring容器帮我们new指定实例并且将实例注入到需要该对象的类中。
依赖注入的另一种说法是"控制反转"。通俗的理解是:平常我们new一个实例,这个实例的控制权是我们程序员。
而控制反转是指new实例工作不由我们程序员来做而是交给Spring容器来做。
1.Set注入
构建一个Student对象
package cn.happy.entity;/** * Created by CKW on 2017/3/19. */public class Student { private String sname; private Integer sage; public Integer getSage() { return sage; } public void setSage(Integer sage) { this.sage = sage; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; }}
在配置applicationContext.xml中:
测试类中:
//被Spring管理的bean默认都是单例的 @Test public void myTest1(){ //ac 就是Spring容器 ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); Student stu=(Student) ac.getBean("stu"); Student stu1=(Student) ac.getBean("stu"); System.out.println(stu+"\n"+stu1); }
2.构造器注入
在对象中添加构造
配置中:
测试类:
//构造注入 @Test public void myTest2(){ ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); Student stu1=(Student) ac.getBean("stu1"); Student stu2=(Student) ac.getBean("stu2"); System.out.println("构造:"+stu1); }
3.空间命名注入
//构造注入 命名空间注入 @Test public void myTest2(){ ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); Student stu1=(Student) ac.getBean("stu1"); Student stu2=(Student) ac.getBean("stu2"); System.out.println("构造:"+stu1+"\n命名空间:"+stu2); }
4.集合注入
哈哈 呵呵 嘿嘿 哈哈 呵呵 嘿嘿
5.注解注入
package cn.happy.entity;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;import org.springframework.stereotype.Controller;import org.springframework.stereotype.Repository;import org.springframework.stereotype.Service;/** * Created by CKW on 2017/3/22. */@Component("car") //不分层/*@Repository //dao层*//*@Service //biz层*//*@Controller //action层*/public class Car { @Value("特斯拉") private String cname; @Override public String toString() { return getCname(); } public Car() { } public Car(String cname) { this.cname = cname; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; }}
package cn.happy.entity;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;import javax.annotation.Resource;/** * Created by CKW on 2017/3/22. */@Component("student")public class Student { @Value("撒打发") private String sname; @Value("20") private Integer sage; //jdk注解// @Resource @Autowired @Qualifier("car") private Car car; @Override public String toString() { return "name="+getSname()+",age="+getSage()+",car="+getCar(); } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } public Student() { } public String getSname() { return sname; } public Student(String sname, Integer sage, Car car) { this.sname = sname; this.sage = sage; this.car = car; } public void setSname(String sname) { this.sname = sname; } public Integer getSage() { return sage; } public void setSage(Integer sage) { this.sage = sage; }}
在配置applicationContext.xml中:
测试类:
//注解 @Test public void myTest4(){ ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); Student stu=(Student)ac.getBean("student"); System.out.println(stu.getCar().getCname()); }