Spring框架及IOC容器
Spring是一个非常活跃的开源框架, 它是一个基于IOC和AOP来构架多层JavaEE系统的框架,它的主要目地是简化企业开发。Spring以一种非侵入式的方式来管理你的代码, Spring提倡”最少侵入”,这也就意味着你可以适当的时候安装或卸载Spring。
1.Spring 6个模块

Spring提供了一站式解决方案:
1) Spring Core spring的核心功能: IOC容器, 解决对象创建及依赖关系
2) Spring Web Spring对web模块的支持。
可以与struts整合,让struts的action创建交给spring
spring mvc模式
3) Spring DAO Spring 对jdbc操作的支持 【JdbcTemplate模板工具类】
4) Spring ORM spring对orm的支持:
可以与hibernate整合
也可以使用spring的对hibernate操作的封装
5)Spring AOP 切面编程
6)SpringEE spring 对javaEE其他模块的支持
2.引入jar文件
commons-logging-1.1.3.jar 日志
spring-beans-3.2.5.RELEASE.jar bean节点
spring-context-3.2.5.RELEASE.jar spring上下文节点
spring-core-3.2.5.RELEASE.jar spring核心功能
spring-expression-3.2.5.RELEASE.jar spring表达式相关表
3.核心配置文件
applicationContext.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:p="http://www.springframework.org/schema/p"
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/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--IOC容器的配置,要创建的所有对象都配置在这里-->
<bean id="user" class="com.juaner.spring.User" scope="prototype" lazy-init="true"/>
</beans>
4.使用Spring容器
@Test
public void test2() {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("com/juaner/spring/applicationContext.xml");
User user = (User) ac.getBean("user");
System.out.println(user);
}
5.实例化bean
bean的属性scope指定单例、多例模式。
如果为单例模式,对象在容器初始化之前创建,lazy_init可以延迟初始化到对象使用时才创建对象,支队单例有效。
如果为多例模式,对象在使用时创建。
<!--默认无参数构造器-->
<bean id="user" class="com.juaner.spring.User" scope="prototype" lazy-init="true"/>
<!--带参数的构造器-->
<bean id="user1" class="com.juaner.spring.User" scope="prototype" lazy-init="true">
<constructor-arg value="100"> </constructor-arg>
<constructor-arg value="juaner"> </constructor-arg>
</bean>
<!--使用工厂类创建对象-->
<!--工厂类实例方法-->
<bean id="factory" class="com.juaner.spring.ObjectFactory"></bean>
<bean id="user2" factory-bean="factory" factory-method="getInstance"></bean>
<!--工厂静态方法-->
<bean id="user3" class="com.juaner.spring.ObjectFactory" factory-method="getStaticInstance"/>
6.设置依赖注入(Dependency Injection)
1) 通过构造函数
2) 通过set方法给属性注入值
<bean id="userDao" class="com.juaner.spring.dependencyInjection.UserDao"/>
<bean id="userService" class="com.juaner.spring.dependencyInjection.UserService">
<property name="userDao" ref="userDao"/>
</bean>
<bean id="userAction" class="com.juaner.spring.dependencyInjection.UserAction">
<property name="userService" ref="userService"/>
</bean>
3) p名称空间,需要引入命名空间
xmlns:p="http://www.springframework.org/schema/p"
<bean id="userDao" class="com.juaner.spring.dependencyInjection.UserDao"/>
<bean id="userService" class="com.juaner.spring.dependencyInjection.UserService" p:userDao-ref="userDao"/>
<bean id="userAction" class="com.juaner.spring.dependencyInjection.UserAction" p:userService-ref="userService"/>
4)自动装配
通过属性名查找:
<bean id="userDao" class="com.juaner.spring.dependencyInjection.UserDao"/>
<bean id="userService" class="com.juaner.spring.dependencyInjection.UserService" autowire="byName"/>
<bean id="userAction" class="com.juaner.spring.dependencyInjection.UserAction" autowire="byName"/>
通过类型查找,必须确保改类型在IOC容器中只有一个对象;否则报错:
<bean id="userDao" class="com.juaner.spring.dependencyInjection.UserDao"/>
<bean id="userService" class="com.juaner.spring.dependencyInjection.UserService" autowire="byType"/>
<bean id="userAction" class="com.juaner.spring.dependencyInjection.UserAction" autowire="byType"/>
5) 注解
引入context名称空间
xmlns:context="http://www.springframework.org/schema/context"
开启注解扫描
<context:component-scan base-package="com.juaner.spring.dependencyInjection"/>
创建对象以及处理对象依赖关系,相关的注解:
@Component 指定把一个对象加入IOC容器
@Repository 作用同@Component; 在持久层使用
@Service 作用同@Component; 在业务逻辑层使用
@Controller 作用同@Component; 在控制层使用
@Resource 属性注入
7.完整配置
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="employeeAction" class="cn.itcast.action.EmployeeAction" scope="prototype">
<property name="employeeService" ref="employeeService"></property>
</bean> </beans>
Spring框架及IOC容器的更多相关文章
- Spring框架 之IOC容器 和AOP详解
主要分析点: 一.Spring开源框架的简介 二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面向切面编程(AOP)和事务管理配置 一.S ...
- Spring框架学习[IoC容器高级特性]
1.通过前面4篇文章对Spring IoC容器的源码分析,我们已经基本上了解了Spring IoC容器对Bean定义资源的定位.读入和解析过程,同时也清楚了当用户通过getBean方法向IoC容器获取 ...
- Spring框架之IOC(控制反转)
[TOC] 第一章Spring框架简介 IOC(控制反转)和AOP(面向方面编程)作为Spring框架的两个核心,很好地实现了解耦合.所以,简单来说,Spring是一个轻量级的控制反转(IoC)和面向 ...
- Spring框架中IoC(控制反转)的原理(转)
原文链接:Spring框架中IoC(控制反转)的原理 一.IoC的基础知识以及原理: 1.IoC理论的背景:在采用面向对象方法设计的软件系统中,底层实现都是由N个对象组成的,所有的对象通过彼此的合作, ...
- Spring.NET的IoC容器(The IoC container)——简介(Introduction)
简介 这个章节介绍了Spring Framework的控制反转(Inversion of Control ,IoC)的实现原理. Spring.Core 程序集是Spring.NET的 IoC 容器实 ...
- Spring之一:IoC容器体系结构
温故而知心. Spring IoC概述 常说spring的控制反转(依赖反转),看看维基百科的解释: 如果合作对象的引用或依赖关系的管理要由具体对象来完成,会导致代码的高度耦合和可测试性降低,这对复杂 ...
- 比Spring简单的IoC容器
比Spring简单的IoC容器 Spring 虽然比起EJB轻量了许多,但是因为它需要兼容许多不同的类库,导致现在Spring还是相当的庞大的,动不动就上40MB的jar包, 而且想要理解Spring ...
- Spring框架(3)---IOC装配Bean(注解方式)
IOC装配Bean(注解方式) 上面一遍文章讲了通过xml来装配Bean,那么这篇来讲注解方式来讲装配Bean对象 注解方式需要在原先的基础上重新配置环境: (1)Component标签举例 1:导入 ...
- 使用Spring.NET的IoC容器
使用Spring.NET的IoC容器 0. 辅助类库 using System; using System.Collections.Generic; using System.Linq; using ...
随机推荐
- C10K及C100K问题探讨 & 怎么应对大流量大并发
首先开宗明义,离开业务单独讨论并发,都是扯淡. 就像 https://www.zhihu.com/question/20493166/answer/15998053 这里面说的 谈并发必然要谈业务,空 ...
- 5.7 C和C++的关系
- D3.js 第一个程序 HelloWorld
一.HTML 是怎么输出 HelloWorld 的 <html> <head> <meta charset="utf-8"> <title ...
- openSUSE 国内镜像和镜像使用帮助 (zhuan)
https://my.oschina.net/u/2475751/blog/631036?p={{currentPage-1}} https://lug.ustc.edu.cn/wiki/mirror ...
- Mybatis调用Mysql存储过程
在我的后台系统中,今天需要使用到存储过程.存储过程还真没写过,今天就写了个存储过程.使用在后台中. 其实这个接口功能 是涉及几张表的修改,删除,新增的.就写个一个存储过程. 存储过程: ), ),) ...
- Canu FAQ常见问题
链接:Canu FAQ Q: What resources does Canu require for a bacterial genome assembly(细菌基因组组装)? A mammal ...
- MIRO校验过程
一.介绍发票校验是物料管理(MM)系统的一部分.它提供物料管理部分和财务会计, 成本控制和资产管理部分的连接.物料管理模块的发票校验为以下目的服务:它完成物料采购的全过程 - 物料采购从采购申请开始, ...
- 【转】 C++中delete和delete[]的区别
一直对C++中的delete和delete[]的区别不甚了解,今天遇到了,上网查了一下,得出了结论.做个备份,以免丢失. C++告诉我们在回收用 new 分配的单个对象的内存空间的时候用 delete ...
- 三张图彻底了解Java中字符串的不变性
转载: 三张图彻底了解Java中字符串的不变性 定义一个字符串 String s = "abcd"; s中保存了string对象的引用.下面的箭头可以理解为"存储他的引用 ...
- word-break:break-all和word-wrap:break-word的区别
了解word-break属性 /* 关键字值 */ word-break: normal; word-break: break-all; word-break: keep-all; /* 全局值 */ ...