目录
Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。
Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。目的:解决企业应用开发的复杂性
功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
范围:任何的Java应用
简单来说,Spring是一个轻量级的控制反转(IOC)和面向切面(AOP)的容器框架。
先建立一个maven项目将以下代码加入到maven项目中的pom.xml中
- <properties>
- <spring.version>5.0.1.RELEASEspring.version>
- <javax.servlet.version>4.0.0javax.servlet.version>
- <junit.version>4.12junit.version>
- properties>

加入改代码块的是为了将当前项目jar依赖版本定义在外部,目的在于所有jar包版本进行统一管理
再将 以下代码块加入其中,替换掉原来
- <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>
后面的步骤和昨天配置的步骤一样,可以前往配置maven
在开发的时候,往往有这样的场景,有这样一个接口,里面放置的方法为增删改查等;需求:同时在用户模块、订单模块拿到所有的用户数据,并且要求用户数据是已经通过年龄排序了的,然而根据之前所学的对应策略:修改userBiz(业务层)中的方法,添加排序功能。但如果需求又发生改变呢?就可以想到多实现的方法。
可以总结一下:
最原始:频繁修改业务层biz层代码,这样bug容易很多。
多实现:凡是涉及到用户业务层调用的地方,都需要修改代码
将以下xml文件加入到src/main/resources中
- <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>

UserAction:
- package com.mgy.web;
-
- import com.mgy.biz.UserBiz;
- import com.mgy.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;
- }
-
-
- public void list() {
- userBiz.list();
- }
- }
该类和OrederAction一样的代码
将之前写的业务层和web加入到之中进行配置,切记要联网
- <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.mgy.biz.impl.UserBizImpl1" id="userBiz">bean>
- <bean class="com.mgy.web.UserAction" id="userAction">
- <property name="userBiz" ref="userBiz">property>
- bean>
- <bean class="com.mgy.web.OrederAction" id="orederAction">
- <property name="userBiz" ref="userBiz">property>
- bean>
- beans>
Demo1:
- package com.mgy.demo;
-
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import com.mgy.web.OrederAction;
- import com.mgy.web.UserAction;
-
- /**
- * 1.对Spring框架的配置晚间进行建模,建模之后,spring-context.xml中所有的Javabean信息都会加载进Spring容器的上下文中
- * 2.上下文中就包含spring-context.xml 所有对象
- * @author Administrator
- *
- * IOC的特点-什么叫控制反转
- * 指的是将创建对象的权力反转给Spring容器来完成
- *
- */
- public class Demo1 {
- public static void main(String[] args) {
- //建模
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
- UserAction userAction = (UserAction) context.getBean("userAction");
- userAction.list();
-
- OrederAction orederAction=(OrederAction)context.getBean("orederAction");
- orederAction.list();
- }
- }
效果:

之后就只需要修改spring-context.xml文件的中的第一行就可以了。
将其中一个action中的get和set方法注销后会产生报错

错误:

将其中一个action类加入字段,并且给set和get方法
- package com.mgy.web;
-
- import java.util.List;
-
- import com.mgy.biz.UserBiz;
- import com.mgy.biz.impl.UserBizImpl1;
-
- /**
- * 依赖注入的3种方式:
- * 1.set注入
- * 2.构造注入
- * 3.自动装配
- * @author Administrator
- *
- */
- 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();
- }
- }
在去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.mgy.biz.impl.UserBizImpl1" id="userBiz">bean>
- <bean class="com.mgy.web.UserAction" id="userAction">
- <property name="userBiz" ref="userBiz">property>
- <property name="name" value="zhangsan">property>
- <property name="age" value="22">property>
- <property name="hobby">
- <list>
- <value>篮球value>
- <value>boyvalue>
- <value>rapvalue>
- list>
- property>
- bean>
- <bean class="com.mgy.web.OrederAction" id="orederAction">
- <property name="userBiz" ref="userBiz">property>
- bean>
- beans>
property该标签没有list这个属性,所以要在该标签内部进行
效果图:
将字段给入构造方法,切记要给入无参的构造方法
- package com.mgy.web;
-
- import java.util.List;
-
- import com.mgy.biz.UserBiz;
- import com.mgy.biz.impl.UserBizImpl1;
-
- public class OrederAction {
- // 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 OrederAction() {
- // TODO Auto-generated constructor stub
- }
- public OrederAction(String name, int age, List
hobby) { - super();
- this.name = name;
- this.age = age;
- this.hobby = hobby;
- }
-
-
- public void list() {
- System.out.println(name);
- System.out.println(age);
- System.out.println(hobby);
- userBiz.list();
- }
- }
xml文件中的配置,构建注入配置和set注入的配置是不一样的
-
- <bean class="com.mgy.web.OrederAction" id="orederAction">
- <property name="userBiz" ref="userBiz">property>
- <constructor-arg name="name" value="zhangsan">constructor-arg>
- <constructor-arg name="age" value="23">constructor-arg>
- <constructor-arg name="hobby">
- <list>
- <value>篮球value>
- <value>boyvalue>
- <value>rapvalue>
- list>
- constructor-arg>
- bean>
效果:

