什么是spring?它能够做什么?
- Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的
- Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情
- 然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益
- 目的:解决企业应用开发的复杂性
- 功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
- 范围:任何Java应用
简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架
详细图解
如果需要用到如图的所有框架,则以spring为中心,即可联合所有的框架,所以说spring是中间层框架、万能胶
什么是控制反转(或依赖注入)?
控制反转(IoC=Inversion of Control)IoC,用白话来讲,就是由容器控制程序之间的(依赖)关系,而非传统实现中,由程序代码直接操控。这也就是所谓“控制反转”的概念所在:(依赖)控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转。
IoC还有一个另外的名字:“依赖注入 (DI=Dependency Injection)” ,即由容器动态的将某种依赖关系注入到组件之中
案例:实现Spring的IoC
IOC/DI
将以前由程序员实例化对象/赋值的工作交给了spring处理
spring的注入方式
1、set注入
基本数据类型
引用数据类型
2、 构造注入
3、自动装配
首先来用代码证实set注入方式(基本数据类型)
UserAction
package com.Tang.ioc.web;
import java.util.ArrayList;
import java.util.List;
import com.Tang.ioc.biz.UserBiz;
import com.Tang.ioc.biz.impl.UserBizImpl;
/**
* spring 将维护代码的权力转交给spring容器来完成
*
* 1.spring 管理Bean的方式(注入方式)
* set注入
* 基本数据类型
* 引用数据类型
* 构造注入
* 自动装配
*
* @author Lzdwzqad
*
*/
public class UserAction {
private UserBiz userBiz=new UserBizImpl();
private int uid;
private String uname;
private List<String> hobby=new ArrayList<>();
public UserBiz getUserBiz() {
return userBiz;
}
public void setUserBiz(UserBiz userBiz) {
this.userBiz = userBiz;
}
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 List<String> getHobby() {
return hobby;
}
public void setHobby(List<String> hobby) {
this.hobby = hobby;
}
/**
* 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();
}
}
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">
<bean id="userAction" class="com.Tang.ioc.web.UserAction">
<property name="uid" value=" 88"></property>
<property name="uname" value="cxk"></property>
<property name="hobby">
<list>
<value>噢特曼</value>
<value>蜘蛛侠</value>
</list>
</property>
</bean>
</beans>
Demo1(测试)
package com.Tang.ioc.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.Tang.ioc.web.UserAction;
/**
* 模拟浏览器发请求后台
* @author Lzdwzqad
*
*/
public class Demo1 {
public static void main(String[] args) {
/*UserAction userAction =new UserAction();
userAction.test1();*/
ApplicationContext context= new ClassPathXmlApplicationContext("/spring-context.xml");
UserAction bean = (UserAction) context.getBean("userAction");
bean.test1();
}
}
效果:
set注入方法(引用类型)
修改一点点UserAction
private UserBiz userBiz;
public UserBiz getUserBiz() {
return userBiz;
}
public void setUserBiz(UserBiz userBiz) {
this.userBiz = userBiz;
}
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">
<bean id="userBiz" class="com.Tang.ioc.biz.impl.UserBizImpl"></bean>
<bean id="userAction" class="com.Tang.ioc.web.UserAction">
<property name="userBiz" ref="userBiz"></property>
<!-- <property name="uid" value="88"></property>
<property name="uname" value="cxk"></property> -->
<constructor-arg name="uid" value="88"></constructor-arg>
<constructor-arg name="uname" value="cxk"></constructor-arg>
<property name="hobby">
<list>
<value>噢特曼</value>
<value>蜘蛛侠</value>
</list>
</property>
</bean>
</beans>
UserAction(构造注入)
package com.Tang.ioc.web;
import java.util.ArrayList;
import java.util.List;
import com.Tang.ioc.biz.UserBiz;
import com.Tang.ioc.biz.impl.UserBizImpl;
/**
* spring 将维护代码的权力转交给spring容器来完成
*
* 1.spring 管理Bean的方式(注入方式)
* set注入
* 基本数据类型
* 引用数据类型
* 构造注入
* 自动装配
*
* @author Lzdwzqad
*
*/
public class UserAction {
private UserBiz userBiz=new UserBizImpl();
private int uid;
private String uname;
private List<String> hobby=new ArrayList<String>();
/*public UserBiz getUserBiz() {
return userBiz;
}
public void setUserBiz(UserBiz userBiz) {
this.userBiz = userBiz;
}
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 List<String> getHobby() {
return hobby;
}
public UserAction() {
super();
}
public UserAction(int uid, String uname) {
super();
this.uid = uid;
this.uname = uname;
}
public void setHobby(List<String> hobby) {
this.hobby = hobby;
}
/**
* 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();
}
}
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">
<bean id="userAction" class="com.Tang.ioc.web.UserAction">
<!-- <property name="uid" value="88"></property>
<property name="uname" value="cxk"></property> -->
<constructor-arg name="uid" value="88"></constructor-arg>
<constructor-arg name="uname" value="cxk"></constructor-arg>
<property name="hobby">
<list>
<value>噢特曼</value>
<value>蜘蛛侠</value>
</list>
</property>
</bean>
</beans>
自动装置
有两种方式:
1、在spring-context.xml里加上
default-autowire="byType"
或者
2、在spring-context.xml里加上
default-autowire="byName"
因为是根据类别来进行查找,所以我们用第二种方式
spring的上下文交给tomcat管理
首先spring上下文为什么tomact?
分析: 目前工程中的所有javabean都交给了spring进行管理,那么浏览器发送请求,请求的是tomcat,
由tomcat来处理请求,tomcat处理请求一般来说都要访问数据库,数据库是由Dao层访问的,
Dao层的实体类又是spring的上下文管理,那就意味着,tomcat要处理请求,必须拿到spring的上下文,才能拿到Dao层的javabean
SpringLoadListener
package com.Tang.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;
/**
* @author Lzdwzqad
*
*/
@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);
}
}
web.xml配置(给配置文件给更改名字)
<context-param>
<param-name>springXMLLocation</param-name>
<param-value>/spring-context.xml</param-value>
</context-param>
UserServlet
package com.Tang.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.Tang.ioc.web.UserAction;
@WebServlet("/user")
public class UserServlet extends HttpServlet{
@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 {
// TODO Auto-generated method stub
ServletContext servletContext = req.getServletContext();
ApplicationContext springContext = (ApplicationContext) servletContext.getAttribute("spring_context_key");
UserAction bean = (UserAction) springContext.getBean("userAction");
bean.test3();
}
}
效果: