当一个类实现了ApplicationContextAware接口之后,这个类就可以方便获得ApplicationContext中的所有bean。换句话说,就是这个类可以直接获取spring配置文件中,所有有引用到的bean对象。
在Spring配置文件中
实现setApplicationContext()方法
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringBeanUtils.applicationContext = applicationContext;
}
非web项目中初始化spring容器、加载配置文件
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { “spring/spring-context.xml” });
context.start();
这样在普通java类中就可以从ioc容器获取bean了。方法如下
Test test=context.getBean(“test”);
Filter Interceptor Listener在Spring MVC中的使用
@Resource,它是javax.annotation.Resource; 这个包中,也就是说是javaEE中的,并不是spring中的
而且@Resource(“xxx”) 是可以定义bean名称的,就是说我这个属性要用那个bean来赋值。
@Autowired,它是org.springframework.beans.factory.annotation.Autowired 是这个包中,它是spring的包。
而且它没有@Autowired(“xxx”),那我要为这个bean定义名称怎么办这个时候可以用@Qualifier(“xxx”) 这个也是spring中的。
为什么这段代码不仅仅扫描@Controller注解的Bean,而且还扫描了@Component的子注解@Service、@Reposity。因为use-default-filters默认为true。
此时指定的include-filter没有起到作用,只要把use-default-filter设置成false就可以了。
在Spring框架中,可以通过ref来互相引用相同或不同xml配置文件中定义的Bean。
引用不同xml配置文件中的bean
格式:引用相同xml配置文件中的bean
格式:
是简化的1类型注入。
此种注入方式是单例模式注入,即只new一次securityManager对象,每一次注入的对象都是同一个securityManager对象。