什么是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注入

  1. 基本数据类型

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概括的更多相关文章

  1. Spring框架IOC容器和AOP解析 非常 有用

    Spring框架IOC容器和AOP解析   主要分析点: 一.Spring开源框架的简介  二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面 ...

  2. 自己动手写Spring框架--IOC、MVC

    对于一名Java开发人员,我相信没有人不知道 Spring 框架,而且也能够轻松就说出 Spring 的特性-- IOC.MVC.AOP.ORM(batis). 下面我想简单介绍一下我写的轻量级的 S ...

  3. Spring框架IOC容器和AOP解析

    主要分析点: 一.Spring开源框架的简介  二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面向切面编程(AOP)和事务管理配置  一.S ...

  4. spring框架--IOC容器,依赖注入

    思考: 1. 对象创建创建能否写死? 2. 对象创建细节 对象数量 action  多个   [维护成员变量] service 一个   [不需要维护公共变量] dao     一个   [不需要维护 ...

  5. Spring框架---IOC装配Bean

    IOC装配Bean (1)Spring框架Bean实例化的方式提供了三种方式实例化Bean 构造方法实例化(默认无参数,用的最多) 静态工厂实例化 实例工厂实例化 下面先写这三种方法的applicat ...

  6. Spring框架IOC,DI概念理解

    1.什么是框架? 框架是一种重复使用的解决方案,针对某个软件开发的问题提出的. Spring框架,它是一个大型的包含很多重复使用的某个领域的解决方案. Spring的理念:不要重复发明轮子. 2.Sp ...

  7. 【Java框架型项目从入门到装逼】第一节 - Spring框架 IOC的丧心病狂解说

    大家好,好久不见,今天我们来一起学习一下关于Spring框架的IOC技术. 控制反转--Spring通过一种称作控制反转(IoC)的技术促进了松耦合.当应用了IoC,一个对象依赖的其它对象会通过被动的 ...

  8. Spring框架-IOC和AOP简单总结

    参考博客: https://blog.csdn.net/qq_22583741/article/details/79589910 1.Spring框架是什么,为什么,怎么用 1.1 Spring框架是 ...

  9. Spring框架 IOC注解

    Spring框架的IOC之注解方式的快速入门        1. 步骤一:导入注解开发所有需要的jar包        * 引入IOC容器必须的6个jar包        * 多引入一个:Spring ...

随机推荐

  1. iOS开发~防止navigation多次push一个页面

    在点击push下一个页面时,因为各种原因,点一下cell或按钮没有响应,用户可能就多点几下,这时候会打开好几个一样的页面. 这是因为push后的页面有耗时操作或者刚好push到另一个页面时,另一个页面 ...

  2. 【hadoop环境问题】namenode无法启动问题解决

    [问题背景] 要在自己的阿里云上搭伪分布式,用来复习和强化hive相关的知识,在执行脚本 sh start-dfs.sh后,jps后: 然后查看name的日志,报错如下: [解决方法] 网上的很多方法 ...

  3. Spring家族主流成员介绍

    摘 要:Spring 就像一个大家族,有众多衍生产品例如 Boot,Security,JPA等等.但他们的基础都是Spring 的 IOC 和 AOP,IOC提供了依赖注入的容器,而AOP解决了面向切 ...

  4. 如何实现数组与List的相互转换

    List转数组:toArray(arraylist.size()方法 数组转List:Arrays的asList(a)方法 List<String> arrayList = new Arr ...

  5. PHPStorm 快捷键大全(Win/Linux/Mac)

    下面的-符号记得改成 ‘`’,markdown 语法会转义.使用频率是我自己为准.仅供参考   Mac 符号 符号 解释 ⌘ Command ⇧ Shift ⌃ Control ↩ Enter/Ret ...

  6. Outlook 邮箱脱机工作解决方法

    在运维过程中,有时候会收到用户这样的抱怨:为什么别人发给我的邮件我都收不到,我的邮件也发不出去了? Outlook 2016图标上显示着一个红叉... 这种情况有时候是因为Outlook正在脱机工作, ...

  7. Uber如何搭建一个基于Kafka的跨数据中心复制平台 原创: 徐宏亮 AI前线 今天

    Uber如何搭建一个基于Kafka的跨数据中心复制平台 原创: 徐宏亮 AI前线 今天

  8. Spring Bootz之热部署

    在项目的pom.xml文件添加如下两段 <dependency> <groupId>org.springframework.boot</groupId> <a ...

  9. 使用editplus等编程工具时UTF-8编码去掉BOM头方法(转载备查)

            Unicode规范中有一个BOM的概念.BOM——Byte Order Mark,就是字节序标记.在这里找到一段关于BOM的说明: 在UCS 编码中有一个叫做"ZERO WI ...

  10. 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_06-SpringSecurityOauth2研究-Oauth2授权码模式-申请令牌

    3.3 Oauth2授权码模式 3.3.1 Oauth2授权模式 Oauth2有以下授权模式: 授权码模式(Authorization Code) 隐式授权模式(Implicit) 密码模式(Reso ...