目录
spring是一个开源框架,它由Rod Johnson 创建。它是为了解决企业应用开发的复杂性而创建的。
目的:解决企业应用开发的复杂性
功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
范围:任何Java应用
简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
建一个maven项目
pom.xml
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0modelVersion>
- <groupId>com.cdlgroupId>
- <artifactId>T280_springartifactId>
- <packaging>warpackaging>
- <version>0.0.1-SNAPSHOTversion>
- <name>T280_spring Maven Webappname>
- <url>http://maven.apache.orgurl>
-
-
- <properties>
- <spring.version>5.0.1.RELEASEspring.version>
- <javax.servlet.version>4.0.0javax.servlet.version>
- <junit.version>4.12junit.version>
- properties>
-
- <dependencies>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>3.8.1version>
- <scope>testscope>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-contextartifactId>
- <version>${spring.version}version>
- dependency>
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-aspectsartifactId>
- <version>${spring.version}version>
- dependency>
-
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>${junit.version}version>
- <scope>testscope>
- dependency>
-
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>javax.servlet-apiartifactId>
- <version>${javax.servlet.version}version>
- <scope>providedscope>
- dependency>
- dependencies>
- <build>
- <finalName>T280_springfinalName>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-compiler-pluginartifactId>
- <version>3.7.0version>
- <configuration>
- <source>1.8source>
- <target>1.8target>
- <encoding>UTF-8encoding>
- configuration>
- plugin>
- plugins>
- build>
- project>
web.xml
- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
- version="3.1">
- <display-name>Archetype Created Web Applicationdisplay-name>
- web-app>
-
建一个com.cdl.biz的包
UserBiz
- package com.cdl.biz;
-
- /**
- * 用户业务类
- * @author Lenovo
- *
- */
- public interface UserBiz {
- void list();
- }
建一个com.cdl.biz.impl的包
UserBizImpl1
- package com.cdl.biz.impl;
-
- import com.cdl.biz.UserBiz;
-
- public class UserBizImpl1 implements UserBiz{
-
- @Override
- public void list() {
- System.out.println("查询用户数据。。按照年龄排序。");
- }
-
- }
UserBizImpl2
- package com.cdl.biz.impl;
-
- import com.cdl.biz.UserBiz;
-
- public class UserBizImpl2 implements UserBiz{
-
- @Override
- public void list() {
- System.out.println("查询用户数据。。按照入职时间排序。");
- }
-
- }
建一个com.cdl.web的包
OrderAction
- package com.cdl.web;
-
- import com.cdl.biz.UserBiz;
- import com.cdl.biz.impl.UserBizImpl1;
-
- public class OrderAction {
-
- private UserBiz userBiz = new UserBizImpl1();
-
- public void list() {
- userBiz.list();
- }
-
- }
UserAction
- package com.cdl.web;
-
- import com.cdl.biz.UserBiz;
- import com.cdl.biz.impl.UserBizImpl1;
-
- public class UserAction {
-
- private UserBiz userBiz = new UserBizImpl1();
-
- public void list() {
- userBiz.list();
- }
- }
常规做法即运用spring之前的处理方法
* 需求:
* 同时在用户模块,订单模块拿到所有的用户数据
*
* 需求变更1:
* 同时在用户模块,订单模块拿到所有的用户数据,并且要求用户数据是通过年龄排序的
* 对应策略:修改UserBiz中list方法,添加排序功能
*
* 需求变更2:
* 同时在用户模块,订单模块拿到所有的用户数据,并且要求用户数据是通过注册时间排序的
* 对应策略,修改UserBiz中list方法,添加排序功能,按照时间点排序
总结:
* 最原始:频繁修改业务层biz的代码
* 多实现:凡是涉及到用户业务层调用的地方,都需要修改代码
运用spring处理
将spring的配置文件加入
spring-context.xml
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
-
-
- beans>
注意:一定要在有网络的情况下进行,否则约束不生效
IOC的主要作用管理整个项目的Javabean:依靠依赖注入、控制反转的特点进行管理
spring-context.xml
-
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
-
-
-
- <bean class="com.cdl.biz.impl.UserBizImpl2" id="userBiz2">bean>
- <bean class="com.cdl.web.UserAction" id="userAction">
- <property name="userBiz" ref="userBiz2">property>
- bean>
- <bean class="com.cdl.web.OrderAction" id="orderAction">bean>
- beans>
UserAction
- package com.cdl.web;
-
- import com.cdl.biz.UserBiz;
- import com.cdl.biz.impl.UserBizImpl1;
-
- public class UserAction {
-
- //private UserBiz userBiz = new UserBizImpl1();
- private UserBiz userBiz;
-
- public void list() {
- userBiz.list();
- }
- public UserBiz getUserBiz() {
- return userBiz;
- }
- public void setUserBiz(UserBiz userBiz) {
- this.userBiz = userBiz;
- }
-
-
- }
建一个com.cdl.ioc的包
在里面建一个Demo1的类
建模,打印com.cdl.biz.impl.UserBizImpl2中的语句
- package com.cdl.ioc;
-
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import com.cdl.web.UserAction;
-
- public class Demo1 {
- @SuppressWarnings("resource")
- public static void main(String[] args) {
- //建模
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
- UserAction userAction = (UserAction) context.getBean("userAction");
- userAction.list();
- }
-
-
- }
运行结果:

