Spring之Ioc

什么是spring

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

1 . 中间层框架、万能胶
struts2
spring
hibernate

2 . 容器框架
JavaBean 项目中的一个个类
IOC和AOP

案例一:Spring解决的问题

接口UserBiz.java

package com.xwt.ioc.biz;
/**
 * 实现业务功能的接口
 *      通过企业的案例来讲解使用spring ioc的必要性
 *      v1.0: 实现游戏上传功能
 *      v2.0: 对游戏的上传功能进行优化
 * @author wt
 *
 */
public interface UserBiz {
	public void upload();
}

UserBizImpl1.java

package com.xwt.ioc.biz.impl;

import com.xwt.ioc.biz.UserBiz;
/**
 *   实现游戏上传功能 没有考虑性能
 * @author wt
 *
 */
public class UserBizImpl1 implements UserBiz{

	@Override
	public void upload() {
		System.out.println("v1:实现用户上传的功能");
	}
}

UserBizImpl2.java

package com.xwt.ioc.biz.impl;

import com.xwt.ioc.biz.UserBiz;
/**
 *  对游戏的上传功能进行优化
 * @author wt
 *
 */
public class UserBizImpl2 implements UserBiz{

	@Override
	public void upload() {
		System.out.println("v2:开始优化性能的代码");
	}

}

UserAction.java

package com.xwt.ioc.biz.web;

import com.xwt.ioc.biz.UserBiz;
import com.xwt.ioc.biz.impl.UserBizImpl1;

public class UserAction {
	
	private UserBiz userBiz=new UserBizImpl1();
	
	public void upload() {
		userBiz.upload();
	}
}

测试IocTest.java

package com.xwt.ioc.biz.test;

import com.xwt.ioc.biz.web.UserAction;

public class IocTest {
	public static void main(String[] args) {
		UserAction user=new UserAction();
		user.upload();
	}
}

在这里插入图片描述
目前存在问题:如果说多个action类要使用同一个UserBiz接口,那么一旦biz实现类发生改变那么就需要程序员改动每一个action中的代码 ,去改变该biz接口的具体应用的实现类

案例二:Springioc就解决上述问题

实现了一处改动 多处受益的结果

UserAction.java

package com.xwt.ioc.biz.web;

import com.xwt.ioc.biz.UserBiz;

public class UserAction {
	
	private UserBiz userBiz;
	
	public UserBiz getUserBiz() {
		return userBiz;
	}

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

	public void upload() {
		userBiz.upload();
	}
}

TeacherAction.java

package com.xwt.ioc.biz.web;

import com.xwt.ioc.biz.UserBiz;
import com.xwt.ioc.biz.impl.UserBizImpl1;

public class TeacherAction {
	
	private UserBiz userBiz;
	
	public UserBiz getUserBiz() {
		return userBiz;
	}

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

	public void upload() {
		userBiz.upload();
	}
}

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 class="com.xwt.ioc.biz.impl.UserBizImpl2" id="userBiz"></bean>
   <bean class="com.xwt.ioc.biz.web.UserAction" id="userAction">
        <property name="userBiz" ref="userBiz"></property>
   </bean>
   
    <bean class="com.xwt.ioc.biz.web.TeacherAction" id="TeacherAction">
        <property name="userBiz" ref="userBiz"></property>
   </bean>
   
</beans>

测试IocTest.java

package com.xwt.ioc.biz.test;

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

import com.xwt.ioc.biz.web.TeacherAction;
import com.xwt.ioc.biz.web.UserAction;

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

在这里插入图片描述

案例三:Spring的注入方式

1.set注入
UserAction.java

package com.xwt.ioc.biz.web;

import java.util.List;

import com.xwt.ioc.biz.UserBiz;
/**
 *  IOC的注入方式及各类类型
 *   1、set注入
 *           基本类型与String
 *           数组
 *           自定义类型
 *           
 *   2、构造注入 
 *   3、自动装配
 * @author wt
 *
 */
public class UserAction {
	
	private UserBiz userBiz;
	private String uname;
	private int age;
	private List<String> love;
	
	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> getLove() {
		return love;
	}

