Spring 一:Spring IOC容器
概要
Spring诞生于解决企业应用开发的复杂性,通过不断的发展,运用越来越广,目前主要的功能有IOC(控制反转)、Bean、AOP(面向切面编程)、springFramework框架等。
spring的特点:①轻量级:在大小和开销两方面而言spring都是轻量的;②开发应用简单、方便、快捷;③为复杂的JavaEE开发带来了新的春天。
spring作用:是一个容器,提供了多种技术的支持;AOP的事务管理;提供了方便的辅助类(JDBC Template等);对主流的框架提供了良好的支持(如:Hibeinate、mybatis等)。
适用范围:构建ssm应用、单独使用Bean、单独使用AOP进行面向切面编程、其他的spring功能:消息的支持。
IOC控制反转
控制反转即控制权的转移,应用程序本身不负责依赖对象的创建和维护,这些工作都交给了外部容器负责创建和维护;我们可以把IOC比做成房屋中介;
房屋中介是负责把房子建造起来,然后向需要房子的提供房子,然而需要房子的那些人不需要自己画图纸自己建造房子,直接通过中介直接使用房子。回来看IOC就是专门创建对象和维护对象的角色,然而应用程序就不需要创建这些,直接找IOC拿来使用就行了(以前我们的应用都是自己new对象,自己维护,什么时候销毁都是在外部的,当程序的复杂度越高的时候,那么这些都相当的繁琐。这就是IOC的意义)。
DI:即依赖注入,是IOC的实现方式,获得依赖对象的过程被反转,获得依赖对象的过程由自身管理变成IOC容器的注入,主要有setter注入和构造器注入这两种方式
IOC的实现
配置spring的配置文件spring-config.xml,该文件放在resources目录中。
<?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">
</beans>
我们创建一个接口类OneInterface和这个接口的实现类OneInterfaceImpl
为什么要这么做?
在Spring结构设计中,要分清层次及调用关系,每层只向外(上层)提供一组功能接口,各层之间只依赖于接口,并不是实现类;接口实现的变动不影响各层之间的调用,这一点是最为重要的;在spring这些结构设计中,面向接口编程是尤为重要的。
OneInterface
1 package spring_Ioc;
5
6 public interface OneInterface {
7 String werite(String str);
8 }
OneInterfaceImpl
1 package spring_Ioc; 2 3 public class OneInterfaceImpl implements OneInterface { 4 5 @Override 6 public String werite(String str) { 7 // TODO Auto-generated method stub 8 return "xxxx"+str; 9 } 10 }
接下来在spring-config.xml中配置bean。注:在IOC容器中将所有对象都称为Bean。
配置如上图:其中的id是在访问时需要的参数,class表示该类的位置。
在新建一个Main测试类来运行上面结果;通过ClassPathXmlApplicationContext来获取spring-config里面的内容,然后使用getBean来获取配置文件中的Bean属性。
运行结果
xxxxyangxin
注:关于使用ClassPathXmlApplicationContext的问题,需要在Pom.xml中配置如下依赖,不然会出错。
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency>
Spring的注入方式
指在启动spring容器加载bean的配置的时候,完成对变量的赋值行为,主要有setter注入和构造器注入两种方式。
创建InjectDaoInterface和InjectDaoInterfaceImpl类,和InjectServiceInterface和InjectServiceInterfaceImpl类;这四个类分别是两个抽象接口,两个相应接口的实现类,在InjectServiceInterface和InjectDaoInterface模拟服务层和Dao的相应操作,Service中调用Dao的方法,故在InjectServiceInterface有InjectDaoInterface的成员变量。要实现set注入和构造器注入,那么要在InjectServiceInterfaceImpl中实现set方法和Constructor方法。
1 package dao; 2 3 public interface InjectDaoInterface { 4 5 public void show(String str); 6 7 } 8 9 /////////////////////////////////////////////////// 10 /////////////////////////////////////////////////// 11 /////////////////////////////////////////////////// 12 13 package dao; 14 15 public class InjectDaoInterfaceImpl implements InjectDaoInterface { 16 17 @Override 18 public void show(String str) { 19 System.out.println("模拟数据处理"+str); 20 21 } 22 23 } 24 25 26 /////////////////////////////////////////////////// 27 /////////////////////////////////////////////////// 28 /////////////////////////////////////////////////// 29 30 package service; 31 32 public interface InjectServiceInterface { 33 void show(String str); 34 } 35 36 /////////////////////////////////////////////////// 37 /////////////////////////////////////////////////// 38 /////////////////////////////////////////////////// 39 40 package service; 41 42 import dao.InjectDaoInterface; 43 44 public class InjectServiceInterfaceImpl implements InjectServiceInterface { 45 46 private InjectDaoInterface injectDaoInterface; 47 48 49 public InjectServiceInterfaceImpl(InjectDaoInterface injectDaoInterface) { 50 this.injectDaoInterface=injectDaoInterface; 51 } 52 53 public void setInjectDaoInterface(InjectDaoInterface injectDaoInterface) { 54 this.injectDaoInterface = injectDaoInterface; 55 } 56 57 58 59 @Override 60 public void show(String str) { 61 62 System.out.println("service层"+str); 63 64 String newstr=str+this.hashCode(); 65 injectDaoInterface.show(newstr); 66 67 } 68 69 }
配置相应的bean,然后在Service的bean中实现 property或者constructor-arg注入两种方式,但是实现这两种方式需要在对应的类中实现set和构造方法。
ps:为了好截图,我把其中一个的注释去掉了的。
1 public class Main { 2 3 public static void main(String[] args) { 4 ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml"); 5 6 7 InjectServiceInterface injectServiceInterface=(InjectServiceInterface)context.getBean("injectServiceInterface"); 8 injectServiceInterface.show("yangxin"); 9 } 10 11 }
运行截图
service层yangxin
模拟数据处理yangxin1007653873