Spring的简介:
含义:
Spring是Java EE编程领域的一个轻量级开源框架,该框架由一个叫Rod Johnson的程序员在 2002 年最早提出并随后创建,是为了解决企业级编程开发中的复杂性,实现敏捷开发的应用型框架 。Spring是一个开源容器框架,它集成各类型的工具,通过核心的Bean factory实现了底层的类的实例化和生命周期的管理。在整个框架中,各类型的功能被抽象成一个个的 Bean,这样就可以实现各种功能的管理,包括动态加载和切面编程
特点:
1.方便解耦,简化开发
通过Spring提供的IoC容器,我们可以将对象之间的依赖关系交由Spring进行控制,避免硬编码所造成的过度程序耦合。有了Spring,用户不必再为单实例模式类、属性文件解析等这些很底层的需求编写代码,可以更专注于上层的应用。
2.AOP编程的支持
通过Spring提供的AOP功能,方便进行面向切面的编程,许多不容易用传统OOP实现的功能可以通过AOP轻松应付。
3.声明式事务的支持
在Spring中,我们可以从单调烦闷的事务管理代码中解脱出来,通过声明式方式灵活地进行事务的管理,提高开发效率和质量。
4.方便程序的测试
可以用非容器依赖的编程方式进行几乎所有的测试工作,在Spring里,测试不再是昂贵的操作,而是随手可做的事情。例如:Spring对Junit4支持,可以通过注解方便的测试Spring程序。
5.方便集成各种优秀框架
Spring不排斥各种优秀的开源框架,相反,Spring可以降低各种框架的使用难度,Spring提供了对各种优秀框架(如Struts,Hibernate、Hessian、Quartz)等的直接支持。
6.降低Java EE API的使用难度
Spring对很多难用的Java EE API(如JDBC,JavaMail,远程调用等)提供了一个薄薄的封装层,通过Spring的简易封装,这些Java EE API的使用难度大为降低。
7.Java 源码是经典学习范例
Spring的源码设计精妙、结构清晰、匠心独运,处处体现着大师对Java设计模式灵活运用以及对Java技术的高深造诣。Spring框架源码无疑是Java技术的最佳实践范例。如果想在短时间内迅速提高自己的Java技术水平和应用开发水平,学习和研究Spring源码将会使你收到意想不到的效果。
Spring的IOC(控制反转)
IOC:控制反转,是一个理论,概念,思想。
描述的:把对象的创建,赋值,管理工作都交给代码之外的容器实现,也就是对象的创建是有其它外部资源完成。控制:创建对象,对象的属性赋值,对象之间的关系管理。
反转:把原来的开发人员管理,创建对象的权限转移给代码之外的容器实现。由容器代替开发人员管理对象。创建对象,给属性赋值。
正转:由开发人员在代码中,使用new 构造方法创建对象,开发人员主动管理对象。
容器:是一个服务器软件,一个框架(spring)
spring-context.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 class="com.xiaoxu.web.UserAction" id="userAction" > <property name="userService" ref="userService"></property> </bean> <bean class="com.xiaoxu.web.GoodsAction" id="goodsAction" > <property name="userService" ref="userService2"></property> </bean> <bean class="com.xiaoxu.service.impl.UserServiceimpl1" id="userService"></bean> <bean class="com.xiaoxu.service.impl.UserServiceimpl" id="userService2"></bean> </beans>
UserService
package com.xiaoxu.service; public interface UserService { public void update(); }
UserAction
package com.xiaoxu.web; import com.xiaoxu.service.UserService; public class UserAction { private UserService userService; public String update(){ userService.update(); return "list"; } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } }
GoodsAction
package com.xiaoxu.web; import com.xiaoxu.service.UserService; public class UserAction { private UserService userService; public String update(){ userService.update(); return "list"; } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } }
测试类
package com.xiaoxu.text; import com.xiaoxu.web.GoodsAction; import com.xiaoxu.web.UserAction; import org.springframework.context.support.ClassPathXmlApplicationContext; public class demo1 { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml"); UserAction userAction = (UserAction) context.getBean("userAction"); userAction.update(); GoodsAction goodsAction = (GoodsAction) context.getBean("goodsAction"); goodsAction.update(); } }
测试结果
注入方式
set注入
package com.xiaoxu.ioc.web; import com.xiaoxu.ioc.service.UserService; import java.util.List; public class GoodsAction { /** * 例如:在不同的控制器中进行方法调用 */ private UserService userService; private String gname;//名称 private int age;//保质期 private List<String> peoples;//使用人群 public String getGname() { return gname; } public void setGname(String gname) { this.gname = gname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<String> getPeoples() { return peoples; } public void setPeoples(List<String> peoples) { this.peoples = peoples; } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } public void pop(){ System.out.println("名称:"+this.gname); System.out.println("保证期:"+this.age); System.out.println("使用人群:"+this.peoples); } public String update(){ userService.update(); return "list"; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" default-autowire="byName" 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 class="com.CloudJun.ioc.web.UserAction" id="userAction"> <property name="userService" ref="userServiceImpl"></property> </bean> <bean class="com.CloudJun.ioc.web.GoodsAction" id="goodsAction"> <property name="userService" ref="userServiceImpl2"></property> <property name="gname" value="辣条"></property> <property name="age" value="2"></property> <property name="peoples"> <list> <value>懒大王</value> <value>美羊羊</value> <value>沸羊羊</value> </list> </property> </bean> <bean class="com.CloudJun.ioc.service.impl.UserServiceImpl" id="userService" ></bean> <bean class="com.CloudJun.ioc.service.impl.UserServiceImpl2" id="userServiceImpl2"></bean> </beans>
构造注入
package com.xiaoxu.ioc.web; import com.xiaoxu.ioc.service.UserService; import java.util.List; public class UserAction { private UserService userService ; private String uname;//姓名 private int age;//年龄 private List<String> hobby;//爱好 public UserAction() { } public UserAction(String uname, int age, List<String> hobby) { this.uname = uname; this.age = age; this.hobby = hobby; } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } public void pop(){ System.out.println("名字为:"+this.uname); System.out.println("年龄为:"+this.age); System.out.println("爱好为:"+this.hobby); } public String update(){ userService.update(); return "list"; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" default-autowire="byName" 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 class="com.CloudJun.ioc.web.UserAction" id="userAction"> <property name="userService" ref="userService"></property> <constructor-arg name="uname" value="阿波罗" ></constructor-arg> <constructor-arg name="age" value="18" ></constructor-arg> <constructor-arg name="hobby" > <list> <value>唱,跳</value> <value>篮球</value> </list> </constructor-arg> </bean> <bean class="com.CloudJun.ioc.web.GoodsAction" id="goodsAction"> <property name="userService" ref="userServiceImpl2"></property> <property name="gname" value="奥里给"></property> <property name="age" value="2"></property> <property name="peoples"> <list> <value>懒大王</value> <value>美羊羊</value> <value>沸羊羊</value> </list> </property> </bean> <bean class="com.CloudJun.ioc.service.impl.UserServiceImpl" id="userService" ></bean> <bean class="com.CloudJun.ioc.service.impl.UserServiceImpl2" id="userServiceImpl2"></bean> </beans>
接口注入
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" default-autowire="byName" 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"> <!-- 在spring配置文件spring-context.xml中配置,那么该类javabean就交给spring容器管理 --> <bean class="com.CloudJun.ioc.web.UserAction" id="userAction"> <!--<property name="userService" ref="userService"></property> <constructor-arg name="uname" value="阿波罗" ></constructor-arg> <constructor-arg name="age" value="18" ></constructor-arg> <constructor-arg name="hobby" > <list> <value>唱,跳</value> <value>篮球</value> </list> </constructor-arg>--> </bean> <bean class="com.CloudJun.ioc.web.GoodsAction" id="goodsAction"> <!-- <property name="userService" ref="userServiceImpl2"></property> <property name="gname" value="奥里给"></property> <property name="age" value="2"></property> <property name="peoples"> <list> <value>懒大王</value> <value>美羊羊</value> <value>沸羊羊</value> </list> </property>--> </bean> <bean class="com.CloudJun.ioc.service.impl.UserServiceImpl" id="userService" ></bean> <bean class="com.CloudJun.ioc.service.impl.UserServiceImpl2" id="userServiceImpl2"></bean> </beans>
web容器整合
配置监听器
package com.xiaoxu.listeer; import org.springframework.context.support.ClassPathXmlApplicationContext; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; @WebListener("") public class SpringLoadListener implements ServletContextListener { public void contextInitialized(ServletContextEvent sce) { //将spring上下文放入Tomcat上下文 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml"); //获取Tomcat上下文 ServletContext servletContext = sce.getServletContext(); servletContext.setAttribute("springContext",context); } }
配置servlet
package com.xiaoxu.servlet; import com.xiaoxu.service.UserService; import org.springframework.context.support.ClassPathXmlApplicationContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/userList") public class UserServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取spring上下文对象 ClassPathXmlApplicationContext context = (ClassPathXmlApplicationContext) req.getServletContext().getAttribute("springContext"); UserService userService = (UserService) context.getBean("userService"); userService.update(); System.out.println(userService); } }
运行结果