spring在filter里面怎么获取注解方式定义的bean

2024-11-28 19:08:32
推荐回答(1个)
回答1:

1使用ApplicationContextAware实现类获取

/**
* 获取applicationContext
*/
@Service
public class ApplicationContextSupport implements ApplicationContextAware{

/**
* Spring 文实体.
*/
private static ApplicationContext applicationContext;

@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
// TODO Auto-generated method stub
ApplicationContextSupport.applicationContext = applicationContext;
}

/**
* 根据名称实体.
*
* @param name 实体名称
* @return the bean
*/
public static Object getBean(String name)
{
return applicationContext.getBean(name);
}

/**
* 根据类型实体.
*
* @param type 实体类型
* @return the bean
*/
public static Object getBean(Class type)
{
return applicationContext.getBean(type);
}
}

2使用
(QuestionaryService) ApplicationContextSupport.getBean("questionaryServiceImpl");
-