Spring框架之IOC介绍讲解

一、对spring的理解

spring是一个开源框架,它由Rod Johnson 创建。它是为了解决企业应用开发的复杂性而创建的。

目的:解决企业应用开发的复杂性

功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能

范围:任何Java应用

简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

二、spring中ioc的特点

建一个maven项目

pom.xml


  4.0.0
  com.yzp
  T280_spring
  war
  0.0.1-SNAPSHOT
  T280_spring Maven Webapp
  http://maven.apache.org
  
  
		5.0.1.RELEASE
		4.0.0
		4.12
  
  
   
			junit
			junit
			3.8.1
			test
		
		
		
			org.springframework
			spring-context
			${spring.version}
		
		
			org.springframework
			spring-aspects
			${spring.version}
		
		
		
			junit
			junit
			${junit.version}
			test
		
		
		
			javax.servlet
			javax.servlet-api
			${javax.servlet.version}
			provided
		
  
  
    T280_spring
    
			
				org.apache.maven.plugins
				maven-compiler-plugin
				3.7.0
				
					1.8
					1.8
					UTF-8
				
			
		
  

web.xml


  Archetype Created Web Application

2.1控制反转

建一个com.yzp.biz的包

UserBiz

package com.yzp.biz;
/**
 * 用户业务类
 * @author yzp
 *
 */
public interface UserBiz {
	void list();
}

建一个com.yzp.biz.impl的包

UserBizImpl1

package com.yzp.biz.impl;
import com.yzp.biz.UserBiz;
public class UserBizImpl1 implements UserBiz{
	@Override
	public void list() {
		System.out.println("查询用户数据。。按照年龄排序。");
	}
}

UserBizImpl2

package com.yzp.biz.impl;
import com.yzp.biz.UserBiz;
public class UserBizImpl2 implements UserBiz{
	@Override
	public void list() {
		System.out.println("查询用户数据。。按照入职时间排序。");
	}
}

建一个com.yzp.web的包

OrderAction

package com.yzp.web;
import com.yzp.biz.UserBiz;
import com.yzp.biz.impl.UserBizImpl1;
public class OrderAction {
private UserBiz userBiz = new UserBizImpl1();
	public void list() {
		userBiz.list();
	}
}

UserAction

package com.yzp.web;
import com.yzp.biz.UserBiz;
import com.yzp.biz.impl.UserBizImpl1;
public class UserAction {
	private UserBiz userBiz = new UserBizImpl1();
	public void list() {
		userBiz.list();
	}
}

常规做法即运用spring之前的处理方法

需求:
同时在用户模块,订单模块拿到所有的用户数据
需求变更1:
同时在用户模块,订单模块拿到所有的用户数据,并且要求用户数据是通过年龄排序的
对应策略:修改UserBiz中list方法,添加排序功能
需求变更2:
同时在用户模块,订单模块拿到所有的用户数据,并且要求用户数据是通过注册时间排序的
对应策略,修改UserBiz中list方法,添加排序功能,按照时间点排序

总结:
最原始:频繁修改业务层biz的代码
多实现:凡是涉及到用户业务层调用的地方,都需要修改代码

将spring的配置文件加入

spring-context.xml


注意:一定要在有网络的情况下进行,否则约束不生效

IOC的主要作用管理整个项目的Javabean:依靠依赖注入、控制反转的特点进行管理

spring-context.xml



	
	
	
	
		
	
	

UserAction

package com.yzp.web;
import com.yzp.biz.UserBiz;
import com.yzp.biz.impl.UserBizImpl1;
public class UserAction {
	//private UserBiz userBiz = new UserBizImpl1();
	private UserBiz userBiz;
	public void list() {
		userBiz.list();
	}
	public UserBiz getUserBiz() {
		return userBiz;
	}
	public void setUserBiz(UserBiz userBiz) {
		this.userBiz = userBiz;
	}
}

建一个com.yzp.ioc的包

在里面建一个Demo1的类

建模,打印com.yzp.biz.impl.UserBizImpl2中的语句

package com.yzp.ioc;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yzp.web.UserAction;
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();
	}
}

运行结果:

Spring框架之IOC介绍讲解_第1张图片

可见 能拿到

 总结

1.对spring框架的配置文件进行建模,建模之后spring-context.xml中所有的Javabean信息
 都会加载进spring容器的上下文中
2.上下文中就包含了spring-context.xml 所有对象

测试orderaction也拿到相同结果,配置和orderaction都如useraction一致修改

package com.yzp.web;
import com.yzp.biz.UserBiz;
import com.yzp.biz.impl.UserBizImpl1;
public class OrderAction {
//private UserBiz userBiz = new UserBizImpl1();
	private UserBiz userBiz;
	public void list() {
		userBiz.list();
	}
	public UserBiz getUserBiz() {
		return userBiz;
	}
	public void setUserBiz(UserBiz userBiz) {
		this.userBiz = userBiz;
	}
}

	
	
	
	
		
	
	
		
	
package com.yzp.ioc;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yzp.web.OrderAction;
import com.yzp.web.UserAction;
/**
 * 1.对spring框架的配置文件进行建模,建模之后spring-context.xml中所有的Javabean信息
 * 都会加载进spring容器的上下文中
 * 2.上下文中就包含了spring-context.xml 所有对象
 * @author yzp
 *
 */
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();
	}
}

