欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring(ioc)

程序员文章站 2022-07-12 13:54:28
...

什么是spring,它能够做什么?

Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。
Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
目的:解决企业应用开发的复杂性
功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
范围:任何Java应用
简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
中间层框架、万能胶
struts2
spring
hibernate
容器框架
JavaBean 项目中的一个个类
IOC和AOP

什么是控制反转(或依赖注入)
控制反转(IoC=Inversion of Control)IoC,用白话来讲,就是由容器控制程序之间的(依赖)关系,而非传统实现中,由程序代码直接操控。这也就是所谓“控制反转”的概念所在:(依赖)控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转。
IoC还有一个另外的名字:“依赖注入 (DI=Dependency Injection)” ,即由容器动态的将某种依赖关系注入到组件之中
案例:实现Spring的IoC
IOC/DI
将以前由程序员实例化对象/赋值的工作交给了spring处理

核心依赖包

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>4.3.10.RELEASE</version>
</dependency>

set注入

UserAction.java

package com.qukang.ioc.web;

import java.util.ArrayList;
import java.util.List;

import com.qukang.ioc.biz.UserBiz;
import com.qukang.ioc.biz.UserBizImpl1;
/**
 * ioc的注入方式及各类类型
 * set注入
 * 		基本类型String
 * 		数组
 * 		自定义类型
 * 构造注入
 * 
 * 自动装配
 * @author VULCAN
 *
 */
public class UserAction {
	private UserBiz userBiz;
	private String uname;
	private int age;
	private List<String> hobby;
	
	public String getUname() {
		return uname;
	}


	public void setUname(String uname) {
		this.uname = uname;
	}


	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 UserBiz getUserBiz() {
		return userBiz;
	}


	public void setUserBiz(UserBiz userBiz) {
		this.userBiz = userBiz;
	}


	public void upload() {
		userBiz.upload();
	}
	
	public void test1() {
		System.out.println(this.uname);
		System.out.println(this.age);
		System.out.println(this.hobby);
	}
}

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:context="http://www.springframework.org/schema/context"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	<bean class="com.qukang.ioc.biz.UserBizImpl2" id="userBiz"></bean>
	<bean class="com.qukang.ioc.web.UserAction" id="xxx">
	<!-- set注入用property标签 -->
		<property name="userBiz" ref="userBiz"></property>
		<property name="uname" value="zs"></property>
		<property name="age" value="22"></property>
		<property name="hobby">
			<list>
			<value>唱</value>
			<value>跳</value>
			<value>RAP</value>
			<value>篮球</value>
			</list>
		</property>
	</bean>
	<bean class="com.qukang.ioc.web.OrderAction" id="ttt">
		<property name="userBiz" ref="userBiz"></property>
	</bean>
</beans>

IoTest测试类

package com.qukang.ioc.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.qukang.ioc.web.OrderAction;
import com.qukang.ioc.web.UserAction;

public class IocTest {
	public static void main(String[] args) {
//		UserAction userAction=new UserAction();
//		userAction.upload();
		
		ApplicationContext springContext=new ClassPathXmlApplicationContext("/spring-context.xml");
		UserAction userAction =(UserAction)springContext.getBean("xxx");
//		OrderAction orderAction =(OrderAction)springContext.getBean("ttt");
//		userAction.upload();
//		orderAction.upload();
		userAction.test1();
	}
}

spring(ioc)

构造器注入

UserAction.java

package com.qukang.ioc.web;

import java.util.ArrayList;
import java.util.List;

import com.qukang.ioc.biz.UserBiz;
import com.qukang.ioc.biz.UserBizImpl1;
/**
 * ioc的注入方式及各类类型
 * set注入
 * 		基本类型String
 * 		数组
 * 		自定义类型
 * 构造注入
 * 
 * 自动装配
 * 		spring4之后出现的
 * 		byType:根据配置的Bean中的接口,在spring的上下文寻找对应的实现类
 * 		byName:根据配置的Bean中的接口名字,在spring的上下文寻找对应的实现类
 * @author VULCAN
 *
 */
public class UserAction {
	private UserBiz userBiz;
	private String uname;
	private int age;
	private List<String> hobby;


	public UserAction(String uname, int age) {
		this.uname = uname;
		this.age = age;
	}
	public UserAction() {}

	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;
	}


	public void upload() {
		userBiz.upload();
	}
	
	public void test1() {
		System.out.println(this.uname);
		System.out.println(this.age);
		System.out.println(this.hobby);
	}
}

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:context="http://www.springframework.org/schema/context"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	<bean class="com.qukang.ioc.biz.UserBizImpl2" id="userBiz"></bean>
	<bean class="com.qukang.ioc.web.UserAction" id="xxx">
	<!-- set注入用property标签 -->
    <property name="userBiz" ref="userBiz"></property> 
		<property name="hobby">
			<list>
			<value>唱</value>
			<value>跳</value>
			<value>RAP</value>
			<value>篮球</value>
			</list>
		</property>
		<constructor-arg name="uname" value="ls"></constructor-arg>
		<constructor-arg name="age" value="22"></constructor-arg>
	</bean>
	<bean class="com.qukang.ioc.web.OrderAction" id="ttt">