	public void setLove(List<String> love) {
		this.love = love;
	}

	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.love);
	}
}

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 class="com.xwt.ioc.biz.impl.UserBizImpl2" id="userBiz"></bean>
   <bean class="com.xwt.ioc.biz.web.UserAction" id="userAction">
        <property name="userBiz" ref="userBiz"></property>
        <!-- set注入用property标签 -->
        <property name="uname" value="小乌龟"></property>
        <property name="age" value="17"></property>
        <property name="love">
               <list>
                       <value>唱歌</value>
                       <value>跳舞</value>
               </list>
        </property>
   </bean>
   
    <bean class="com.xwt.ioc.biz.web.TeacherAction" id="TeacherAction">
        <property name="userBiz" ref="userBiz"></property>
   </bean>
  
</beans>

测试IocTest.java

package com.xwt.ioc.biz.test;

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

import com.xwt.ioc.biz.web.TeacherAction;
import com.xwt.ioc.biz.web.UserAction;

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

在这里插入图片描述
2 . 构造注入
UserAction.java

package com.xwt.ioc.biz.web;

import java.util.List;

import com.xwt.ioc.biz.UserBiz;
/**
 *  IOC的注入方式及各类类型
 *   1、set注入
 *           基本类型与String
 *           数组
 *           自定义类型
 *           
 *   2、构造注入 
 *   3、自动装配
 * @author wt
 *
 */
public class UserAction {
	
	private UserBiz userBiz;
	private String uname;
	private int age;
	private List<String> love;
	
	public UserAction() {}
	
	public UserAction(String uname, int age) {
		this.uname = uname;
		this.age = age;
	}
	
	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> getLove() {
		return love;
	}

	public void setLove(List<String> love) {
		this.love = love;
	}

	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.love);
	}
}

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 class="com.xwt.ioc.biz.impl.UserBizImpl2" id="userBiz"></bean>
   <bean class="com.xwt.ioc.biz.web.UserAction" id="userAction">
        <property name="userBiz" ref="userBiz"></property>
       <!-- 构造注入用constructor-arg标签 -->
       <constructor-arg name="uname" value="小王八"></constructor-arg>
       <constructor-arg name="age" value="19"></constructor-arg>
        <property name="love">
               <list>
                       <value>唱歌</value>
                       <value>跳舞</value>
               </list>
        </property>
   </bean>
   
    <bean class="com.xwt.ioc.biz.web.TeacherAction" id="TeacherAction">
        <property name="userBiz" ref="userBiz"></property>
   </bean>
   
</beans>

测试IocTest.java

package com.xwt.ioc.biz.test;

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

import com.xwt.ioc.biz.web.TeacherAction;
import com.xwt.ioc.biz.web.UserAction;

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

在这里插入图片描述
3 . 自动装配
spring4之后出现的
byType: spring框架根据接口自动在spring的上下文中去寻找与之匹配的实现类,进行自动注入
如果在spring上下文中有连个匹配上的,那会报错
byName: spring框架根据接口的名字(属性名)去上下文进行寻找,是通过Bean标签的id进行寻找

<?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"
	<!-- default-autowire="byName" -->
	
	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 class="com.xwt.ioc.biz.impl.UserBizImpl1" id="userBiz"></bean>
   <bean class="com.xwt.ioc.biz.web.UserAction" id="userAction">
       <!--  <property name="userBiz" ref="userBiz"></property> -->
        <!-- set注入用property标签 -->
        <!-- <property name="uname" value="小乌龟"></property> -->
       <!--  <property name="age" value="17"></property> -->
       <!-- 构造注入用constructor-arg标签 -->
       <constructor-arg name="uname" value="小王八"></constructor-arg>
       <constructor-arg name="age" value="19"></constructor-arg>
        <property name="love">
               <list>
                       <value>唱歌</value>
                       <value>跳舞</value>
               </list>
        </property>
   </bean>
   
    <bean class="com.xwt.ioc.biz.web.TeacherAction" id="TeacherAction">
        <!-- <property name="userBiz" ref="userBiz"></property> -->
   </bean>
   
</beans>

案例四:Spring与web容器的整合原理