根据spring中的id进行查询的,如果找不到则出现报错,反之成功
xml文件:
- <beans default-autowire="byName" 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.mgy.biz.impl.UserBizImpl1" id="userBiz">bean>
-
- <bean class="com.mgy.web.UserAction" id="userAction">
-
- <property name="name" value="zhangsan">property>
- <property name="age" value="22">property>
- <property name="hobby">
- <list>
- <value>篮球value>
- <value>boyvalue>
- <value>rapvalue>
- list>
- property>
- bean>
-
- <bean class="com.mgy.web.OrederAction" id="orederAction">
-
- <constructor-arg name="name" value="zhangsan">constructor-arg>
- <constructor-arg name="age" value="23">constructor-arg>
- <constructor-arg name="hobby">
- <list>
- <value>篮球value>
- <value>boyvalue>
- <value>rapvalue>
- list>
- constructor-arg>
- bean>
- beans>
效果:
根据spring中的bean对象接口实现类进行查询的,如果找不到或出现两个则出现报错,反之成功
将上面的byName给成byType就可以了

Spring与web容器的整合原理
why:建模的过程十分耗时的
how:
解决问题:
1.建模比不可少
2.建模要保障只执行一次
3.建模后期望在每一个servlet都能够拿到Spring的上下文对象ClassPathXmlApplicationContext
how:
1.监听器(里面初始化只执行一次)
2.Spring的上下文要存放在Tomcat上下文中
- package com.mgy.biz.listener;
-
- import javax.servlet.ServletContext;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
-
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import com.mgy.web.UserAction;
-
- /**
- * 1.实现监听器
- * 2.初始化方法contextInitialized
- * 3.拿到Tomcat上下文
- * 2.将Spring上下文保存到Tomcat上下文中
- *
- * @author Administrator
- *
- */
- public class SpringLoadListener implements ServletContextListener{
-
- @Override
- public void contextInitialized(ServletContextEvent sce) {
- System.out.println("初始化");
- ServletContext servletContext = sce.getServletContext();
- //自定义配置文件
- String aa = servletContext.getInitParameter("applicationContext.xml");
- System.out.println(aa+"///拿到了自定义配置文件");
- //拿到Spring上下文(包含所有javabean)
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
- //将Spring上下文保存到Tomcat上下文中
- //applicationContext.xml
- servletContext.setAttribute("springContext", context);
-
- }
-
- }
-
然后我们配置到web服务器上,顺便把定义项目名也配置了
- <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.xlb.biz.ioc.listener.SpringLoadListenerlistener-class>
- listener>
-
- web-app>
-
然后我们写一个实现类web层DemoServlet.java
- package com.mgy.demo;
-
- import java.io.IOException;
-
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
-
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import com.mgy.web.UserAction;
-
- /**
- * Spring与web容器的整合原理
- * why:建模的过程十分耗时的
- * how:
- * 解决问题:
- * 1.建模比不可少
- * 2.建模要保障只执行一次
- * 3.建模后期望在每一个servlet都能够拿到Spring的上下文对象ClassPathXmlApplicationContext
- *
- * how:
- * 1.监听器(里面初始化只执行一次)
- * 2.Spring的上下文要存放在Tomcat上下文中
- *
- * @author Administrator
- *
- */
- @WebServlet("/springDemo")
- public class DemoServlet extends HttpServlet{
-
- @Override
- public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
- //建模
- //1、spring-context.xml模型对象
- //ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
- ClassPathXmlApplicationContext context = (ClassPathXmlApplicationContext)req.getServletContext().getAttribute("springContext");
- //拿到UserAction
- UserAction userAction = (UserAction)context.getBean("userAction");
- //调用方法
- userAction.list();
- }
-
- }
-