spring框架以及ioc概念和运用

spring框架以及ioc概念和运用

spring框架

spring是一个开源框架,不仅可用于服务器的开发,从简单性和松耦合等角度看,更是让很多java应用从中受益,spring框架是一个轻量级的控制反转(ioc)和面向切面(aop)的容器框架

ioc概念

ioc中文意思为控制反转,又称依赖注入,传统实现中,代码直接操控程序之间的关系,也就是耦合关系非常紧密,而控制反转将控制权由应用代码中转到外部容器,这个控制权的转移就是反转。

举个例图方便理解:
在这里插入图片描述
在没使用spring前,各个类之间的关系可能是如上图一样,关系非常复杂且紧密,而使用spring后,如下图:
在这里插入图片描述
将类与类之间的联系交给第三方(spring)处理,此时spring就犹如一个胶水和中间商一样,将各个类联系起来

ioc运用

package com.luojun.action;

import java.util.List;

import com.luojun.biz.UserBiz;
/**
 * ioc 的注入方式以及各种类型
 * 
 * set注入
 *    基本类型和string
 *    数组
 *    自定义类型(此案例)
 * 构造注入
 * 自动装配
 *  spring4后出现
 *  byType:根据配置的bean中的接口,在spring的上下文中寻找对应的实现类
 *  byName:根据配置的bean中的接口名字,在spring的上下文寻找对应的实现类
 *   
 * @author luojun
 *
 */
public class UserAction {

	private UserBiz userBiz;
	private String uname;
	private int age;
	private List<String> hobby;
	

	public List<String> getHobby() {
		return hobby;
	}

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

	public void setHobby(List<String> hobby) {
		this.hobby = hobby;
	}