实现的目标:
在用户每一次发送任意请求,在对应请求处理代码中可以获取到spring容器中配置的任意的JavaBean,从而可以对应的javabean中定义的方法

实现思路:
1、在tomcat启动的时候自动去加载spring的上下文(ApplicationContext)
2、利用监听器去把spring的上下文放到tomcat的上下文中
3、为了解决框架的配置文件冲突的问题,我们需要动态指定spring上下文的配置文件名称;

SpringLoaderListener.java

package com.xwt.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--->任意的JavaBean
 * @author wt
 *
 */
@WebListener
public class SpringLoaderListener implements ServletContextListener{
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("tomcat启动会加载的方法");
       //1、在tomcat启动的时候自动去加载spring的上下文(ApplicationContext)
		ServletContext servletContext = sce.getServletContext();
		String springXmlLocation = servletContext.getInitParameter("springXmlLocation");
		if(springXmlLocation == null || "".equals(springXmlLocation)) {
			springXmlLocation = "/spring-context.xml";
		}
		System.out.println("Spring上下配置文件是:"+springXmlLocation);
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(springXmlLocation);
		servletContext.setAttribute("spring_key", applicationContext);
	}
}

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

UserServlet.java

package com.xwt.ioc.biz.web;

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.ApplicationContext;
@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 {
		System.out.println("处理用户请求");
		ApplicationContext springContext = (ApplicationContext) req.getServletContext().getAttribute("SPRING_WEB_KEY");
		UserAction userAction=(UserAction) springContext.getBean("userAction");
		userAction.test1();
	}
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
东南亚位于我国倡导推进的“一带一路”海陆交汇地带,作为当今全球发展最为迅速的地区之一,近年来区域内生产总值实现了显著且稳定的增长。根据东盟主要经济体公布的最新数据,印度尼西亚2023年国内生产总值(GDP)增长5.05%;越南2023年经济增长5.05%;马来西亚2023年经济增速为3.7%;泰国2023年经济增长1.9%;新加坡2023年经济增长1.1%;柬埔寨2023年经济增速预计为5.6%。 东盟国家在“一带一路”沿线国家中的总体GDP经济规模、贸易总额与国外直接投资均为最大,因此有着举足轻重的地位和作用。当前,东盟与中国已互相成为双方最大的交易伙伴。中国-东盟贸易总额已从2013年的443亿元增长至 2023年合计超逾6.4万亿元,占中国外贸总值的15.4%。在过去20余年中,东盟国家不断在全球多变的格局里面临挑战并寻求机遇。2023东盟国家主要经济体受到国内消费、国外投资、货币政策、旅游业复苏、和大宗商品出口价企稳等方面的提振,经济显现出稳步增长态势和强韧性的潜能。 本调研报告旨在深度挖掘东南亚市场的增长潜力与发展机会,分析东南亚市场竞争态势、销售模式、客户偏好、整体市场营商环境,为国内企业出海开展业务提供客观参考意见。 本文核心内容: 市场空间:全球行业市场空间、东南亚市场发展空间。 竞争态势:全球份额,东南亚市场企业份额。 销售模式:东南亚市场销售模式、本地代理商 客户情况:东南亚本地客户及偏好分析 营商环境:东南亚营商环境分析 本文纳入的企业包括国外及印尼本土企业,以及相关上下游企业等,部分名单 QYResearch是全球知名的大型咨询公司,行业涵盖各高科技行业产业链细分市场,横跨如半导体产业链(半导体设备及零部件、半导体材料、集成电路、制造、封测、分立器件、传感器、光电器件)、光伏产业链(设备、硅料/硅片、电池片、组件、辅料支架、逆变器、电站终端)、新能源汽车产业链(动力电池及材料、电驱电控、汽车半导体/电子、整车、充电桩)、通信产业链(通信系统设备、终端设备、电子元器件、射频前端、光模块、4G/5G/6G、宽带、IoT、数字经济、AI)、先进材料产业链(金属材料、高分子材料、陶瓷材料、纳米材料等)、机械制造产业链(数控机床、工程机械、电气机械、3C自动化、工业机器人、激光、工控、无人机)、食品药品、医疗器械、农业等。邮箱:market@qyresearch.com

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值