可见 能拿到
总结
1.对spring框架的配置文件进行建模,建模之后spring-context.xml中所有的Javabean信息
都会加载进spring容器的上下文中
2.上下文中就包含了spring-context.xml 所有对象
测试orderaction也拿到相同结果,配置和orderaction都如useraction一致修改
- package com.cdl.web;
-
- import com.cdl.biz.UserBiz;
- import com.cdl.biz.impl.UserBizImpl1;
-
- public class OrderAction {
-
- //private UserBiz userBiz = new UserBizImpl1();
-
- private UserBiz userBiz;
-
- public void list() {
- userBiz.list();
- }
-
- public UserBiz getUserBiz() {
- return userBiz;
- }
-
- public void setUserBiz(UserBiz userBiz) {
- this.userBiz = userBiz;
- }
-
-
-
- }
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
-
-
-
- <bean class="com.cdl.biz.impl.UserBizImpl2" id="userBiz2">bean>
- <bean class="com.cdl.web.UserAction" id="userAction">
- <property name="userBiz" ref="userBiz2">property>
- bean>
- <bean class="com.cdl.web.OrderAction" id="orderAction">
- <property name="userBiz" ref="userBiz2">property>
- bean>
- beans>
- package com.cdl.ioc;
-
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import com.cdl.web.OrderAction;
- import com.cdl.web.UserAction;
- /**
- * 1.对spring框架的配置文件进行建模,建模之后spring-context.xml中所有的Javabean信息
- * 都会加载进spring容器的上下文中
- * 2.上下文中就包含了spring-context.xml 所有对象
- * @author Lenovo
- *
- */
- public class Demo1 {
- @SuppressWarnings("resource")
- public static void main(String[] args) {
- //建模
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
- UserAction userAction = (UserAction) context.getBean("userAction");
- userAction.list();
-
- OrderAction orderAction = (OrderAction) context.getBean("orderAction");
- orderAction.list();
- }
-
-
- }
结果:

若是要按年龄排序,就只需要修改配置文件就OK了
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
-
-
- <bean class="com.cdl.biz.impl.UserBizImpl1" id="userBiz2">bean>
- <bean class="com.cdl.web.UserAction" id="userAction">
- <property name="userBiz" ref="userBiz2">property>
- bean>
- <bean class="com.cdl.web.OrderAction" id="orderAction">
- <property name="userBiz" ref="userBiz2">property>
- bean>
- beans>

结论:IOC特点(控制反转):将创建对象的权利反转给spring容器来完成