结果:

Spring框架之IOC介绍讲解_第2张图片

若是要按年龄排序,就只需要修改配置文件就OK了


	
	
	
		
	
	
		
	

Spring框架之IOC介绍讲解_第3张图片

2.2注入依赖

方式① set注入

UserAction

package com.yzp.web;
import java.util.List;
import com.yzp.biz.UserBiz;
import com.yzp.biz.impl.UserBizImpl1;
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 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 getHobby() {
		return hobby;
	}
	public void setHobby(List hobby) {
		this.hobby = hobby;
	}
	public void list() {
		System.out.println(name);
		System.out.println(age);
		System.out.println(hobby);
		userBiz.list();
	}
}

	
	
	
		
		
		
		
		
			
				篮球
				足球
				唱歌
			
		
	
	
		
	
package com.yzp.ioc;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yzp.web.OrderAction;
import com.yzp.web.UserAction;
/**
 * 1.对spring框架的配置文件进行建模,建模之后spring-context.xml中所有的Javabean信息
 * 都会加载进spring容器的上下文中
 * 2.上下文中就包含了spring-context.xml 所有对象
 * @author yzp
 * 
 * IOC特点(控制反转):将创建对象的权利反转给spring容器来完成
 * 
 *
 */
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();
	}
}

结果;

Spring框架之IOC介绍讲解_第4张图片

方式② 构造注入

OrderAction

package com.yzp.web;
import java.util.List;
import com.yzp.biz.UserBiz;
import com.yzp.biz.impl.UserBizImpl1;
/**
 * 依赖注入的三种方式:
 * 1.set注入
 * 2.构造注入
 * 3.自动装配
 *  byName
 *  byType
 * @author yzp
 *
 */
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 hobby;
	public OrderAction(String name, int age, List hobby) {
		super();
		this.name = name;
		this.age = age;
		this.hobby = hobby;
	}
	public OrderAction() {
		super();
		// TODO Auto-generated constructor stub
	}
	public void list() {
		System.out.println(name);
		System.out.println(age);
		System.out.println(hobby);
		userBiz.list();
	}
}

	
	
	
	
		
		
		
		
			
				篮球
				足球
				唱歌
			
		
	
	
	
		
		
		
		
				
				篮球1
				足球1
				唱歌1
				
		
	

运行结果:

Spring框架之IOC介绍讲解_第5张图片

方式③ 自动装配

将set和构造的注入 注释

byType不报错 byName报错

Spring框架之IOC介绍讲解_第6张图片

Spring框架之IOC介绍讲解_第7张图片

自动装配
 default-autowire="byName" 
 byName:是通过spring管理的bean对象的ID进行查找,如果找不到,则注入失败,反之成功
 byType:是通过spring管理的bean对象的接口实现类进行查找,如果没有或者2个以上,则注入失败,反之成功

三、spring与web容器的整合

分析:

spring与web容器的整合原理
    why:建模的过程是十分耗时的
  解决问题:
 1.建模必不可少
 2.建模只保障只执行一次
 3.建模后期望在每一个servlet都能够拿到spring的上下文对象ClassPathXmlApplicationContext
   how:
  1.监听器的初始化方法 只执行一次
  2.spring的上下文要存放在Tomcat上下文中

建一个包com.yzp.ioc.listener

package com.yzp.ioc.listener;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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("springContext", context);
	}
}

在com.yzp.ioc中建一个类DemoServlet

package com.yzp.ioc;
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.yzp.web.UserAction;
/**
 * spring与web容器的整合原理
 * 	why:建模的过程是十分耗时的
 * 解决问题:
 * 1.建模必不可少
 * 2.建模只保障只执行一次
 * 3.建模后期望在每一个servlet都能够拿到spring的上下文对象ClassPathXmlApplicationContext
 * how:
 * 1.监听器的初始化方法 只执行一次
 * 2.spring的上下文要存放在Tomcat上下文中
 * @author yzp
 * 
 */
@WebServlet("/springDemo")
public class DemoServlet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1){
		//ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
		ClassPathXmlApplicationContext context = (ClassPathXmlApplicationContext) arg0.getServletContext().getAttribute("springContext");
		UserAction userAction = (UserAction) context.getBean("userAction");
		userAction.list();
	}
}

spring-context.xml


	
	
	
	
	
		 
		
		
		
			
				篮球
				足球
				唱歌
			
		
	
	
	
		 
		
		
		
				
				篮球1
				足球1
				唱歌1
				
		
	
	

web.xml


  Archetype Created Web Application
	
		springConfigLocation
		/applicationContext.xml
	
	
		com.yzp.ioc.listener.SpringLoadListener
	

到此这篇关于Spring框架之IOC介绍讲解的文章就介绍到这了,更多相关Spring IOC内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(Spring框架之IOC介绍讲解)