Spring_002 依赖注入方式实现
一、Spring的依赖注入(DI)
在Spring框架下,当Bean实例A运行过程中需要引用另外一个Bean实例B时,Spring框架会创建Bean的实例B,并将实例B通过实例A的构造函数、set方法、自动装配和注解方式注入到实例A,这种注入实例Bean到另外一个实例Bean的过程叫做依赖注入。
依赖注入的好处就是尽可能隔离Bean之间的代码耦合,提高Bean重用的可能性,并尽量降低程序代码的维护难度,Spring框架通过依赖注入技术将不同的Bean融合起来,完成复杂业务操作,但又确保了每个Bean相对的独立性。
Spring框架提供构造函数注入、设置方法注入、自动装配注入和注解注入四种注入方式。
1、基于构造函数的依赖注入
BybService.java
public class BybService { public String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "BybService{" + "name='" + name + '\'' + '}'; } public String sayByb(){ System.out.println("Byb"+name); return "Byb"+name; } }
HelloWorld.java
public class HelloWorld { private String message; private BybService bybService; public HelloWorld(BybService bybService) { this.bybService=bybService; } public void setMessage(String message) { this.message = message; } public void getMessage() { System.out.println("message : " + message); } public void sayHello(){ System.out.println(bybService.sayByb()); } }
Bean.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"> <bean id="helloWorld" class="HelloWorld"> <constructor-arg ref="BybService"></constructor-arg> </bean> <bean id="BybService" class="BybService"> <property name="name" value="Hello World!" /> </bean> </beans>
Main.App
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); BybService bybService=(BybService) context.getBean("BybService"); obj.sayHello(); } }
运行MainApp.java
2、基于set方法注入(属性注入)
HelloWorld.java
public class HelloWorld { private String message; private BybService bybService; public void setMessage(String message) { this.message = message; } public void getMessage() { System.out.println("message : " + message); } public void sayHello(){ System.out.println(bybService.sayByb()); } public BybService getBybService() { return bybService; } public void setBybService(BybService bybService) { this.bybService = bybService; } }
BybService.java
public class BybService { public String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "BybService{" + "name='" + name + '\'' + '}'; } public String sayByb(){ System.out.println("Byb"+name); return "Byb"+name; } }
Bean.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"> <bean id="helloWorld" class="HelloWorld"> <property name="message" value="Hello World!" /> <property name="BybService" ref="BybService"></property> </bean> <bean id="BybService" class="BybService"> <property name="name" value="Hello World!" /> </bean> </beans>
MainApp.java
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); BybService bybService=(BybService) context.getBean("BybService"); obj.sayHello(); } }
运行MainApp.java
3、注解注入
Bean.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <bean id="helloWorld" class="HelloWorld"> <property name="message" value="aaa!" /> </bean> <bean id="BybService" class="BybService"> <property name="name" value="Hello World!" /> </bean> </beans>
BybService.java
public class BybService { public String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "BybService{" + "name='" + name + '\'' + '}'; } public String sayByb(){ System.out.println("Byb"+name); return "Byb"+name; } }
HelloWorld.java
import org.springframework.beans.factory.annotation.Autowired; public class HelloWorld { private String message; @Autowired private BybService bybService; public void setMessage(String message) { this.message = message; } public void getMessage() { System.out.println("message : " + message); } public void sayHello(){ System.out.println(bybService.sayByb()); } }
MainApp.java
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); BybService bybService=(BybService) context.getBean("BybService"); obj.sayHello(); //bybService.sayByb(); } }
运行结果如下:
二、Spring注入依赖的时间
Spring在创建beanFactory实例化类的时候,先加载类,然后调用类的构造器进行初始化,在完成初始化进行依赖注入(常见的就是set注入,通过执行set方法)。
三、使用eclipse创建注入Bean实例
继续写我们的第一个Spring程序,这次我们使用依赖注入的方式实现程序
第一步,建立我们的Spring_002程序,并在程序中添加BookDao.java、BookDaoImpl.java、BookService.java、BookServiceImpl.java、
ApplicationContext.xml
第二步 在BookDao接口中配置一个addBook方法
package com.zk.myspring; public interface BookDao { public void addBook(); }
在BookDaoImpl中实现addBook方法
package com.zk.myspring; public class BookDaoImpl implements BookDao{ @Override public void addBook() { // TODO Auto-generated method stub System.out.println("addBook"); } }
在BookService接口中配置addBook方法
package com.zk.myspring; public interface BookService { public abstract void addBook(); }
在BookServiceImpl方法中实现addBook方法
package com.zk.myspring; public class BookServiceImpl implements BookService{ //方式一:接口等于实现类 //private BookDao bookDao=new BookDaoImpl(); //方式二:接口+set方法 private BookDao bookdao; public void setBookdao(BookDao bookdao) {//依赖注入 this.bookdao = bookdao; } @Override public void addBook() { // TODO Auto-generated method stub //this.bookdao.addBook(); System.out.println("add book"); } }
注意在BookServiceImpl文件中,我们使用接口+set方法实现,并在setBookdao方法中实现依赖注入,使得BookService依赖于BookDao
第三步 配置我们的ApplicationContext.xml,使用property属性将BookDao注入进BookService
<property name="bookdao" ref="bookDaoId"></property>
<property> 用于属性注入
name:bean的属性名,通过setter方法获得
ref:另一个bean的id值的引用
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 创建Dao --> <!-- 模拟spring执行过程 创建Service实例:BookService bookservice=new BookServiceImpl(); IOC <bean> 创建Dao实例:BookDao bookDao=new BookDaoImpl(); IOC 将dao设置给service:bookservice.setBookDao(bookDao); DI <property> <property> 用于属性注入 name:bean的属性名,通过setter方法获得 ref:另一个bean的id值的引用 --> <!--创建Dao --> <bean id="bookDaoId" class="com.zk.myspring.BookDaoImpl"></bean> <!-- 创建service --> <bean id="bookServiceId" class="com.zk.myspring.BookServiceImpl"> <property name="bookdao" ref="bookDaoId"></property> </bean> </beans>
第四步 创建一个test方法,对配置文件进行测试
package com.zk.myspring; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource; public class TestBook { public static void main(String[]args) { //从spring容器中获得 String path="ApplicationContext.xml"; ApplicationContext ac=new ClassPathXmlApplicationContext(path); BookService bs=(BookService) ac.getBean("bookServiceId"); bs.addBook(); } @Test public void test1(){ //使用BeanFactory实例化 String path="ApplicationContext.xml"; BeanFactory bf=new XmlBeanFactory(new ClassPathResource(path)); BookService bs=(BookService) bf.getBean("bookServiceId"); bs.addBook(); } }
得出输出
依赖注入方式配置spring成功
三、使用ideal创建Spring依赖注入实例
首先创建一个Spring项目
创建BybService.java、HelloWorld.java、MainApp.java和资源文件Bean.xml
BybService.java
public class BybService { public String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "BybService{" + "name='" + name + '\'' + '}'; } public void sayByb(){ System.out.println("Byb"+name); } }
HelloWorld.java
public class HelloWorld { private String message; private BybService bybService; public void setMessage(String message) { this.message = message; } public void getMessage() { System.out.println("message : " + message); bybService.sayByb(); } public BybService getBybService() { return bybService; } public void setBybService(BybService bybService) { this.bybService = bybService; } }
MainApp.java
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); BybService bybService=(BybService) context.getBean("BybService"); obj.getMessage(); bybService.sayByb(); } }
Bean.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"> <bean id="helloWorld" class="HelloWorld"> <property name="message" value="Hello World!" /> <property name="bybService" ref="BybService"></property> </bean> <bean id="BybService" class="BybService"> <property name="name" value="Hello World!" /> </bean> </beans>
Spring容器可以配置Bean,注入属性,而且可以维护Bean与Bean之间的关系。
<bean>标签的作用是当spring框架加载的时候,spring会自动创建一个bean,并放入内存。当ApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml");执行的时候,Spring容器就会被创建,同时applicationContext.xml中配置,Bean就会被创建放入(内存[HashMap/HashTable])中。