Spring ApplicationContextAware获取上下文
来源:互联网 发布:游戏俱乐部书生数据 编辑:程序博客网 时间:2024/05/16 16:35
一、ApplicationContextAware 用处
Spring 提供了ApplicationContextAware类,通过它可以获取所有bean上下文。
二、怎么用?
①、定义一个工具类,去实现 ApplicationContextAware,实现 setApplicationContext方法即可
public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; // Spring应用上下文环境 @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringContextUtil.applicationContext = applicationContext; } /** * 获取对象 * * @param name * @return Object 一个以所给名字注册的bean的实例 * @throws BeansException */ public static Object getBean(String name) throws BeansException { return applicationContext.getBean(name); }}
②、业务代码
public interface UserReadService { public UserInfo getUserInfoById(Long id);}@Component("userReadService")public class UserReadServiceImpl implements UserReadService { @Override public UserInfo getUserInfoById(Long id) { System.out.println("获取用户信息"); return null; }}
③、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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:annotation-config /> <!--自动扫描含有@Service将其注入为bean --> <context:component-scan base-package="com.mycompany.yuanmeng.springdemo.aop" /> <bean id="springContextUtil" class="com.mycompany.yuanmeng.springdemo.aop.SpringContextUtil" /></beans>
④、test
public static void test() { new ClassPathXmlApplicationContext("spring.xml"); // 加载ApplicationContext(模拟启动web服务), 当然如果是用web工程的话,可以直接在web.xml配置,容器会去加载此文件。 UserReadService userReadService = (UserReadService) SpringContextUtil.getBean("userReadService"); userReadService.getUserInfoById(null); }
0 0
- ApplicationContextAware 获取Spring 上下文
- Spring ApplicationContextAware获取上下文
- Spring ApplicationContextAware获取上下文
- ApplicationContextAware获取上下文bean
- 程序运行过程中获取Spring上下文通过ApplicationContextAware
- 通过ApplicationContextAware加载Spring上下文环境(获取bean)
- springboot 获取上下文代码 ApplicationContextAware
- 加载Spring上下文环境ApplicationContextAware
- ApplicationContextAware加载Spring上下文环境
- Spring ApplicationContextAware 获取bean
- ApplicationContextAware获取Spring上下文,通过基类获取properties中的配置属性
- 通过ApplicationContextAware加载Spring上下文环境
- 通过ApplicationContextAware加载Spring上下文环境
- 通过ApplicationContextAware加载Spring上下文环境
- 通过ApplicationContextAware加载Spring上下文环境
- 通过ApplicationContextAware加载Spring上下文环境
- 通过ApplicationContextAware加载Spring上下文环境
- ApplicationContextAware动态获取spring bean
- chrome插件
- spring jdbcTemplate源码剖析
- 求最大公约数最小公倍数
- 搭建maven+spring+mybatis工程
- Spring ProxyFactory
- Spring ApplicationContextAware获取上下文
- operator重写
- 第三十一、Java面向对象三大特征之多态
- spring aop 的一个demo(未完,待完善)
- spring InitializingBean接口
- java定时任务接口ScheduledExecutorService
- ThymeLeaf通过模板转换对象(根据模板和map生成)
- svn 常用命令
- eclipse jetty启动内存溢出