一 :什么是spring?
Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。
Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
目的:解决企业应用开发的复杂性
功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
范围:任何Java应用
简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
2.中间层框架、万能胶
struts2
spring
hibernate
3.容器框架
JavaBean 项目中的一个个类
IOC和AOP
4.框架与框架之间需要整合:
5. spring包含的核心模块:
二: 什么是控制反转(依赖注入)
控制反转(IoC=Inversion of Control)IoC,用白话来讲,就是由容器控制程序之间的(依赖)关系,而非传统实现中,由程序代码直接操控。这也就是所谓“控制反转”的概念所在:(依赖)控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转。
IoC还有一个另外的名字:“依赖注入 (DI=Dependency Injection)” ,即由容器动态的将某种依赖关系注入到组件之中
案例:
1、原始上传功能:接口
package com.xly.ioc.biz;
public interface UserBiz {
public void upload();
}
2、接口
package com.xly.ioc.biz.impl;
import com.xly.ioc.biz.UserBiz;
/**
* 功能:上传功能
* 如更改功能:1.限定上传文件大小
* 2.限定上传文件类别 (就要更改原本代码)
* 总结:1。更新版本需要改动原有代码
* 2.相关调用此方法的模块伴随着巨大的风险
* @author zjjt
*
*/
public class UserBizImpl implements UserBiz{
@Override
public void upload() {
System.out.println("循规蹈矩的开发出来");
}
}
3、UserAction
package com.xly.ioc.web;
import com.xly.ioc.biz.UserBiz;
import com.xly.ioc.biz.impl.UserBizImpl;
public class UserAction {
private UserBiz userBiz=new UserBizImpl();
public void upload() {
userBiz.upload();
}
public static void main(String[] args) {
UserAction userAction =new UserAction();
userAction.upload();
}
}
结果
三:spring的使用
1、导入jar包
<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.0</modelVersion>
<groupId>com.xly</groupId>
<artifactId>maven_4</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>maven_4 Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>5.0.1.RELEASE</spring.version>
<javax.servlet.version>4.0.0</javax.servlet.version>
<junit.version>4.12</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- 2、导入spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 5.1、junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- 5.2、servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>maven_4</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、核心配置文件(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"
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>
3、进行xml配置
package com.xly.ioc.web;
import com.xly.ioc.biz.UserBiz;
import com.xly.ioc.biz.impl.UserBizImpl2;
public class PersonAction {
private UserBiz userBiz;
public UserBiz getUserBiz() {
return userBiz;
}
public void setUserBiz(UserBiz userBiz) {
this.userBiz = userBiz;
}
public void upload() {
userBiz.upload();
}
}
4、由配置文件负责配置
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!--本文件中配置整个项目中包含的所有javabean 目的在于spring统一管理-->
<bean name="userBiz" class="com.zy.ioc.biz.impl.UserBizImpl"></bean>
<bean name="userBiz2" class="com.zy.ioc.biz.impl.UserBizImpl2"></bean>
<bean name="personAction" class="com.zy.ioc.web.PersonAction">
<property name="userBiz" ref="userBiz1"></property>
</bean>
<bean name="userAction" class="com.xly.ioc.web.UserAction"></bean>
</beans>
运行结果
这样我们就可以不用更改源代码了
三:spring三种传参
一、set传参
package com.xly.ioc.web;
import java.util.List;
public class ParamAction {
private int age;
private String name;
private List<String> hobby;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getHobby() {
return hobby;
}
public void setHobby(List<String> hobby) {
this.hobby = hobby;
}
public void execute() {
System.out.println(this.age);
System.out.println(this.name);
System.out.println(this.hobby);
}
}
2、配置
<property name="name" value="影子1"></property>
<property name="age" value="18"></property>
<property name="hobby" >
<list>
<value>打羽毛球</value>
<value>追剧</value>
</list>
</property>
3、测试
package com.xly.ioc.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xly.ioc.web.ParamAction;
import com.xly.ioc.web.PersonAction;
public class IocTest {
public static void main(String[] args) {
//建模
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("/spring-context.xml");
ParamAction paramAction= (ParamAction)applicationContext.getBean("paramAction");
paramAction.execute();
}
}
运行结果
二、构造传参
package com.xly.ioc.web;
import java.util.List;
public class ParamAction {
private int age;
private String name;
private List<String> hobby;
public ParamAction(int age, String name, List<String> hobby) {
super();
this.age = age;
this.name = name;
this.hobby = hobby;
}
public ParamAction() {
// TODO Auto-generated constructor stub
}
public void execute() {
System.out.println(this.age);
System.out.println(this.name);
System.out.println(this.hobby);
}
}
2、配置
<constructor-arg name="name" value="影子2"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
<constructor-arg name="hobby" >
<list>
<value>打羽毛球</value>
<value>追剧</value>
</list>
</constructor-arg>
运行结果
四:spring与tomcat的整合
假如spring-context.xml中配置了几百个类,测试建模的时间过程非常久;
javabean每建一次模,时间就越久,性能就越差,所以将建模放到监听器中
1.监听类
package com.xly.ioc.listenner;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionListener;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xly.ioc.web.ParamAction;
public class SpringLoaderListener implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("监听器方法执行.....");
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("/spring-context.xml");
sce.getServletContext().setAttribute("SpringContext", applicationContext);
}
}
2.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 Application</display-name>
<listener>
<listener-class>com.xly.ioc.listenner.SpringLoaderListener</listener-class>
</listener>
</web-app>
3.测试
package com.xly.ioc.web;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@WebServlet("/user")
public class UserServlet extends HttpServlet{
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
ApplicationContext springContext= (ApplicationContext) req.getServletContext().getAttribute("SpringContext");
ParamAction paramAction = (ParamAction) springContext.getBean("paramAction");
paramAction.execute();
}
}
运行结果
注意:spring监听器只会执行一次