V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
guyeuro
V2EX  ›  问与答

Spring 的 @Configuration 和 @Component 同时使用的问题

  •  
  •   guyeuro · Jul 28, 2017 · 5841 views
    This topic created in 3201 days ago, the information mentioned may be changed or developed.

    @Configuration 相当于一个 beans.xml 文件,也就是里面会有很多类 是一个类的集合

    而 @Component 是注解在一个类上

    我看到有的用法如下:

    @Configuration
    @Component
    public class RefConfig{
        。。。。
    }
    

    我认为这是为了让 component-scan 配置能够自动扫描到这个 @Configuration, 而不用在代码里硬写

    ApplicationContext context = new AnnotationConfigApplicationContext(RefConfig.class);
    

    但是有一点很奇怪,@Configuration 相当于一个 beans.xml 文件,也就是里面会有很多类,是一个类的集合,而 @Component 是注解在一个类上,而非类集合上,这个用法如何理解?

    3 replies    2017-07-28 15:49:55 +08:00
    zael
        1
    zael  
       Jul 28, 2017
    guyeuro
        2
    guyeuro  
    OP
       Jul 28, 2017
    @liygheart 喔,看了一下


    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Component
    public @interface Configuration {
    String value() default "";
    }

    这个 @Component 在这里起的作用是啥?
    zael
        3
    zael  
       Jul 28, 2017
    @guyeuro #2 @Component 啥用,它就啥用呗
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2363 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 10:32 · PVG 18:32 · LAX 03:32 · JFK 06:32
    ♥ Do have faith in what you're doing.