Spring框架ioc概括
什么是Spring且能做什么
- Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。
- Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
- Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
- 目的:解决企业应用开发的复杂性
- 功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
- 范围:任何Java应用
- 简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
图解,如果框架太多的话,会导致开发复杂性大大提高,框架与框架之间到处串联;

但是如果以spring框架为中心去串联其他框架,就大大降低了开发的复杂性

什么是控制反转(Ioc)
- 控制反转(IoC=Inversion of Control)IoC,用白话来讲,就是由容器控制程序之间的(依赖)关系,而非传统实现中,由程序代码直接操控。这也就是所谓“控制反转”的概念所在:(依赖)控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转。
- IoC还有一个另外的名字:“依赖注入 (DI=Dependency Injection)” ,即由容器动态的将某种依赖关系注入到组件之中
案例:实现Spring的IoC
IOC/DI
将以前由程序员实例化对象/赋值的工作交给了spring处理
Biz层
创建一个接口类,写一个方法接口
package com.ht.ioc.biz;
/**
* 需求:
* 上传文件:
* 完成的思路:完成功能好 文件上传就行了
* @author Administrator
*
*/
public interface UserBiz { public void read();
}
为了体现出差距和弊端,就写俩个实现类吧
UserBizipml01
public class UserBizimpl implements UserBiz{
@Override
public void read() {
// TODO Auto-generated method stub
System.out.println("本是青灯不归客,却因浊酒留风尘。");
}
}
UserBizipml02
public class UserBizipml02 implements UserBiz{
@Override
public void read() {
// TODO Auto-generated method stub
System.out.println("星光不问赶路人,岁月不负有心人。");
}
}
写一个测试类去调用web层的测试方法让他在控制台上实现效果
package com.ht.ioc.test; import com.ht.ioc.web.UserAction; /**
* 模拟浏览器请求后台
* @author Administrator
*
*/
public class Demo1 {
public static void main(String[] args) {
//实例化一个用户web类
UserAction userAction=new UserAction();
userAction.text03();//调用web层中的测试方法
}
}
在web层中调用接口不同的实现类
public class UserAction {
// 实例化一个接口
private UserBiz userBiz = new UserBizimpl();
public void text03() {
userBiz.read();
}
}
效果图:

public class UserAction {
// 实例化一个接口
private UserBiz userBiz = new UserBizipml02();
public void text03() {
userBiz.read();
}
}
效果图:

