spring–ioc

1. 什么是spring,它能够做什么?

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

  1. 中间层框架、万能胶
    struts2
    spring
    hibernate
  2. 容器框架
    JavaBean 项目中的一个个类
    IOC和AOP

2. 什么是控制反转(或依赖注入)

控制反转(IoC=Inversion of Control)IoC,用白话来讲,就是由容器控制程序之间的(依赖)关系,而非传统实现中,由程序代码直接操控。这也就是所谓“控制反转”的概念所在:(依赖)控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转。
IoC还有一个另外的名字:“依赖注入 (DI=Dependency Injection)” ,即由容器动态的将某种依赖关系注入到组件之中
案例:实现Spring的IoC

IOC/DI
将以前由程序员实例化对象/赋值的工作交给了spring处理

3. 如何在spring当中定义和配置一个JavaBean(使用无参构造方法+set方法创建一个JavaBean)

  1. id:在容器中查找Bean的id(唯一、且不能以/开头)
  2. class:bean的完整类名
  3. name:在容器中查找Bean的名字(唯一、允许以/开头、允许多个值,多个值之间用逗号或空格隔开)
  4. scope:(singleton|prototype)默认是singleton
    4.1 singleton(单例模式):在每个Spring IoC容器中一个bean定义对应一个对象实例
    4.2 prototype(原型模式/多例模式):一个bean定义对应多个对象实例
  5. abstract:将一个bean定义成抽象bean(抽象bean是不能实例化的),抽象类一定要定义成抽象bean,非抽象类也可以定义成抽象bean
  6. parent:指定一个父bean(必须要有继承关系才行)
  7. init-method:指定bean的初始化方法
  8. constructor-arg:使用有参数构造方法创建javaBean

注1:struts2的Action请使用多例模式

4. 简单属性的配置:

8基础数据&String+3个sql
java.util.Date
java.sql.Date
java.sql.Time
java.sql.Timestamp
通过标签赋值即可

5. 复杂属性的配置

  1. JavaBean
    ref bean=""
  2. List或数组
  3. Map
  4. Properties

6. 针对项目,配置文件路径的2种写法

 ApplicationContextString path = "applicationContext.xml";String path = "classpath:applicationContext-*.xml";//srcString[] path = new String[] { "applicationContext-a.xml", "applicationContext-b.xml" };//分模块开发

7. spring与web项目的集成

 WEB项目如何读取spring上下文通过监听器实现ServletContextListenercontextConfigLocation:classpath:applicationContext-*.xml

8. log4j2

9. spring.pom

spring-context
spring-orm
spring-web
spring-aspects
注:创建spring的XML文件时,需要添加beans/aop/tx/context标签支持

<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.10.RELEASE</version>
</dependency>

简介
spring-context.xml
ioc
set注入
基本数据类型注入
集合注入
对象注入
构造注入
基本数据类型注入
自动装配

tomcat整合ioc容器

1、spring tool suite官方下载地址:http://spring.io/tools/sts/all

2、很详细的网文在线安装介绍:http://www.cnblogs.com/liuyungao/p/6213997

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.lrc</groupId><artifactId>spring</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>spring Maven Webapp</name><url>http://maven.apache.org</url><properties><spring.version>5.0.1.RELEASE</spring.version><javax.servlet.version>4.0.0</javax.servlet.version><junit.version>4.12</junit.version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- 2、导入spring依赖 --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>${spring.version}</version></dependency><!-- 5.1、junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency><!-- 5.2、servlet --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>${javax.servlet.version}</version><scope>provided</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.10.RELEASE</version></dependency></dependencies><build><finalName>spring</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.7.0</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin></plugins></build>
</project>

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.xsdhttp://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"><!-- id:在容器中查找Bean的id(唯一、且不能以/开头)class:bean的完整类名name:在容器中查找Bean的名字(唯一、允许以/开头、允许多个值,多个值之间用逗号或空格隔开)scope:(singleton|prototype)默认是singletonsingleton(单例模式):在每个Spring IoC容器中一个bean定义对应一个对象实例prototype(原型模式/多例模式):一个bean定义对应多个对象实例abstract:将一个bean定义成抽象bean(抽象bean是不能实例化的),抽象类一定要定义成抽象bean,非抽象类也可以定义成抽象beanparent:指定一个父bean(必须要有继承关系才行)init-method:指定bean的初始化方法constructor-arg:使用有参数构造方法创建javaBean --><!-- default-autowire="byType" --><!-- byName按name去进行自动装配 --><bean id="userBiz" class="com.LHJ.ioc.biz.impl.UserBizImpl1"></bean><bean id="userAction" class="com.LHJ.ioc.web.UserAction"><!-- <property name="uid" value="22"></property> <property name="uname" value="LHJ"></property> --><property name="userBiz" ref="userBiz"></property><constructor-arg name="uid" value="22"></constructor-arg><constructor-arg name="uname" value="LHJ"></constructor-arg><property name="hobby"><list><value>唱</value><value>跳</value><value>rap</value></list></property></bean>
</beans>

