您的位置:首页 > 编程语言 > Java开发

Spring BeanFactory

2016-05-27 20:32 411 查看
控制反转是Spring中的重要概念,它的先进之处在于对象交给容器控制,而不是传统的在你的对象内部直接控制。由容器来操控对象之间的依赖关系,摒弃直接由具体代码控制的传统模式,降低业务对象之间的依赖程度。

所有可以被Spring实例化并管理的Java类都可以被看做Bean。Bean是Spring的重要组成部分,不同的Bean之间的依赖关系在Spring的配置文件中界定,Spring使用BeanFactory来实例化、配置和管理对象。BeanFactory作为Bean工厂管理不同类型的Java对象。

<?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: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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
<!-- 配置声明式事务管理(采用注解的方式) -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>


// 测试SessionFactory
@Test
public void testSessionFactory() throws Exception {
SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory");
System.out.println(sessionFactory);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: