IOC及Bean容器
来源:互联网 发布:mac怎么切换独立显卡 编辑:程序博客网 时间:2024/06/17 00:05
最近在学习慕课网上的http://www.imooc.com/learn/196,讲的很好。这里结合网上的资料还有《Spring in Action》 做的一点笔记
---
IOC 控制反转 ,就是把控制权交给容器
相当于住房子,我们自己不用建筑房子,而是去找中介 租或者买房。
Bean
,Spring
里的Java
对象都是Bean
Spring
注入是指在启动Spring
容器加载Bean
配置的时候完成对变量的赋值行为,。
常用的两种注入方式
设值注入
构造注入
现在看设值注入的配置:
<?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" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<bean id="injectionService" class="com.xyf.InjectionServiceImpl">
<property name="injectionDAO" ref="injectionDao"></property>
</bean>
<bean id="injectionDao" class="com.xyf.InjectionDaoImpl"></bean>
</beans>
Spring 配置文件的根元素是来源于Spring beans命名空间所定义的<beans>
元素。
配置完毕后我们需要一个Spring容器来测试代码。Spring的容器有BeanFactory
和ApplicationContext
。
BeanFactory
作为最简单的容器提供了基础的依赖注入和Bean
装配服务。当我们需要更高级的框架服务时,选择ApplicationContext更合适
在例子代码中我们采用了ApplicationContext
来实现。
先实例化ApplicationContext
context = new ClassPathXmlApplicationContext(springXmlpath.split("[,\\s]+"));
context.start();
容器通过beanid 得到bean,调用方法。最后destroy方法回收容器内容。
附上 UnitTestBase的代码
package com.xyf;
import org.junit.After;
import org.junit.Before;
import org.springframework.beans.BeansException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;
public class UnitTestBase {
private ClassPathXmlApplicationContext context;
private String springXmlpath;
public UnitTestBase() {}
public UnitTestBase(String springXmlpath) {
this.springXmlpath = springXmlpath;
}
@Before
public void before() {
if (StringUtils.isEmpty(springXmlpath)) {
springXmlpath = "classpath*:spring-*.xml";
}
try {
context = new ClassPathXmlApplicationContext(springXmlpath.split("[,\\s]+"));
context.start();
} catch (BeansException e) {
e.printStackTrace();
}
}
@After
public void after() {
context.destroy();
}
@SuppressWarnings("unchecked")
protected <T extends Object> T getBean(String beanId) {
try {
return (T)context.getBean(beanId);
} catch (BeansException e) {
e.printStackTrace();
return null;
}
}
protected <T extends Object> T getBean(Class<T> clazz) {
try {
return context.getBean(clazz);
} catch (BeansException e) {
e.printStackTrace();
return null;
}
}
}
PS : 使用Junit测试spring项目
test目录只需要和main一样的目录就可以了
Junit项目会有一篇笔记专门介绍。
0 0
- IOC及Bean容器
- Spring--IOC及Bean容器
- 【慕课网Spring系列学习攻略】IOC及Bean容器篇
- Spring学习笔记(3.IOC及Bean容器)
- IoC容器中装配Bean
- Ioc容器装配Bean(2)
- IoC容器配置Bean(3)
- IoC容器装配Bean 上
- IoC容器装配Bean 下
- spring-IOC容器bean生命周期
- spring ioc 容器装配bean
- IOC 容器中Bean 注入
- 【Spring.Framework】【IoC】Spring容器,Bean及Bean的实例化
- 在IoC容器中装配Bean
- Spring 3 Ioc 容器装在bean
- Spring IoC容器--Bean的装配
- Spring框架学习【IoC容器解析Bean】
- Spring IOC容器bean初始化源码分析
- 关于Certificate、Provisioning Profile、App ID的介绍及其之间的关系
- 第11周项目1.2用函数输出星号图
- C++中#ifdef、#endif等宏的使用
- WPF学习(第九章)命令
- 蓝桥杯begin练习
- IOC及Bean容器
- Json & Gson
- crackme.chm之figugegl.1.exe
- 第十一周项目 6 回文,素数(反序数)
- glusterfs双副本原理解析和脑裂解决方案
- SAP ABAP编程 Ranges用法
- Javamail入门
- 第十一周上机项目4-阶乘
- 宋江