在使用
@Servicepublic class BeanFactoryHelper implements BeanFactoryAware {
private static BeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
public static Object getBean(String beanName){
if(beanFactory == null){
throw new NullPointerException("BeanFactory is null!");
}
return beanFactory.getBean(beanName); } }
还有一种方式是实现ApplicationContextAware接口,代码也很简单:
@Service
public class ApplicationContextHelper implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public static Object getBean(String beanName){
if(applicationContext == null){
throw new NullPointerException("ApplicationContext is null!");
}
return applicationContext.getBean(beanName);
}
}
上面两种方法,只有容器启动的时候,才会把BeanFactory 和 ApplicationContext 注入到自定义的helper类中,如果在本地junit测试的时候,如果需要根据bean的名称获取bean对象,则可以通过ClassPathXmlApplicationContext来获取一个ApplicationContext,代码如下:
@Test
public void test() throws SQLException {
//通过从classpath中加载spring-mybatis.xml实现bean的获取
ApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
IUserService userService = (IUserService) context.getBean("userService");
User user = new User();
user.setName("test");
user.setAge(20);
userService.addUser(user);
}
总结
以上就是小编分享给大家的Java编程实现Aware接口自定义获取bean的两种方式的全部内容,希望对大家有所帮助。如果有什么问题,可以留言,小编会及时回复大家。感谢大家对本站的支持!