Difference between Spring annotations

前端 未结 5 1523
说谎
说谎 2020-12-04 11:08

Questions:

1) Difference between @Component and @Configuration?

I have read that both remove the necessity of

5条回答
  •  佛祖请我去吃肉
    2020-12-04 11:48

    In most the answers above, users suggest to say that @Component and @ Configuration serve different purposes. But I do not see it happening in reality.

    But I have a simple Spring MVC application.

    @Configuration
        public class SpringConfiguration {
    
    @Bean
    public  InternalResourceViewResolver initViewResolver(){
        InternalResourceViewResolver x = new InternalResourceViewResolver();
        x.setPrefix("/WEB-INF/jsp/");
        x.setSuffix(".jsp");
        return x;
    }
    
    }
    

    This main class works fine even if it is annotated as @Component instead of @Configuration.

    Similarly inside a class annotated as @Component if you have methods annotated with @Bean, those beans are created when context is loaed.

    So I think, it is just for code readability that we should mark main configuration class as @Configuration and other classes with @Component. Actual execution wise there seems no difference.

提交回复
热议问题