可见报错的原因就是找不到set的方法
接下来用一个例子(多注入三个属性)来更深入的了解
UserAction
- package com.cdl.web;
-
- import java.util.List;
-
- import com.cdl.biz.UserBiz;
- import com.cdl.biz.impl.UserBizImpl1;
-
- public class UserAction {
-
- //private UserBiz userBiz = new UserBizImpl1();
- private UserBiz userBiz;
-
-
- public UserBiz getUserBiz() {
- return userBiz;
- }
- public void setUserBiz(UserBiz userBiz) {
- this.userBiz = userBiz;
- }
-
- private String name;
- private int age;
- private List
hobby; -
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- public List
getHobby() { - return hobby;
- }
-
- public void setHobby(List
hobby) { - this.hobby = hobby;
- }
-
- public void list() {
- System.out.println(name);
- System.out.println(age);
- System.out.println(hobby);
- userBiz.list();
- }
-
- }
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
-
-
- <bean class="com.cdl.biz.impl.UserBizImpl1" id="userBiz2">bean>
- <bean class="com.cdl.web.UserAction" id="userAction">
- <property name="userBiz" ref="userBiz2">property>
-
- <property name="age" value="22">property>
- <property name="name" value="cdl">property>
- <property name="hobby">
- <list>
- <value>篮球value>
- <value>足球value>
- <value>唱歌value>
- list>
- property>
- bean>
- <bean class="com.cdl.web.OrderAction" id="orderAction">
- <property name="userBiz" ref="userBiz2">property>
- bean>
- beans>
- package com.cdl.ioc;
-
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import com.cdl.web.OrderAction;
- import com.cdl.web.UserAction;
- /**
- * 1.对spring框架的配置文件进行建模,建模之后spring-context.xml中所有的Javabean信息
- * 都会加载进spring容器的上下文中
- * 2.上下文中就包含了spring-context.xml 所有对象
- * @author Lenovo
- *
- * IOC特点(控制反转):将创建对象的权利反转给spring容器来完成
- *
- *
- */
- public class Demo1 {
- @SuppressWarnings("resource")
- public static void main(String[] args) {
- //建模
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
- UserAction userAction = (UserAction) context.getBean("userAction");
- userAction.list();
-
- OrderAction orderAction = (OrderAction) context.getBean("orderAction");
- orderAction.list();
- }
-
-
- }
结果:

OrderAction
- package com.cdl.web;
-
- import java.util.List;
-
- import com.cdl.biz.UserBiz;
- import com.cdl.biz.impl.UserBizImpl1;
-
- /**
- * 依赖注入的三种方式:
- * 1.set注入
- * 2.构造注入
- * 3.自动装配
- * byName
- * byType
- * @author Lenovo
- *
- */
- public class OrderAction {
-
- //private UserBiz userBiz = new UserBizImpl1();
-
- private UserBiz userBiz;
-
-
- public UserBiz getUserBiz() {
- return userBiz;
- }
-
- public void setUserBiz(UserBiz userBiz) {
- this.userBiz = userBiz;
- }
-
- private String name;
- private int age;
- private List
hobby; -
- public OrderAction(String name, int age, List
hobby) { - super();
- this.name = name;
- this.age = age;
- this.hobby = hobby;
- }
-
- public OrderAction() {
- super();
- // TODO Auto-generated constructor stub
- }
-
- public void list() {
- System.out.println(name);
- System.out.println(age);
- System.out.println(hobby);
- userBiz.list();
- }
-
- }
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
-
-
- <bean class="com.cdl.biz.impl.UserBizImpl1" id="userBiz2">bean>
-
-
- <bean class="com.cdl.web.UserAction" id="userAction">
- <property name="userBiz" ref="userBiz2">property>
- <property name="age" value="22">property>
- <property name="name" value="cdl">property>
- <property name="hobby">
- <list>
- <value>篮球value>
- <value>足球value>
- <value>唱歌value>
- list>
- property>
- bean>
-
-
- <bean class="com.cdl.web.OrderAction" id="orderAction">
- <property name="userBiz" ref="userBiz2">property>
- <constructor-arg name="name" value="cdl">constructor-arg>
- <constructor-arg name="age" value="23">constructor-arg>
- <constructor-arg name="hobby">
- <list>
- <value>篮球1value>
- <value>足球1value>
- <value>唱歌1value>
- list>
- constructor-arg>
- bean>
- beans>
在Demo1运行

