Spring值IOC

哈喽铁子们今天分享ioc

先看目录

目录

一、回顾

maven 安装配置

maven的三大仓库

maven的setting.xml的配置

eclipse集成maven

eclipse创建maven

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

三、Spring中ioc的特点及依赖注入的三种方式

1、什么是控制反转(或依赖注入)

2、依赖注入的三种方式

四、Spring与web容器的整合 

1、why

2、how

3、配置xml

五,完整代码

1、UserBiz

 2.UserBizImp1

3.UserBizImpl2

4、Demo1

5、DemoServlet

6、SpringLoadListener

7、OrderAction

8、UserAction

9、spring-context.xml

10、web.xml


一、回顾

我们回顾回顾上次讲的Maven

maven 安装配置

1.先下载,在非中文目录下进行解压

2.在电脑属性、高级设置、环境变量进行配置

3.新建MAVEN HOME=bin的上一层(maven的根目录)

4.path;응MAVEN HOME%\bin;

5.mvn -version

maven的三大仓库

中央仓库:国外的包含所有jar

私有仓库:私服,公司根据自身业务,存放所有的jar依赖

本地仓库:本电脑所有开发jar包

maven的setting.xml的配置

1.设置本地仓库地址

2.设置阿里云私服地址eclipse集成maven 1.install

eclipse集成maven

1.install   指定bin上一层(maven的根目录)

2.user setting 指定maven的setting.xml的配置,下面的地址 会同步切换为 maven的setting.xml的私有仓库地址

eclipse创建maven

1.选中项目,右键build path—→修改目录结构

2.pom.xml添加jdk1.8的插件

3.web.xml中修改为3.1约束

4.选中项目,右键properties jre1.5->jdk1.8

web2.3->web3.1->设置src/main/webapp

5.maven update

好了回顾结束下面开始新的内容

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

 Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。
   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="22"></property>
		<property name="name" value="zhangsan"></property>
		<property name="hobby">
			<list>
				<value>篮球</value>
				<value>boy</value>
				<value>篮球</value>
			</list>
		</property>
	</bean>

②构造注入
  

<!-- 构造注入 -->
	<bean class="com.zking.web.OrderAction" id="orderAction">
		<property name="userBiz" ref="userBiz"></property>
		<constructor-arg name="name" value="zhangsan"></constructor-arg>
		<constructor-arg name="age" value="22"></constructor-arg>
		<constructor-arg name="hobby">
			<list>
				<value>篮球</value>
				<value>boy</value>
				<value>篮球</value>
			</list>
		</constructor-arg>
	</bean>

   ③自动装配(spring3后出现的特性)

default-autowire="byName"

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

四、Spring与web容器的整合 

1、why

建模的过程是十分耗时的
解决问题:
1.建模必不可少
2.建模要保障只执行一次
3.建模后期望在每一个servlet都能够拿到spring的上下文对象ClassPathXmlApplicationContext

2、how

1.监听器的初始化方法
2.spring的上下 要 存放 在tomcat上下文中

3、配置xml

<context-param>
      <param-name>springConfigLocation</param-name>
      <param-value>/applicationContext.xml</param-value>
  </context-param>
  <listener>
      <listener-class>com.zking.ioc.listener.SpringLoadListener</listener-class>
  </listener>

五,完整代码

1、UserBiz

用户业务类
  需求:
      同时在用户模块、订单模块拿到所有的用户数据
      
 需求变更1:
     同时在用户模块、订单模块拿到所有的用户数据,并且要求用户数据是已经通过年龄排序了的
      对应策略:修改userBiz中list方法,添加排序功能
      
 需求变更2:
     同时在用户模块、订单模块拿到所有的用户数据,并且要求用户数据是已经注册的时间点排序了的
    对应策略:修改userBiz中list方法,添加排序功能,按照时间点排序
 
 ..
      总结:
         最原始:频繁修改业务层biz层代码
        多实现:凡是涉及到用户业务层 调用的地方,都需要修改代码
 
 

package com.zking.biz;
 
 
public interface UserBiz {
	void list();
}

 2.UserBizImp1

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

3.UserBizImpl2

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

4、Demo1

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


   IOC的特点-什么叫控制反转
   指的是将创建对象的权利反转给spring容器来完成
 

package com.zking.ioc.demo;
 
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.zking.web.OrderAction;
import com.zking.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();
		
		OrderAction orderAction=(OrderAction)context.getBean("orderAction");
		orderAction.list();
	}
}

5、DemoServlet

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

6、SpringLoadListener

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

7、OrderAction

package com.zking.web;
 
import java.util.List;
 
import com.zking.biz.UserBiz;
import com.zking.biz.impl.UserBizImpl1;
import com.zking.biz.impl.UserBizImpl2;
 
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() {
		// TODO Auto-generated constructor stub
	}
	
	public OrderAction(String name, int age, List<String> hobby) {
		super();
		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();
	}
}

8、UserAction

注解:


  依赖注入的三种方式
     1.set 注入
     2.构造注入
     3.自动装配(spring3后出现的特性)

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

package com.zking.web;
 
import java.util.List;
 
import com.zking.biz.UserBiz;
import com.zking.biz.impl.UserBizImpl1;
import com.zking.biz.impl.UserBizImpl2;
 
 
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();
	}
	
	public UserAction() {
		// TODO Auto-generated constructor stub
	}
}

9、spring-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName" 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.zking.biz.impl.UserBizImpl1" id="userBiz"></bean>
	<!-- <bean class="com.zking.biz.impl.UserBizImpl2" id="userBiz"></bean> -->
	<!-- set注入 -->
	<bean class="com.zking.web.UserAction" id="userAction">
		<property name="userBiz" ref="userBiz"></property>
		<property name="age" value="22"></property>
		<property name="name" value="zhangsan"></property>
		<property name="hobby">
			<list>
				<value>篮球</value>
				<value>boy</value>
				<value>篮球</value>
			</list>
		</property>
	</bean>
	<!-- 构造注入 -->
	<bean class="com.zking.web.OrderAction" id="orderAction">
		<property name="userBiz" ref="userBiz"></property>
		<constructor-arg name="name" value="zhangsan"></constructor-arg>
		<constructor-arg name="age" value="22"></constructor-arg>
		<constructor-arg name="hobby">
			<list>
				<value>篮球</value>
				<value>boy</value>
				<value>篮球</value>
			</list>
		</constructor-arg>
	</bean>
</beans>

10、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>springConfigLocation</param-name>
  	<param-value>/applicationContext.xml</param-value>
  </context-param>
  <listener>
  	<listener-class>com.zking.ioc.listener.SpringLoadListener</listener-class>
  </listener>
</web-app>

好了今天分享Spring值IOC结束了拜拜

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值