简体   繁体   中英

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);
    }
}

I think @Bean will put the new AuthorizeInterceptor(); into IOC and in method addInterceptors() call authorizelInterceptor() will get the bean registered in IOC. if use proxy, method authorizelInterceptor() called in addInterceptors() will not do proxy.

@Bean is a method level annotation which simply provides the bean configuration to the Spring container and the container uses it to inject the respective dependency. In short,it is just alternative to defining the bean using xml <bean/> tags.

I generally use @Bean while writing the simple unit tests, to provide the bean definitions in the same Test class file (rather than defining a separate xml for bean configurations).

I recommend you to go through the below link for more details:

http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch02s02.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM