Spring学习笔记之IoC
Spring是指一个用于构造java应用程序的轻量级框架。Spring框架的核心基于“控制反转(Inversion of Control, IoC)”的原理。
IoC是一种将组件依赖关系的创建和管理置于程序外部的技术。

1、在依赖托拽中,依赖关系是根据需要从一个集中的注册处获取的。 如:
BeanFactory factory = new XmlBeanFactory(new FileSystemResource(config.getServletContext().getRealPath("") + "/WEB-INF/classes/spring/applicationContext.xml"));
AdvDao advDao = (AdvDao)factory.getBean("advDao");
而上下文配置依赖查找是在容器管理的资源中进行查找的。 如:
public void perFormLoopup(Container container) {
AdvDao advDao = container.getAdvDao("advDao");
}
2、构造器依赖注入是在组件的构造器处提供依赖关系的注入,这种组件声明一个构造器或者一组构造器从构造参数中获取依赖关系。 如:
public class TestJob implements TestInterface{
private String message;
public TestJob(){
}
public TestJob(String message){
this.message = message;
}
public String getMessage(){
return message;
}
@Override
public void print() {
System.out.println(getMessage());
System.out.println("This is TestJob!");
}
}
构造器依赖注入的bean配置:
<bean id="testJob" class="con.anne.test.TestJob">
<constructor-arg>
<value>This is a configurable message</value>
</constructor-arg>
</bean>
3、Setter注入: 如:
public class AdvDaoImpl implements AdvDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}
通过xml支持来配置setter依赖注入,需要在节点下指定。如:
<bean id="advDao" class="com.anne.dao.impl.AdvDaoImpl">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
4、对比构造器注入与setter注入: 一般推荐setter注入。 在使用组件之前就需要实例化存在依赖关系的类的时候,构造器注入特别有用。因为setter方法的调用次序是无法规定的,在使用组件之前,也许并非所有必要的setter方法都会被调用到。 在以下情况时,setter注入更具有优势: ① 对象需要配置的熟悉比较多。 ② 对象既有简单类型的依赖,也有与对象的依赖。 ③ 对象有可能位于具体继承体系中。 ④ 对象有多于一个统一类型的依赖关系。 ⑤ 对象必须提供运行时修改依赖关系的途径。
5、依赖注入容器的核心时Bean工厂。 Spring提供了两个主要的BeanFactory的实现,一个是DefaultListableBeanFactory,还有一个是XmlBeanFactory。 对于DefaultListableBeanFactory来说,会使用PropertiesBeanDefinitionReader从一个properties文件中读取Bean的定义信息。
public class BeanFactoryTest {
public static void main(String[] args) throws Exception {
BeanFactory factory = getBeanFactory();
TestJob testJob = (TestJob)factory.getBean("testJob");
testJob.print();
}
private static BeanFactory getBeanFactory() throws Exception {
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(factory);
Properties props = new Properties();
props.load(new FileInputStream("/config/beans.properties"));
reader.registerBeanDefinitions(props);
return factory;
}
}
而对于XmlBeanFactory来说,则使用XmlBeanDefinitionReader从XML文件中读取bean。 XmlBeanFactory派生于DefaultListableBeanFactory并简单的进行了扩展,使它能够通过XmlBeanDefinitionReader自动获取配置信息。
//use XmlBeanFactory
XmlBeanFactory xmlFactory = new XmlBeanFactory(new FileSystemResource("/config/beans.xml"));
6、需要注意的: ① 默认情况下,Spring中的所有bean都是单例的,Spring维护bean的唯一实例,对bean工厂的getBean()方法的每一次调用都返回同一个实例。 即:
<bean id="advDao" class="com.anne.dao.impl.AdvDaoImpl" singleton="true">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
把singleton="true"改为singleton="false",就可把单例转换为非单例。 ② Spring支持四种装配模式:通过命名,通过类型,构造器,自动检测。 配置bean的属性autowire="byName",那么Spring会将每个属性连接到一个同名的bean上。 ③ bean也可以继承属性。 给bean配置上parent="parentBean"即可。 Bean的继承不需要符合java的继承层次关系。应该把bean继承看作模板的功能,而不是实际的继承功能。