环境:
spring 注解
版本3.1
需求:
自定义的类中获取注解的bean
实现:
1.实现ApplicationContextAware方法:
@Component
public class SpringBeanUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
applicationContext = arg0;
}
public static <T> T getBean(String id, Class<T> clasz) {
return applicationContext.getBean(id, clasz);
}
}
2.直接获取:
KeyProperties instance=SpringBeanUtils.getBean("com.linkrmb.KeyProperties",
KeyProperties.class);
本文详细介绍了如何在Spring框架下通过实现ApplicationContextAware接口和自定义方法来获取自定义类中带有特定注解的bean,提供了一个灵活的依赖注入解决方案。
1867

被折叠的 条评论
为什么被折叠?



