[java][spring]取得通过配置文件和注解注入的bean、取得当前数据库连接、取得ApplicationContext的方法
2014-04-12 16:01
1106 查看
[spring]取得通过配置文件和注解注入的bean、取得当前数据库连接、取得ApplicationContext的方法
1、思路:
应用程序启动时使用ApplicationContextAware让Spring容器传递自己生成的ApplicationContext给我们, 然后我们把这个ApplicationContext设置成一个类的静态变量, 这样我们就随时都可以在老的代码里得到Application的对象了。
2、编写工具类:ApplicationContextHelper
3、在Spring的配置文件中配置上述类后、应用程序启动时会使用ApplicationContextAware让Spring容器传递自己生成的ApplicationContext给我们, 然后我们把这个ApplicationContext设置成一个类的静态变量, 这样我们就随时都可以在老的代码里得到Application的对象了。
4、获得bean的方法、获取数据库连接的方法、获取ApplicationContext
4.1、取得通过Spring配置文件注入的任意Bean(以取得DataSource为例)
4.2、取得通过注解方式注入的Bean(以在取得Service为例)
通过@Service("userTestService")定义bean
通过以下代码获取Bean
1、思路:
应用程序启动时使用ApplicationContextAware让Spring容器传递自己生成的ApplicationContext给我们, 然后我们把这个ApplicationContext设置成一个类的静态变量, 这样我们就随时都可以在老的代码里得到Application的对象了。
2、编写工具类:ApplicationContextHelper
/** * 版权所有:华信软件 * 项目名称:ACWS框架 * 创建者: Wangdf * 创建日期: 2014-4-2 * 文件说明: Spring工具栏 */ package com.huaxin.framework.core.util; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * Spring工具栏 * @author wangdf */ public class ApplicationContextHelper implements ApplicationContextAware { private static ApplicationContext appCtx; /** * 此方法可以把ApplicationContext对象inject到当前类中作为一个静态成员变量。 * @param applicationContext ApplicationContext 对象. * @throws BeansException * @author wangdf */ @Override public void setApplicationContext( ApplicationContext applicationContext ) throws BeansException { appCtx = applicationContext; } /** * 获取ApplicationContext * @return * @author wangdf */ public static ApplicationContext getApplicationContext(){ return appCtx; } /** * 这是一个便利的方法,帮助我们快速得到一个BEAN * @param beanName bean的名字 * @return 返回一个bean对象 * @author wangdf */ public static Object getBean( String beanName ) { return appCtx.getBean( beanName ); } }
3、在Spring的配置文件中配置上述类后、应用程序启动时会使用ApplicationContextAware让Spring容器传递自己生成的ApplicationContext给我们, 然后我们把这个ApplicationContext设置成一个类的静态变量, 这样我们就随时都可以在老的代码里得到Application的对象了。
<bean id="SpringApplicationContext" class="com.huaxin.framework.core.util.ApplicationContextHelper"></bean>
4、获得bean的方法、获取数据库连接的方法、获取ApplicationContext
4.1、取得通过Spring配置文件注入的任意Bean(以取得DataSource为例)
<!-- 配置dbcp数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method='init' destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="minIdle" value="${jdbc.minIdle}" /> <!-- 队列中的最小等待数 --> <property name="maxWait" value="${jdbc.maxWait}" /> <!-- 最长等待时间,单位毫秒 --> <property name="maxActive" value="${jdbc.maxActive}" /> <!-- 最大活跃数 --> <property name="initialSize" value="${jdbc.initialSize}" /><!-- 初始大小 --> </bean>
//取得数据源 DruidDataSource dataSource = (DruidDataSource)ApplicationContextHelper.getBean("dataSource"); Connection connection = DataSourceUtils.getConnection(dataSource); //取得ApplicationContext ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
4.2、取得通过注解方式注入的Bean(以在取得Service为例)
通过@Service("userTestService")定义bean
@Service("userTestService") public class UserTestServiceImpl implements UserTestService { @Autowired private UserTestDao userDao; @Transactional(propagation=Propagation.NOT_SUPPORTED) @Override public Map<String, Object> doGetRecordById() { return userDao.getRecordById("1"); } 。。。略。。。 }
通过以下代码获取Bean
/** * 测试Bean获取 * @throws Exception */ @Test public void testGetBean() throws Exception{ //通过注解方式 UserTestService userTestService = (UserTestService)ApplicationContextHelper.getBean("userTestService"); System.out.println(userTestService); }
相关文章推荐
- [java][spring]取得通过配置文件和注解注入的bean、取得当前数据库连接、取得ApplicationContext的方法
- [java][spring]取得通过配置文件和注解注入的bean、取得当前数据库连接、取得Appli
- Spring中c3p0连接池的配置 及JdbcTemplate的使用 通过XML配置文件注入各种需要对象的操作 来完成数据库添加Add()方法
- spring学习(四)—属性注入的四种方法(通过配置文件)
- C#连接数据库的新方法(通过web.config配置文件)
- C#连接数据库的新方法(通过web.config配置文件)
- java通过配置文件连接数据库
- Spring零配置通过注解实现Bean依赖注入总结<转>
- C#连接数据库的新方法(通过web.config配置文件)
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- Spring通过ApplicationContextAware获得bean的方法、获取数据库连接的方法、获取Appl...
- ADO.NET 连接数据库的另一种方法(通过配置文件和基类转化,适用于后期维护)
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- Spring AOP注解通过@Autowired,@Resource(J2EE提供),@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法
- Spring通过配置文件注入bean
- 原始方式:spring四种依赖注入方式 |注解方式:Spring零配置通过注解实现Bean依赖注入
- Spring 全注解配置 bean 和 调用 (7) properties文件注入
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- 【java基础:JDBC】通过读取配置文件进行数据库连接并读取数据的Demostration