UserBiz

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

UserBizImpl1

package com.lrc.ioc.biz;import com.lrc.ioc.biz.UserBiz;public class UserBizImpl1 implements  UserBiz {@Overridepublic void upload() {System.out.println("实现游戏上传的功能");}}

UserBizImpl2

package com.lrc.ioc.biz;import com.lrc.ioc.biz.UserBiz;public class UserBizImpl2 implements  UserBiz {@Overridepublic void upload() {System.out.println("实现游戏上传的功能");}}

UserAction

package com.lrc.ioc.web;import java.util.List;import com.lrc.ioc.biz.UserBiz;/*** IOC的注入方式及各类类型 set注入 基本类型与String 数组 自定义类型 构造注入 自动装配* * 构造注入 自动装配 spring4之后出现的 byType:根据配置的Bean中的接口,在Spring的上下文中寻找对应的实现类* byName:根据配置的Bean中的接口名字,在Spring的上下文中寻找对应的实现类**/
public class UserAction {private UserBiz userBiz;private String uname;private int age;private List<String> hobby;public UserAction() {super();// TODO Auto-generated constructor stub}public UserAction(String uname, int age) {super();this.uname = uname;this.age = age;}public UserBiz getUserBiz() {return userBiz;}public void setUserBiz(UserBiz userBiz) {this.userBiz = userBiz;}public List<String> getHobby() {return hobby;}public void setHobby(List<String> hobby) {this.hobby = hobby;}public void upload() {userBiz.upload();}/*** 注入问题*/public void test1() {System.out.println(this.uname);System.out.println(this.age);System.out.println(this.hobby);}}

IocTest

package com.lrc.ioc.test;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.lrc.ioc.web.UserAction;public class IocTest {public static void main(String[] args) {// UserAction userAction=new UserAction();// userAction.upload();ApplicationContext springContext = new ClassPathXmlApplicationContext("/spring-context.xml");UserAction userAction = (UserAction) springContext.getBean("xxx");OrderAction orderAction = (OrderAction) springContext.getBean("jy");userAction.upload();orderAction.upload();// userAction.test1();}
}

创建一个监听器SpringLoaderListener

package com.lrc.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,那么如何在用户发送请求的时候能够访问到 处理方式:* 在监听器中将spring的上下文交给tomcat的上下文中进行管理* 浏览器--》request-->servletContext-->springContext-->任意的javaBean* **/
@WebListener
public class SpringLoaderListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent sce) {System.out.println("tomcat一启动就触发了...");ServletContext tomcatContext = sce.getServletContext();String springXmlLocation = tomcatContext.getInitParameter("springXmlLocation");System.out.println("spring的上下文配置文件:" + springXmlLocation);ApplicationContext springContext = null;if (springXmlLocation == null || "".equals(springXmlLocation)) {springContext = new ClassPathXmlApplicationContext("/spring-context.xml");} else {springContext = new ClassPathXmlApplicationContext(springXmlLocation);}tomcatContext.setAttribute("spring_key", springContext);}}

UserServlet

package com.lrc.ioc.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 {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("处理用户请求");ApplicationContext springContext = (ApplicationContext) req.getServletContext().getAttribute("spring_key");UserAction userAction = (UserAction) springContext.getBean("xxx");userAction.upload();}
}

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"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-other.xml</param-value></context-param>
</web-app>

