【Spring】@Component 和 @Configuration 对比

@Configuration

@Configuration
public class ComponentTest {

    @Bean
    public String test(){
        return new String("123");
    }

    @Bean
    public String test2(){
        return test();
    }
}

Test:

public class ComponentBeanTest extends BaseTest{

    @Resource
    String test;

    @Resource
    String test2;

    @Test
    public void test() {
        System.out.println(test);
        System.out.println(test2);
        System.out.println(test == test2);
    }
}

输出:

123
123
true

@Component

@Component
public class ComponentTest {

    @Bean
    public String test(){
        return new String("123");
    }

    @Bean
    public String test2(){
        return test();
    }
}

Test和@Configuration注解的相同
输出:

123
123
false

你可能感兴趣的:(杂七杂八,spring,java,后端)