将set和构造的注入 注释
byType不报错 byName报错




byType报错 byName不报错




自动装配
default-autowire="byName"
byName:是通过spring管理的bean对象的ID进行查找,如果找不到,则注入失败,反之成功
byType:是通过spring管理的bean对象的接口实现类进行查找,如果没有或者2个以上,则注入失败,反之成功
分析:
spring与web容器的整合原理
why:建模的过程是十分耗时的
解决问题:
1.建模必不可少
2.建模只保障只执行一次
3.建模后期望在每一个servlet都能够拿到spring的上下文对象ClassPathXmlApplicationContext
how:
1.监听器的初始化方法 只执行一次
2.spring的上下文要存放在Tomcat上下文中
建一个包com.cdl.ioc.listener
- package com.cdl.ioc.listener;
-
- import javax.servlet.ServletContext;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
-
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class SpringLoadListener implements ServletContextListener{
-
- @Override
- public void contextInitialized(ServletContextEvent sce) {
- System.out.println("初始化执行");
- ServletContext servletContext = sce.getServletContext();
- String springConfigLocation = servletContext.getInitParameter("springConfigLocation");
- System.out.println(springConfigLocation+"...");
- //拿到spring上下文
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
- //将spring的上下文保存在Tomcat上下文中
- servletContext.setAttribute("springContext", context);
- }
-
- }
在com.cdl.ioc中建一个类DemoServlet
- package com.cdl.ioc;
-
- import java.io.IOException;
-
- 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 org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import com.cdl.web.UserAction;
-
- /**
- * spring与web容器的整合原理
- * why:建模的过程是十分耗时的
- * 解决问题:
- * 1.建模必不可少
- * 2.建模只保障只执行一次
- * 3.建模后期望在每一个servlet都能够拿到spring的上下文对象ClassPathXmlApplicationContext
- * how:
- * 1.监听器的初始化方法 只执行一次
- * 2.spring的上下文要存放在Tomcat上下文中
- * @author Lenovo
- *
- */
- @WebServlet("/springDemo")
- public class DemoServlet extends HttpServlet{
-
- @Override
- protected void service(HttpServletRequest arg0, HttpServletResponse arg1){
- //ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
- ClassPathXmlApplicationContext context = (ClassPathXmlApplicationContext) arg0.getServletContext().getAttribute("springContext");
- UserAction userAction = (UserAction) context.getBean("userAction");
- userAction.list();
- }
-
- }
spring-context.xml
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
-
-
- <bean class="com.cdl.biz.impl.UserBizImpl1" id="userBiz">bean>
- <bean class="com.cdl.biz.impl.UserBizImpl1" id="userBiz1">bean>
-
- <bean class="com.cdl.web.UserAction" id="userAction">
- <property name="userBiz" ref="userBiz1">property>
- <property name="age" value="22">property>
- <property name="name" value="cdl">property>
- <property name="hobby">
- <list>
- <value>篮球value>
- <value>足球value>
- <value>唱歌value>
- list>
- property>
- bean>
-
-
- <bean class="com.cdl.web.OrderAction" id="orderAction">
- <property name="userBiz" ref="userBiz1">property>
- <constructor-arg name="name" value="cdl">constructor-arg>
- <constructor-arg name="age" value="23">constructor-arg>
- <constructor-arg name="hobby">
- <list>
- <value>篮球1value>
- <value>足球1value>
- <value>唱歌1value>
- list>
- constructor-arg>
- bean>
-
-
-
-
- beans>
web.xml
- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
- version="3.1">
- <display-name>Archetype Created Web Applicationdisplay-name>
-
- <context-param>
- <param-name>springConfigLocationparam-name>
- <param-value>/applicationContext.xmlparam-value>
- context-param>
-
- <listener>
- <listener-class>com.cdl.ioc.listener.SpringLoadListenerlistener-class>
- listener>
- web-app>
-
结果:
