什么是spring?
Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。
Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
目的:解决企业应用开发的复杂性
功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
范围:任何Java应用
简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
1 中间层框架、万能胶
2 容器框架
什么是控制反转(或依赖注入)
控制反转(IoC=Inversion of Control)IoC,用白话来讲,就是由容器控制程序之间的(依赖)关系,而非传统实现中,由程序代码直接操控。这也就是所谓“控制反转”的概念所在:(依赖)控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转。
IoC还有一个另外的名字:“依赖注入 (DI=Dependency Injection)” ,即由容器动态的将某种依赖关系注入到组件之中
案例:实现Spring的IoC
IOC/DI
将以前由程序员实例化对象/赋值的工作交给了spring处理
如何在spring当中定义和配置一个JavaBean(使用无参构造方法+set方法创建一个JavaBean)
3.1 id:在容器中查找Bean的id(唯一、且不能以/开头)
3.2 class:bean的完整类名
3.3 name:在容器中查找Bean的名字(唯一、允许以/开头、允许多个值,多个值之间用逗号或空格隔开)
3.4 scope:(singleton|prototype)默认是singleton
3.4.1 singleton(单例模式):在每个Spring IoC容器中一个bean定义对应一个对象实例
3.4.2 prototype(原型模式/多例模式):一个bean定义对应多个对象实例
3.4 abstract:将一个bean定义成抽象bean(抽象bean是不能实例化的),抽象类一定要定义成抽象bean,非抽象类也可以定义成抽象bean
3.5 parent:指定一个父bean(必须要有继承关系才行)
3.6 init-method:指定bean的初始化方法
3.7 constructor-arg:使用有参数构造方法创建javaBean
注1:struts2的Action请使用多例模式
简单属性的配置:
8基础数据+String+3个sql
复杂属性的配置
1 JavaBean
ref bean=""
2 List或数组
3 Map
4 Properties
针对项目,配置文件路径的2种写法
ApplicationContext
String path = “applicationContext.xml”;
String path = “classpath:applicationContext-*.xml”;//src
String[] path = new String[] { “applicationContext-a.xml”, “applicationContext-b.xml” };//分模块开发
spring与web项目的集成
WEB项目如何读取spring上下文
通过监听器实现ServletContextListener
contextConfigLocation:classpath:applicationContext-*.xml
注:创建spring的XML文件时,需要添加beans/aop/tx/context标签支持
ioc
set注入
基本数据类型注入
集合注入
对象注入
构造注入
基本数据类型注入
自动装配
列子
写一个接口,然后写两个类实现接口
UserBiz.java
package com.tt.ioc.biz;
/**
* 需求:
* 上传文件需求
* 完成的思路:完成功能就好 文件上传就行了
* 客户使用的时候:嫌弃上传的时间过长 ,要求优化
* 由于某程序员的失误,改动的代码影响了其他模块的正常使用
* 客户:两头利害取其轻,请愿回退到上一个版本
* @author tt
*
*/
public interface UserBiz {
public void read();
}
UserBizImpl1.java
package com.tt.ioc.biz.impl;
import com.tt.ioc.biz.UserBiz;
public class UserBizImpl1 implements UserBiz{
@Override
public void read() {
System.out.println("边学习边记笔记");
}
}
UserBizImpl2.java
package com.tt.ioc.biz.impl;
import com.tt.ioc.biz.UserBiz;
public class UserBizImpl2 implements UserBiz{
@Override
public void read() {
System.out.println("死记硬背");
}
}
实体类属性和方法UserAction.java
package com.tt.ioc.web;
import java.util.ArrayList;
import java.util.List;
import com.tt.ioc.biz.UserBiz;
import com.tt.ioc.biz.impl.UserBizImpl1;
import com.tt.ioc.biz.impl.UserBizImpl2;
/**
* 弊端:
* 当需求变化非常快的时候,不便于维护,因为维护的权利是属于程序员的
*
* spring的ioc就是解决这一问题的
* 将维护代码的权利由程序员转交给spring容器来完成
*
* 1、spring管理Bean的方式(注入方式)
* set注入
* 基本数据类型
* 引用数据类型
* 构造注入
* 自动装配
* @author tt
*
*/
public class UserAction {
private UserBiz userBiz;
private int uid;
private String uname;
private List<String> hobby = new ArrayList<String>();
// public int getUid() {
// return uid;
// }
// public void setUid(int uid) {
// this.uid = uid;
// }
// public String getUname() {
// return uname;
// }
// public void setUname(String uname) {
// this.uname = uname;
// }
public UserAction(int uid, String uname) {
super();
this.uid = uid;
this.uname = uname;
}
public UserAction() {
super();
}
public List<String> getHobby() {
return hobby;
}
public void setHobby(List<String> hobby) {
this.hobby = hobby;
}
public UserBiz getUserBiz() {
return userBiz;
}
public void setUserBiz(UserBiz userBiz) {
this.userBiz = userBiz;
}
/**
* set注入
*/
public void test1() {
System.out.println("uid:"+this.uid);
System.out.println("uname:"+this.uname);
System.out.println("hobby:"+this.hobby);
}
/**
* 构造注入
*/
public void test2() {
}
/**
* 测试注入引用数据类型
*/
public void test3() {
userBiz.read();
}
}
测试Demo1.java
package com.tt.ioc.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tt.ioc.web.UserAction;
/**
* 模拟浏览器发请求后台
* @author tt
*/
public class Demo1 {
public static void main(String[] args) {
// UserAction userAction = new UserAction();
// userAction.test3();
// userAction.test1();
ApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
UserAction bean = (UserAction) context.getBean("userAction");
bean.test1();
bean.test3();
}
}
配置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"
default-autowire="byType"
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">
<!-- 一个接口实现两次会报错 default-autowire="byType"
default-autowire="byName"时不报错,会打印第一个,根据userBiz2找
-->
<!-- <bean id="userBiz2" class="com.tt.ioc.biz.impl.UserBizImpl1"></bean> -->
<bean id="userBiz" class="com.tt.ioc.biz.impl.UserBizImpl2"></bean>
<bean id="userAction" class="com.tt.ioc.web.UserAction">
<!-- <property name="uid" value="22"></property>
<property name="uname" value="zs"></property> -->
<property name="userBiz" ref="userBiz"></property>
<constructor-arg name="uid" value="22"></constructor-arg>
<constructor-arg name="uname" value="zs"></constructor-arg>
<property name="hobby">
<list>
<value>篮球</value>
<value>RAP</value>
<value>津港</value>
</list>
</property>
</bean>
</beans>
这里一个接口实现两次会报错 ,解决方法是default-autowire="byType"设置方式
SpringLoadListener.java
package com.tt.ioc.test;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 2、 如何将spring的上下文交给tomcat上下文进行管理
* 首先spring上下文为什么tomcat?
* 分析:目前工程中的所有javabean都交给了spring进行管理,那么浏览器发送请求,请求的是tomcat,
* 由tomcat来处理请求,tomcat处理请求一般说都要访问数据库,数据库是由Dao层访问的,Dao层的实体类
* 又是交给了spring的上下文管理,你就意味着,tomcat要处理请求,必须拿到spring的上下文,才能够拿到
* Dao层的javabean
* @author tt
*
*/
@WebListener
public class SpringLoadListener implements ServletContextListener{
private String springXmlLocation = "";
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("监听了tomcat的启动......");
ServletContext servletContext = sce.getServletContext();
springXmlLocation = servletContext.getInitParameter("springXmlLocation");
if(null == springXmlLocation || "".equals(springXmlLocation)) {
springXmlLocation = "/spring-context.xml";
}
System.out.println("springXmlLocation:"+springXmlLocation);
ApplicationContext springcontext = new ClassPathXmlApplicationContext(springXmlLocation);
servletContext.setAttribute("SPRING_CONTEXT_KEY", springcontext);
}
}
//@WebListener监听 一定要写,不要漏了
UserServlet.java
package com.tt.ioc.test;
import java.io.IOException;
import javax.servlet.ServletContext;
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.ApplicationContext;
import com.tt.ioc.web.UserAction;
@WebServlet("/user")
public class UserServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = req.getServletContext();
ApplicationContext springcontext = (ApplicationContext) servletContext.getAttribute("SPRING_CONTEXT_KEY");
UserAction bean = (UserAction) springcontext.getBean("userAction");
bean.test3();
}
}
可以在web里面部署配置文件web.xml(spring-other.xml和spring-context.xml是一样的内容)
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>springXmlLocation</param-name>
<param-value>/spring-other.xml</param-value>
</context-param>
</web-app>
tomcat整合ioc容器
org.springframework spring-context 4.3.10.RELEASE1、spring tool suite官方下载地址:http://spring.io/tools/sts/all
2、很详细的网文在线安装介绍:http://www.cnblogs.com/liuyungao/p/6213997