<!-- 		<property name="userBiz" ref="userBiz"></property> -->
	</bean>
</beans>

IocTest.java

package com.qukang.ioc.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.qukang.ioc.web.OrderAction;
import com.qukang.ioc.web.UserAction;

public class IocTest {
	public static void main(String[] args) {		
		ApplicationContext springContext=new ClassPathXmlApplicationContext("/spring-context.xml");
		UserAction userAction =(UserAction)springContext.getBean("xxx");
		userAction.test1();
	}
}

spring(ioc)

自动装配注入

spring4之后出现的

  •  byType:根据配置的Bean中的接口,在spring的上下文寻找对应的实现类
    
  •  byName:根据配置的Bean中的接口名字,在spring的上下文寻找对应的实现类
    

UserAction.java

package com.qukang.ioc.web;

import java.util.ArrayList;
import java.util.List;

import com.qukang.ioc.biz.UserBiz;
import com.qukang.ioc.biz.UserBizImpl1;
/**
 * ioc的注入方式及各类类型
 * set注入
 * 		基本类型String
 * 		数组
 * 		自定义类型
 * 构造注入
 * 
 * 自动装配
 * 		spring4之后出现的
 * 		byType:根据配置的Bean中的接口,在spring的上下文寻找对应的实现类
 * 		byName:根据配置的Bean中的接口名字,在spring的上下文寻找对应的实现类
 * @author VULCAN
 *
 */
public class UserAction {
	private UserBiz userBiz;
	private String uname;
	private int age;
	private List<String> hobby;


	public UserAction(String uname, int age) {
		this.uname = uname;
		this.age = age;
	}
	public UserAction() {}

	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;
	}


	public void upload() {
		userBiz.upload();
	}
	
	public void test1() {
		System.out.println(this.uname);
		System.out.println(this.age);
		System.out.println(this.hobby);
	}
}

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"
   default-autowire="byType"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	<bean class="com.qukang.ioc.biz.UserBizImpl2" id="userBiz"></bean>
	<bean class="com.qukang.ioc.web.UserAction" id="xxx">
	<!-- set注入用property标签 -->
		<property name="hobby">
			<list>
			<value>唱</value>
			<value>跳</value>
			<value>RAP</value>
			<value>篮球</value>
			</list>
		</property>
		<constructor-arg name="uname" value="ls"></constructor-arg>
		<constructor-arg name="age" value="22"></constructor-arg>
	</bean>
	<bean class="com.qukang.ioc.web.OrderAction" id="ttt">
	</bean>
</beans>

IocTest.java

package com.qukang.ioc.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.qukang.ioc.web.OrderAction;
import com.qukang.ioc.web.UserAction;

public class IocTest {
	public static void main(String[] args) {		
		ApplicationContext springContext=new ClassPathXmlApplicationContext("/spring-context.xml");
		UserAction userAction =(UserAction)springContext.getBean("xxx");
		userAction.test1();
	}
}

spring(ioc)

spring与tomcat整合

spring 作为管理整个工程中的所有的javaBean,那么如何在用户发送请求的时候能够访问到指定的javaBean呢?
处理方式:
在监听器中 讲spring的上下文交给tomcat上下文中进行管理
过程:浏览器–>request–>servletContext–>springContext–>任意的javaBean

监听器
SpringLoderListener.java

 package com.qukang.ioc.listener;

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;
/**
 * spring 作为管理整个工程中的所有的javaBean,那么如何在用户发送请求的时候能够访问到指定的javaBean呢?
 * 处理方式:
 * 		在监听器中 讲spring的上下文交给tomcat上下文中进行管理
 * 		浏览器-->request-->servletContext-->springContext-->任意的javaBean
 * @author VULCAN
 *
 */
@WebListener
public class SpringLoderListener implements ServletContextListener {
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("tomcat一启动就触发了");
		ServletContext ServletContext = sce.getServletContext();
		String springXml=ServletContext.getInitParameter("springXml");
		ApplicationContext springContext=null;
		if(springXml==null || "".equals(springXml)) {
			springContext=new ClassPathXmlApplicationContext("/spring-context.xml");
		}else {
			springContext=new ClassPathXmlApplicationContext(springXml);
		}
		System.out.println("spring的上下文配置文件"+springXml);
		ServletContext.setAttribute("spring_key",springContext);
	}
}

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>
<context-param>
  	<param-name>springXml</param-name>
  	<param-value>/spring-other.xml</param-value>
  </context-param>
</web-app>

web层处理
所有的user请求
UserServlet.java

package com.qukang.ioc.web;

import java.io.IOException;

import javax.jws.WebService;
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;
@WebServlet("/user")
public class UsetServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("处理用户请求");
		ApplicationContext springContext=(ApplicationContext)req.getServletContext().getAttribute("spring_key");
		UserAction userAction =(UserAction)springContext.getBean("xxx");
		userAction.upload();
	}
}

spring(ioc)