	public void upload() {
		userBiz.upload();
	}
	/**
	 * 注入
	 */
	public void test() {
		System.out.println(this.uname);
		System.out.println(this.age);
		System.out.println(this.hobby);
	}
	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"
	default-autowire="byType"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean class="com.luojun.biz.impl.UserBizImp1" id="userBiz"></bean>
	<bean class="com.luojun.action.UserAction" id="userAction">
		<!-- <property name="userBiz" ref="userBiz"></property> -->
		<!-- set注入 property标签 -->
		<!-- <property name="uname" value="zs"></property> <property name="age" 
			value="22"></property> -->
		<!-- 构造器注入 constructor-arg -->
		<constructor-arg name="uname" value="ls"></constructor-arg>
		<constructor-arg name="age" value="22"></constructor-arg>
		<property name="hobby">
			<list>
				<value>篮球</value>
				<value>rap</value>
			</list>
		</property>
	</bean>
	</bean>
</beans>


两个业务逻辑的action(测试):

package com.luojun.biz.impl;

import com.luojun.biz.UserBiz;

public class UserBizImp1 implements UserBiz {

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

}


package com.luojun.biz.impl;

import com.luojun.biz.UserBiz;

public class UserBizImp2 implements UserBiz {

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

}


两个不同的实现类是为了模仿一个业务逻辑:

假如项目上线时有个功能是用户上传,但是需要维护优化性能,那么如果不使用spring,需要修改那么势必就会将之前的功能代码进行修改,而使用spring只需要在注入依赖时,更换成优化版本即可,也就是这行:

<bean class="com.xiaoyang.biz.impl.UserBizImp1" id="userBiz"></bean>

测试类:

package com.luojun.test;

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

import com.luojun.action.OrderAction;
import com.luojun.action.UserAction;

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


输出:

实现用户上传功能
ls
22
[篮球, rap]


这个测试代表刚发布时版本,如果优化,只需要进行修改xml中bean为对应优化后的实现类:

<bean class="com.xiaoyang.biz.impl.UserBizImp2" id="userBiz"></bean>

输出:

开始优化性能的代码
ls
22
[篮球, rap]

与tomcat整合
与tomcat整合只需要在web.xml中配置一下上下文,并在action中进行读取配置文件信息即可:
web.xml:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>Archetype Created Web Application</display-name>
	<context-param>
		<param-name>springXmlLocation</param-name>
		<param-value>/spring-context.xml</param-value>
	</context-param>
</web-app>


目录
spring框架
ioc概念
ioc运用
与tomcat整合
spring框架以及ioc概念和运用

Dawn.x 2020-08-14 01:01:18 55 收藏
文章标签: spring
版权
目录
spring框架
ioc概念
ioc运用
与tomcat整合
spring框架
spring是一个开源框架,不仅可用于服务器的开发,从简单性和松耦合等角度看,更是让很多java应用从中受益,spring框架是一个轻量级的控制反转(ioc)和面向切面(aop)的容器框架

ioc概念
ioc中文意思为控制反转,又称依赖注入,传统实现中,代码直接操控程序之间的关系,也就是耦合关系非常紧密,而控制反转将控制权由应用代码中转到外部容器,这个控制权的转移就是反转。

举个例图方便理解:
在这里插入图片描述在没使用spring前,各个类之间的关系可能是如上图一样,关系非常复杂且紧密,而使用spring后,如下图:

在这里插入图片描述
将类与类之间的联系交给第三方(spring)处理,此时spring就犹如一个胶水和中间商一样,将各个类联系起来

ioc运用
package com.xiaoyang.action;

import java.util.List;

import com.xiaoyang.biz.UserBiz;
/**

  • ioc 的注入方式以及各种类型
  • set注入
  • 基本类型和string
  • 数组
  • 自定义类型(此案例)
  • 构造注入
  • 自动装配
  • spring4后出现
  • byType:根据配置的bean中的接口,在spring的上下文中寻找对应的实现类
  • byName:根据配置的bean中的接口名字,在spring的上下文寻找对应的实现类
  • @author xiaoyang

*/
public class UserAction {

private UserBiz userBiz;
private String uname;
private int age;
private List<String> hobby;


public List<String> getHobby() {
	return hobby;
}

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

public void setHobby(List<String> hobby) {
	this.hobby = hobby;
}

public void upload() {
	userBiz.upload();
}
/**
 * 注入
 */
public void test() {
	System.out.println(this.uname);
	System.out.println(this.age);
	System.out.println(this.hobby);
}
public UserBiz getUserBiz() {
	return userBiz;
}

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

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
spring.context.xml:

<?xml version="1.0" encoding="UTF-8"?>

<bean class="com.xiaoyang.biz.impl.UserBizImp1" id="userBiz"></bean>
<bean class="com.xiaoyang.action.UserAction" id="userAction">
	<!-- <property name="userBiz" ref="userBiz"></property> -->
	<!-- set注入 property标签 -->
	<!-- <property name="uname" value="zs"></property> <property name="age" 
		value="22"></property> -->
	<!-- 构造器注入 constructor-arg -->
	<constructor-arg name="uname" value="ls"></constructor-arg>
	<constructor-arg name="age" value="22"></constructor-arg>
	<property name="hobby">
		<list>
			<value>篮球</value>
			<value>rap</value>
		</list>
	</property>
</bean>
</bean>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
两个业务逻辑的action(测试):

package com.xiaoyang.biz.impl;

import com.xiaoyang.biz.UserBiz;

public class UserBizImp1 implements UserBiz {

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

}

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.xiaoyang.biz.impl;

import com.xiaoyang.biz.UserBiz;

public class UserBizImp2 implements UserBiz {

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

}

1
2
3
4
5
6
7
8
9
10
11
12
13
两个不同的实现类是为了模仿一个业务逻辑:

假如项目上线时有个功能是用户上传,但是需要维护优化性能,那么如果不使用spring,需要修改那么势必就会将之前的功能代码进行修改,而使用spring只需要在注入依赖时,更换成优化版本即可,也就是这行:


1
测试类:

package com.xiaoyang.test;

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

import com.xiaoyang.action.OrderAction;
import com.xiaoyang.action.UserAction;

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
输出:

实现用户上传功能
ls
22
[篮球, rap]

1
2
3
4
5
这个测试代表刚发布时版本,如果优化,只需要进行修改xml中bean为对应优化后的实现类:


1
输出:

开始优化性能的代码
ls
22
[篮球, rap]
1
2
3
4
与tomcat整合
与tomcat整合只需要在web.xml中配置一下上下文,并在action中进行读取配置文件信息即可:
web.xml:


Archetype Created Web Application

springXmlLocation
/spring-context.xml

action:

package com.luojun.action;

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 {
		// 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("userAction");
			userAction.upload();
		}
	}


运行项目后,控制台输出:

处理用户请求
开始优化性能的代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值