Spring之IOC
一.Spring
Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。
Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
Spring:
目的:解决企业应用开发的复杂性
功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
范围:任何Java应用
简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
Springspring包含的模块
1.1 中间层框架、万能胶
struts2
spring
hibernate
1.2 容器框架
JavaBean 项目中的一个个类
IOC和AOP
二. 什么是控制反转(或依赖注入)
控制反转(IoC=Inversion of Control)IoC,用白话来讲,就是由容器控制程序之间的(依赖)关系,而非传统实现中,由程序代码直接操控。
这也就是所谓“控制反转”的概念所在:(依赖)控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转。
IoC还有一个另外的名字:“依赖注入 (DI=Dependency Injection)” ,即由容器动态的将某种依赖关系注入到组件之中
IOC/DI
将以前由程序员实例化对象/赋值的工作交给了spring处理
三.案例:实现Spring的IoC
导入核心pom依赖
1 <dependency> 2 <groupId>org.springframework</groupId> 3 <artifactId>spring-context</artifactId> 4 <version>4.3.10.RELEASE</version> 5 </dependency>
安装Spring插件可进行手动安装和自动安装
手动安装:解压Spring安装包,点击Eclipse中的Help中的Instell NewSoft...
自动安装: 在Eclipse的Help中的Marketplace 搜索Spring,直接install
set注入
UserAction
1 package com.Spring.ioc.web; 2 3 import java.util.List; 4 5 import com.Spring.ioc.biz.UserBiz; 6 import com.Spring.ioc.impl.UserBizmpl1; 7 import com.Spring.ioc.impl.UserBizmpl2; 8 9 /** 10 * Ioc的注入方式及各类类型 有三种注入方式: 11 * 12 * set注入 13 * 基本类型与String 14 * 数组 15 * 自定义类型 16 * 构造注入 17 * 自动装配 18 * @author Administrator 19 * 20 */ 21 public class UserAction { 22 23 private UserBiz userBiz; 24 private String uname; 25 private int age; 26 private List<String> hobby; 27 28 29 30 public String getUname() { 31 return uname; 32 } 33 34 public void setUname(String uname) { 35 this.uname = uname; 36 } 37 38 public int getAge() { 39 return age; 40 } 41 42 public void setAge(int age) { 43 this.age = age; 44 } 45 46 public List<String> getHobby() { 47 return hobby; 48 } 49 50 public void setHobby(List<String> hobby) { 51 this.hobby = hobby; 52 } 53 54 public UserBiz getUserBiz() { 55 return userBiz; 56 } 57 58 public void setUserBiz(UserBiz userBiz) { 59 this.userBiz = userBiz; 60 } 61 62 public void upload() { 63 userBiz.upload(); 64 } 65 /** 66 * 注入问题 67 */ 68 public void test1() { 69 System.out.println(this.uname); 70 System.out.println(this.age); 71 System.out.println(this.hobby); 72 73 } 74 }
spring-context.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 8 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 11 <bean class="com.qukang.ioc.biz.UserBizImpl2" id="userBiz"></bean> 12 <bean class="com.qukang.ioc.web.UserAction" id="xxx"> 13 <!-- set注入用property标签 --> 14 <property name="userBiz" ref="userBiz"></property> 15 <property name="uname" value="CXK"></property> 16 <property name="age" value="22"></property> 17 <property name="hobby"> 18 <list> 19 <value>唱</value> 20 <value>跳</value> 21 <value>Rap</value> 22 </list> 23 </property> 24 </bean> 25 <bean class="com.qukang.ioc.web.OrderAction" id="ttt"> 26 <property name="userBiz" ref="userBiz"></property> 27 </bean> 28 </beans>
测试
IocTest
1 package com.Spring.ioc.test; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 import com.Spring.ioc.web.OrderAction; 7 import com.Spring.ioc.web.UserAction; 8 9 public class IocTest { 10 11 public static void main(String[] args) { 12 // UserAction userAction = new UserAction(); 13 // userAction.upload(); 14 15 ApplicationContext springContext = new ClassPathXmlApplicationContext("/spring-context.xml"); 16 UserAction userAction = (UserAction) springContext.getBean("xxx"); 17 //OrderAction orderAction = (OrderAction) springContext.getBean("ttt"); 18 19 //userAction.upload(); 20 //orderAction.upload(); 21 userAction.test1(); 22 } 23 }
构造器注入
UserAction
1 package com.Spring.ioc.web; 2 3 import java.util.List; 4 5 import com.Spring.ioc.biz.UserBiz; 6 import com.Spring.ioc.impl.UserBizmpl1; 7 import com.Spring.ioc.impl.UserBizmpl2; 8 9 /** 10 * Ioc的注入方式及各类类型 有三种注入方式: 11 * 12 * set注入 13 * 基本类型与String 14 * 数组 15 * 自定义类型 16 * 构造注入 17 * 自动装配 18 * spring4之后出现的 19 * byType:根据配置夫人bean中的接口,在spring的上下文中寻找对应的实现类 20 * byName:会让你据配置的bean中的接口名字,在spring的上下文中寻找对应的实体类 21 * @author Administrator 22 * 23 */ 24 public class UserAction { 25 26 private UserBiz userBiz; 27 private String uname; 28 private int age; 29 private List<String> hobby; 30 31 32 public UserAction(String uname, int age) { 33 super(); 34 this.uname = uname; 35 this.age = age; 36 } 37 38 39 public UserAction() { 40 super(); 41 } 42 43 44 public List<String> getHobby() { 45 return hobby; 46 } 47 48 public void setHobby(List<String> hobby) { 49 this.hobby = hobby; 50 } 51 52 public UserBiz getUserBiz() { 53 return userBiz; 54 } 55 56 public void setUserBiz(UserBiz userBiz) { 57 this.userBiz = userBiz; 58 } 59 60 public void upload() { 61 userBiz.upload(); 62 } 63 /** 64 * 注入问题 65 */ 66 public void test1() { 67 System.out.println(this.uname); 68 System.out.println(this.age); 69 System.out.println(this.hobby); 70 71 } 72 }
在之前配置好的spring-context.xml中加入
测试类
IocTest
package com.Spring.ioc.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.Spring.ioc.web.OrderAction; import com.Spring.ioc.web.UserAction; public class IocTest { public static void main(String[] args) { // UserAction userAction = new UserAction(); // userAction.upload(); ApplicationContext springContext = new ClassPathXmlApplicationContext("/spring-context.xml"); UserAction userAction = (UserAction) springContext.getBean("xxx"); OrderAction orderAction = (OrderAction) springContext.getBean("ttt"); // //userAction.upload(); //orderAction.upload(); userAction.test1(); } }
自动装配
byType:根据配置的Bean中的接口,在spring的上下文寻找对应的实现类
byName:根据配置的Bean中的接口名字,在spring的上下文寻找对应的实现类
四.tomcat整合ioc容器
Spring 作为管理整个工程的所有JavaBean,那么如何在用户发送请求的时候能够访问到指定的JavaBean?
处理方式:
在监听器中将spring的上下文交给tomcat的上下文进行管理
思路:
浏览器-->request-->servletcontext-->springcontext-->任意的JavaBean
监听器:SpringLoaderListener
1 package com.Spring.ioc.instener; 2 3 import javax.servlet.ServletContext; 4 import javax.servlet.ServletContextEvent; 5 import javax.servlet.ServletContextListener; 6 import javax.servlet.annotation.WebListener; 7 8 import org.springframework.context.ApplicationContext; 9 import org.springframework.context.support.ClassPathXmlApplicationContext; 10 /** 11 * Spring 作为管理整个工程的所有JavaBean,那么如何在用户发送请求的时候能够访问到指定的JavaBean? 12 * 处理方式: 13 * 在监听器中将spring的上下文交给tomcat的上下文进行管理 14 * 思路: 15 * 浏览器-->request-->servletcontext-->springcontext-->任意的JavaBean 16 * @author Administrator 17 * 18 */ 19 @WebListener 20 public class SpringLoaderListener implements ServletContextListener{ 21 @Override 22 public void contextInitialized(ServletContextEvent sce) { 23 // TODO Auto-generated method stub 24 System.out.println("tomcat--启动触发了...."); 25 //tomcat上下文 26 ServletContext tomcatContext = sce.getServletContext(); 27 String springXmlLocation=tomcatContext.getInitParameter("springXmlLocation"); 28 System.out.println("spring的上下文配置文件:"+springXmlLocation); 29 ApplicationContext springContext = null; 30 if (springXmlLocation == null || "" .equals(springXmlLocation)) { 31 springContext = new ClassPathXmlApplicationContext("/spring-context.xml"); 32 33 }else { 34 springContext = new ClassPathXmlApplicationContext(springXmlLocation); 35 36 } 37 //作用域 38 tomcatContext.setAttribute("spring_key", springContext); 39 } 40 }
配置监听器
web.xml
1 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 4 version="3.1"> 5 6 <display-name>Archetype Created Web Application</display-name> 7 <!-- 监听器参数 --> 8 <context-param> 9 <param-name>springXmlLocation</param-name> 10 <param-value>/spring-other.xml</param-value> 11 </context-param> 12 </web-app>
spring-other.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 default-autowire="byType" 8 xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 9 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 10 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 11 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 12 13 <bean class="com.Spring.ioc.impl.UserBizmpl2" id="userBiz"></bean> 14 <bean class="com.Spring.ioc.web.UserAction" id="xxx"> 15 <!-- set注入用property标签 --> 16 <!-- <property name="userBiz" ref="userBiz"></property> --> 17 <!-- <property name="uname" value="CXK"></property> 18 <property name="age" value="22"></property> --> 19 <!-- set注入用constructor-arg标签 --> 20 <constructor-arg name="uname" value="CXK"></constructor-arg> 21 <constructor-arg name="age" value="22"></constructor-arg> 22 <property name="hobby" > 23 <list> 24 <value>唱</value> 25 <value>跳</value> 26 <value>Rap</value> 27 </list> 28 29 </property> 30 </bean> 31 <bean class="com.Spring.ioc.web.OrderAction" id="ttt"> 32 <!-- <property name="userBiz" ref="userBiz"></property> --> 33 </bean> 34 35 </beans>
处理请求
UserServelt
1 package com.Spring.ioc.web; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.annotation.WebServlet; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 import org.springframework.context.ApplicationContext; 12 13 @WebServlet("/user") 14 public class UserServelt extends HttpServlet { 15 @Override 16 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 17 // TODO Auto-generated method stub 18 doPost(req, resp); 19 } 20 21 @Override 22 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 23 // TODO Auto-generated method stub 24 System.out.println("处理请求"); 25 ApplicationContext springContext = (ApplicationContext) req.getServletContext().getAttribute("spring_key"); 26 UserAction userAction = (UserAction) springContext.getBean("xxx"); 27 userAction.upload(); 28 29 } 30 }