[英]how to implement @bean like Spring
@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter {
@Bean
AuthorizeInterceptor authorizelInterceptor() {
return new AuthorizeInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(authorizelInterceptor()).addPathPatterns("/user/**");
super.addInterceptors(registry);
}
}
我认为@Bean
将放入new AuthorizeInterceptor();
进入IOC,并在方法addInterceptors()
调用authorizelInterceptor()
将获得在IOC中注册的bean。 如果使用代理,则addInterceptors()
调用的方法authorizelInterceptor()
addInterceptors()
将不进行代理。
@Bean是方法级别的注释,它仅向Spring容器提供Bean配置,并且容器使用它来注入相应的依赖项。 简而言之,它只是使用xml <bean/>
标记定义bean的替代方法。
我通常在编写简单的单元测试时使用@Bean,以在同一Test类文件中提供bean定义(而不是为bean配置定义单独的xml)。
我建议您通过以下链接获取更多详细信息:
http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch02s02.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.