spring--ioc相关推荐

  1. spring IOC创建对象方式

    spring IOC创建对象方式 通过无参构造来创建 验证方法: 创建实体类: public class User {private String name;public User() {System ...

  2. spring ioc原理分析

    spring ioc原理分析 spring ioc 的概念 简单工厂方法 spirng ioc实现原理 spring ioc的概念 ioc: 控制反转 将对象的创建由spring管理.比如,我们以前用 ...

  3. Spring IoC是如何进行依赖注入的

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 依赖注入(DI) DI(Dependency Injection) ...

  4. 面试被问烂的 Spring IOC(求求你别再问了)

    点击上方"方志朋",选择"置顶或者星标" 你的关注意义重大! 作者:莫那·鲁道 链接:http://thinkinjava.cn 广义的 IOC IoC(Inv ...

  5. spring根据名称获取bean_带你从零开始手写 spring ioc 框架,深入学习 spring 源码

    IoC Ioc 是一款 spring ioc 核心功能简化实现版本,便于学习和理解原理. 创作目的 使用 spring 很长时间,对于 spring 使用非常频繁,实际上对于源码一直没有静下心来学习过 ...

  6. 还不懂spring IOC核心原理?200行代码带你手撸一个

    Spring做为Java企业级应用的开源开发框架,早已成为Java后端开发事实上的行业标准,无数的公司选择Spring作为基础的开发框架. 使用Spring框架的人一定都听过Spring的IoC(控制 ...

  7. 头条一面:Spring IOC容器中只存放单例Bean吗?

    最近,很多小伙伴出去面试,感觉自己面的不是很理想,回来后,不少小伙伴把面试题做了记录发给我,让我给大家解析下,然后发出来.当我看到这些面试题时,快速在脑海中构建起了整个知识体系,从基础到框架.从分布式 ...

  8. 谈谈对Spring IOC的理解

    2019独角兽企业重金招聘Python工程师标准>>> 学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人 ...

  9. Spring点滴一:Spring Ioc 容器

    Spring 容器: Spring 容器是Spring框架的核心.Spring容器将创建Bean对象实例,把它们联系在一起,配置它们,并管理它们整个生命周期从创建到销毁.Spring 容器通过依赖注入 ...

  10. MyEclipse Spring 学习总结一 Spring IOC容器

    一.Spring IOC容器---- Spring AllicationContext容器 程序的结构如下: 1.首先在MyEclipse 创建创建Java Project 2.创建好后,添加spin ...

最新文章

  1. 并发编程不是少数派技能,每个程序员都要尝试掌握
  2. AlarmManager深入浅出
  3. 常用的函数式接口_Predicate接口
  4. c++ 传入动态参数_一文了解Mybatis中动态SQL的实现
  5. 在业务层实现校验请求参数
  6. linux 随机抽取文件,shell 随机从文件中抽取若干行的实现方法
  7. matlab 二维高斯滤波 傅里叶_机器视觉 03.2 频域低通滤波
  8. java Launcher源码_Launcher3源码浅析(5.1)--Launcher.java
  9. 跑linux编译什么CPU速度快,linux 加快编译速度
  10. 面向对象 —— 类设计(十一)—— 构造与析构
  11. Access、Trunk、Hybrid三种端口收发规则以及tagged端口和untagged端口的区别
  12. 水光半导体于2017 CES展示全方位通讯网络、多媒体及消费性电子芯片解决方案
  13. MyEclipse 2014 破解失败,cracker.jar文件打开闪退
  14. 《PyQt5 开发技巧与实践》学习笔记
  15. 微信群发消息怎么发?微信群发消息只需要4步?
  16. redis-setnx-实现原理
  17. 180122 逆向-Frida在Windows下的使用
  18. Nano Today(IF=21)| 北京大学齐宪荣团队通过原位重编程巨噬细胞增强肿瘤免疫治疗效果
  19. (复健计划)python中的字典
  20. ORACLE EBS WORKFLOW实现多附件下载

热门文章

  1. 《MongoDB入门教程》第12篇 查询结果排序
  2. 编写程序HelloWorld
  3. 删除我的电脑下的360云U盘
  4. 心灵鸡汤----幸福来敲门
  5. 为什么很多人说现在做什么生意都不好做?
  6. 易网客商业Wifi的时代
  7. 产品运营的数据指标体系
  8. 制造业物联网的商业案例
  9. 《刺客信条:起源》画面BUG?我先“退”一步!
  10. Box2D v2.1.0用户手册翻译 - 第12, 13, 14章