由此也可以看出来弊端:
当需求变化非常快的时候,不便于维护,因为维护的权利是属于程序员的
spring的ioc就是解决这一个问题的
将维护代码的权利由程序员转交给spring容器来完成
如何在spring当中定义和配置一个JavaBean(使用无参构造方法+set方法创建一个JavaBean)
- id:在容器中查找Bean的id(唯一、且不能以/开头)
- class:bean的完整类名
- name:在容器中查找Bean的名字(唯一、允许以/开头、允许多个值,多个值之间用逗号或空格隔开)
- scope:(singleton|prototype)默认是singleton
- singleton(单例模式):在每个Spring IoC容器中一个bean定义对应一个对象实例
- prototype(原型模式/多例模式):一个bean定义对应多个对象实例
- abstract:将一个bean定义成抽象bean(抽象bean是不能实例化的),抽象类一定要定义成抽象bean,非抽象类也可以定义成抽象bean
- parent:指定一个父bean(必须要有继承关系才行)
- init-method:指定bean的初始化方法
constructor-arg:使用有参数构造方法创建javaBean
set注入
- 基本数据类型
userAction类
public class UserAction {
private int uid;
private String uname;
private List<String> hobby =new ArrayList<String>();
public void setUid(int uid) {
this.uid = uid;
}
public void setUname(String uname) {
this.uname = uname;
}
public void setHobby(List<String> hobby) {
this.hobby = hobby;
}
public int getUid() {
return uid;
}
public String getUname() {
return uname;
}
public List<String> getHobby() {
return hobby;
}
/**
* set注入ע
*
*/
public void text01() {
System.out.println("uid:"+this.uid);
System.out.println("uname:"+this.uname);
System.out.println("hobby:"+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: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 id="userAction" class="com.ht.ioc.web.UserAction">
<property name="uid" values=""></property>
<property name="uname" values="zk"></property>
<!-- <constructor-arg name="uid" value=""></constructor-arg>
<constructor-arg name="uname" value="zs"></constructor-arg> -->
<property name="hobby">
<list>
<value>青灯</value>
<value></value>
<value>古镇</value>
</list>
</property>
</bean>
</beans>
测试类
public class Demo1 {
public static void main(String[] args) {
// UserAction userAction=new UserAction();
// userAction.text03();
// userAction.text01();
ApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
UserAction userAction = (UserAction) context.getBean("userAction");
userAction.text01();
}
}
运行结果:
uid:23
uname:zk
hobby:[青灯,15,古镇]
2.set注入(引用类型)
useAction类改变一下
private UserBiz userBiz;
// private UserBiz userBiz = new UserBizipml02();
public UserBiz getUserBiz() {
return userBiz;
} public void setUserBiz(UserBiz userBiz) {
this.userBiz = userBiz;
}
spring-context.xml
<bean id="userAction" class="com.ht.ioc.biz.impl.UserBizimpl"></bean>
<bean id="userAction" class="com.ht.ioc.web.UserAction">
<!-- <property name="uid" values=""></property>
<property name="uname" values="zk"></property> -->
<constructor-arg name="uid" value=""></constructor-arg>
<constructor-arg name="uname" value="zs"></constructor-arg>
<property name="hobby">
<list>
<value>青灯</value>
<value></value>
<value>古镇</value>
</list>
</property>
</bean>
运行效果
uid:22
uname:zs
hobby:[青灯,15,古镇]
构造注入
userAction类
public UserAction() {
super();
}
public UserAction(int uid, String uname, List<String> hobby) {
super();
this.uid = uid;
this.uname = uname;
this.hobby = hobby;
}
spring-context.xml
<bean id="userAction" class="com.ht.ioc.web.UserAction">
<!-- <property name="uid" values=""></property>
<property name="uname" values="zk"></property> -->
<constructor-arg name="uid" value=""></constructor-arg>
<constructor-arg name="uname" value="zs"></constructor-arg>
<property name="hobby">
<list>
<value>青灯</value>
<value></value>
<value>古镇</value>
</list>
</property>
</bean>
自动装置
在spring-context.xml里加上
default-autowire="byType"
因为是根据类别进行查询,所以出现多个实体类时就不行了
- 在spring-context.xml里加上
default-autowire="byName"
这是根据名字查询,追踪的位置比较清晰,所以就不会报错
tomcat管理spring
实现思路:
如何将spring的上下文交给tomcate上下文进行管理
首先spring上下文为什么tomact?
分析: 目前工程中的所有javabean都交给了spring进行管理,那么浏览器发送请求,请求的是tomcat,
由tomcat来处理请求,tomcat处理请求一般来说都要访问数据库,数据库是由Dao层访问的,
Dao层的实体类又是spring的上下文管理,那就意味着,tomcat要处理请求,必须拿到spring的上下文,
才能拿到Dao层的javabean
上代码:
SpringLoadListener类
package com.ht.ioc.test; import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringLoadListener implements ServletContextListener{
private String springXmlLocation="";
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("启动........");
ServletContext servletContext = sce.getServletContext();
springXmlLocation=servletContext.getInitParameter("springXmlLocation");
if(null==springXmlLocation||"".equals(springXmlLocation)) {
springXmlLocation ="/spring-context.xml";
}
System.out.println("springXmlLocation:"+springXmlLocation);
ApplicationContext springContext= new ClassPathXmlApplicationContext(springXmlLocation);
servletContext.setAttribute("spring_context_key", springContext);
}
}
userServlet类
package com.ht.ioc.test; import java.io.IOException; import javax.servlet.ServletContext;
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 com.ht.ioc.web.UserAction; @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 {
// TODO Auto-generated method stub
ServletContext servletContext = req.getServletContext();
ApplicationContext springContext = (ApplicationContext) servletContext.getAttribute("spring_context_key");
UserAction bean = (UserAction) springContext.getBean("userAction");
bean.text03();
}
}
谢谢观看!
Spring框架ioc概括的更多相关文章
- Spring框架IOC容器和AOP解析 非常 有用
Spring框架IOC容器和AOP解析 主要分析点: 一.Spring开源框架的简介 二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面 ...
- 自己动手写Spring框架--IOC、MVC
对于一名Java开发人员,我相信没有人不知道 Spring 框架,而且也能够轻松就说出 Spring 的特性-- IOC.MVC.AOP.ORM(batis). 下面我想简单介绍一下我写的轻量级的 S ...
- Spring框架IOC容器和AOP解析
主要分析点: 一.Spring开源框架的简介 二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面向切面编程(AOP)和事务管理配置 一.S ...
- spring框架--IOC容器,依赖注入
思考: 1. 对象创建创建能否写死? 2. 对象创建细节 对象数量 action 多个 [维护成员变量] service 一个 [不需要维护公共变量] dao 一个 [不需要维护 ...
- Spring框架---IOC装配Bean
IOC装配Bean (1)Spring框架Bean实例化的方式提供了三种方式实例化Bean 构造方法实例化(默认无参数,用的最多) 静态工厂实例化 实例工厂实例化 下面先写这三种方法的applicat ...
- Spring框架IOC,DI概念理解
1.什么是框架? 框架是一种重复使用的解决方案,针对某个软件开发的问题提出的. Spring框架,它是一个大型的包含很多重复使用的某个领域的解决方案. Spring的理念:不要重复发明轮子. 2.Sp ...
- 【Java框架型项目从入门到装逼】第一节 - Spring框架 IOC的丧心病狂解说
大家好,好久不见,今天我们来一起学习一下关于Spring框架的IOC技术. 控制反转--Spring通过一种称作控制反转(IoC)的技术促进了松耦合.当应用了IoC,一个对象依赖的其它对象会通过被动的 ...
- Spring框架-IOC和AOP简单总结
参考博客: https://blog.csdn.net/qq_22583741/article/details/79589910 1.Spring框架是什么,为什么,怎么用 1.1 Spring框架是 ...
- Spring框架 IOC注解
Spring框架的IOC之注解方式的快速入门 1. 步骤一:导入注解开发所有需要的jar包 * 引入IOC容器必须的6个jar包 * 多引入一个:Spring ...
随机推荐
- MongoDB与RoboMongo的安装+python基本操作MongoDB
MongoDB(来自于英文单词“Humongous”,中文含义为“庞大”)是可以应用于各种规模的企业.各个行业以及各类应用程序的开源数据库.作为一个适用于敏捷开发的数据库,MongoDB的数据 ...
- linux too many files
Too many open files这个问题主要指的是进程企图打开一个文件,或者叫句柄,但是现在进程打开的句柄已经达到了上限,已经无法打开新句柄了. 网上一提到这个问题就要增加句柄上限,而往往这种情 ...
- Docs-.NET-C#-指南-语言参考-预处理器指令:#undef(C# 参考)
ylbtech-Docs-.NET-C#-指南-语言参考-预处理器指令:#undef(C# 参考) 1.返回顶部 1. #undef(C# 参考) 2018/06/30 #undef 允许你定义一个符 ...
- SQL-W3School-函数:SQL NOW() 函数
ylbtech-SQL-W3School-函数:SQL NOW() 函数 1.返回顶部 1. NOW() 函数 NOW 函数返回当前的日期和时间. 提示:如果您在使用 Sql Server 数据库,请 ...
- Unix/Linux系统下获得时间戳函数
在Unix/Linux系统下,使用gettimeofday函数来获得当前系统的时间戳,精度可达到微秒(microsecond,即μs)级别. 通过结构体timeval来存放当前时间戳的信息: #ifn ...
- osg::Camera example
#ifdef _WIN32 #include <Windows.h> #endif // _WIN32 #include<iostream> #include <osgV ...
- windows的mysql无法启动 服务没有报告任何错误
相信很多人都遇到过安装Mysql的时候出现各种各样的问题,今天小编就教大家解决window下mysql服务没有报告任何错误的情况下无法启动 的问题.本文所用的mysql版本是5.7以上版本,解决方法: ...
- ansible软件部署
每一个软件或集群都创建一个单独的角色. mkdir -pv ansible/roles/{jdk,hadoop,spark}/{tasks,files,templates,meta,handlers, ...
- 【leetcode_easy】598. Range Addition II
problem 598. Range Addition II 题意: 第一感觉就是最小的行和列的乘积即是最后结果. class Solution { public: int maxCount(int ...
- 十六章 综合实例——《跟我学Shiro》
目录贴:跟我学Shiro目录贴 简单的实体关系图 简单数据字典 用户(sys_user) 名称 类型 长度 描述 id bigint 编号 主键 username varchar 100 用户名 pa ...