目录
一,Spring是什么?
Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
目的:解决企业应用开发的复杂性
功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
范围:任何Java应用
简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
二、Spring中ioc的特点及依赖注入的三种方式
1、控制反转
控制反转(IoC=Inversion of Control)IoC,用白话来讲,就是由容器控制程序之间的(依赖)关系,而非传统实现中,由程序代码直接操控。这也就是所谓“控制反转”的概念所在:(依赖)控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转。
IoC还有一个另外的名字:“依赖注入 (DI=Dependency Injection)” ,即由容器动态的将某种依赖关系注入到组件之中
案例:实现Spring的IoC
IOC/DI
将以前由程序员实例化对象/赋值的工作交给了spring处理
2、依赖注入的三种方式
set 注入
<!-- set注入 -->
<bean class="com.zking.web.UserAction" id="userAction">
<property name="userBiz" ref="userBiz"></property>
<property name="age" value="19"></property>
<property name="name" value="bobo"></property>
<property name="hobby">
<list>
<value>电竞</value>
<value>boy</value>
<value>girl</value>
</list>
</property>
</bean>
构造注入
<!-- 构造注入 -->
<bean class="com.zking.web.OrderAction" id="orderAction">
<property name="userBiz" ref="userBiz"></property>
<constructor-arg name="name" value="boob"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
<constructor-arg name="hobby">
<list>
<value>电竞</value>
<value>boy</value>
<value>girl</value>
</list>
</constructor-arg>
</bean>
三,spring中ioc的特点
建一个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.0</modelVersion>
<groupId>com.cdl</groupId>
<artifactId>T280_spring</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>T280_spring Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- 将当前项目所用的jar包依赖版本定义在外部目的在于所有jar
包版本进行统一管理-->
<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>T280_spring</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>
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 Application</display-name>
</web-app>
四:后台代码
userbiz:
package com.zlp.biz;
/**
*
* @author zlp
*
* 名称:用户业务类
* 需求:
* 同时在用户模块,订单模块所拿到所有的用户数据
*
* 需求变更1:
* 同时在用户模块,订单模块拿到所有的用户数据,并且要求用户数据是已经通过年龄排序的
* 对应策略:修改userBiz中list方法,添加排序功能
*
* 需求变更2:
* 同时在用户模块,订单模块拿到所有的用户数据,并且要求用户数据是已经通过时间点排序的
* 对应策略:修改userBiz中list方法,添加排序功能,按照时间点排序
*
*
* 总结:
* 最原始:频繁修改业务层biz层代码
* 多实现:凡是涉及到用户业务层调用的地方,都需要修改代码
*
*/
public interface UserBiz {
void list();
}
UserBizImpl1/2:
//UserBizImpl1:
package com.zlp.impl;
import com.zlp.biz.UserBiz;
public class UserBizImpl1 implements UserBiz{
@Override
public void list() {
System.out.println("查询用户数据,按照入职时间排序。。。。。");
}
}
//UserBizImpl2 :
package com.zlp.impl;
import com.zlp.biz.UserBiz;
public class UserBizImpl2 implements UserBiz{
@Override
public void list() {
System.out.println("查询用户数据,按照入职年龄排序。。。。。");
}
}
Demo1:
package com.zlp.ioc.demo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zlp.web.OrderAction;
import com.zlp.web.UserAction;
/**
* 1.对sqpring框架的配置文件进行建模,建模之后 spring-context.xml中所有的javabean信息都
* 都会加载进Spring 容器的上下文
* 2.上下文中就包含了spring-context.xml 所有对象
*
* IOC的特点-什么叫控制反转
* 指的是将创建对象的权利反转个给spring容器来完成
*
* @author zlp
*/
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();
}
}
DemoServlet:
package com.zlp.ioc.demo;
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.zlp.web.UserAction;
/**
* spring与web容器的整合原理
* why:建模的过程是十分耗时的
* 解决问题:
* 1.建模必不可少
* 2.建模要保障只执行一次
* 3.建模后期望在每一次servlet都能够拿到Spring的上下文对象ClassPathXmlApplicationContext
*
* how:
* 1.监听器的初始化方法
* 2.Spring的上下要存放在tomcat上下文中
*
* @author zlp
*/
@WebServlet("/springDemo")
public class DemoServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//Thread.sleep(10000);
//ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
ClassPathXmlApplicationContext context = (ClassPathXmlApplicationContext) req.getServletContext().getAttribute("servletContext");
UserAction useraction =(UserAction) context.getBean("userAction");
useraction.list();
}
}
SpringLoadListener
package com.zlp.ioc.listener;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zlp.web.UserAction;
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("servletContext", servletContext);
}
}
OrderAction
package com.zlp.web;
import java.util.List;
import com.zlp.biz.UserBiz;
import com.zlp.impl.UserBizImpl1;
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<String> hobby;
public OrderAction() {
}
public OrderAction(String name, int age, List<String> hobby) {
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();
}
}
UserAction
package com.zlp.web;
import java.util.List;
import com.zlp.biz.UserBiz;
/**
* 依赖注入的方式:
* 1.set注入
* 2.构造注入
* 3.自动装配
* default-autowire="byType"
* byName:是spring管理的bean对象的id进行查找,如果找不到则注入失败;反之成功
* byType:是Spring管理bean对象接口实现类进行查找;如果没有或2个以上,则注入失败,反之成功
* @author zlp
*/
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<String> 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<String> getHobby() {
return hobby;
}
public void setHobby(List<String> hobby) {
this.hobby = hobby;
}
public void list() {
System.out.println(name);
System.out.println(age);
System.out.println(hobby);
userbiz.list();
}
}
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">
<!-- IOC的主要作用管理整个项目的javabean,依靠依赖注入,控制反转的特点进行管理 -->
<!-- <bean class="com.zlp.impl.UserBizImpl1" id="userBiz1"></bean>-->
<bean class="com.zlp.impl.UserBizImpl1" id="userBiz"></bean>
<!-- <bean class="com.zlp.impl.UserBizImpl1" id="userbiz1"></bean> -->
<!-- set注入-->
<bean class="com.zlp.web.UserAction" id="userAction">
<property name="userbiz" ref="userBiz"></property>
<property name="name" value="bobo"></property>
<property name="age" value="19"></property>
<property name="hobby">
<list>
<value>电竞</value>
<value>boy</value>
<value>girl</value>
</list>
</property>
</bean>
<!-- 构造注入 -->
<bean class="com.zlp.web.OrderAction" id="orderAction">
<property name="userbiz" ref="userBiz"></property>
<constructor-arg name="name" value="lele"></constructor-arg>
<constructor-arg name="age" value="112"></constructor-arg>
<constructor-arg name="hobby">
<list>
<value>电竞</value>
<value>boy</value>
<value>girl</value>
</list>
</constructor-arg>
</bean>
</beans>
今天的分享就到啦~我们下次再见~~~~