SpringBoot中@Configuration和@Component注解的区别

172 篇文章 6 订阅

使用

@Configuration和@Component都是使用于配置类上以代替XML文件中<beans>标签;
@Configuration是@Component的扩展,同样类似的扩展还有@Repository、@Service、@Controller、@RestController等等,而后面四个都是用于传统三层架构中使用的注解;
在被@Configuration注解的类中所有带有@Bean注解的方法都会被CGLib动态代理,而后每次调用这些方法时返回的都是第一次返回的实例;
被@Configuration标记的类不能是final类,不能是本地类、访问修饰符也不能是private。

测试区别

上面说了被@Configuration注解的类中所有带有@Bean注解的方法都会被CGLib动态代理并在第一次调用之后的每次调用时从BeanFactory中返回相同的实例,而@Component注解则不会。下面的例子展示了其区别:

编辑配置类

通过两个实体类测试,School属性包括(String)addr和(Student)student,Student属性有(String)name和(String)age:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

//选择配置注解

@Configuration

//@Component

public class TestConfiguration {

    //生成shool实例的方法

    @Bean

    public School school() {

        School school = new School();

        school.setAddr("");

        school.setStudent(student());   //这里调用student()新建一个对象给school

        return school;

    }

    //生成student实例的方法

    @Bean

    public Student student() {

        Student student = new Student();

        student.setName("xiaoming");

        student.setAge(18);

        return student;

    }

}

编辑控制层用于测试

分别通过注入方式实例化一个school和一个student对象,再判断school中的student对象与刚刚注入的student对象是否是一个对象:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

@RestController

@RequestMapping("/")

public class TestController {

    @Autowired

    private School school;  //注入school

    @Autowired

    private Student student;    //注入student

    @RequestMapping("/test")

    public String test() {

        if (school.getStudent() == student) {   //判断是否是同一个对象

            return "同一个student对象";

        } else {

            return "不同的student对象";

        }

    }

}

发送请求查看结果

修改注解为@Component

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

//选择配置注解

//@Configuration

@Component

public class TestConfiguration {

    //生成shool实例的方法

    @Bean

    public School school() {

        School school = new School();

        school.setAddr("");

        school.setStudent(student());   //这里调用student()新建一个对象给school

        return school;

    }

    //生成student实例的方法

    @Bean

    public Student student() {

        Student student = new Student();

        student.setName("xiaoming");

        student.setAge(18);

        return student;

    }

} 

发送请求再次查看结果

修改

如果想在@Component注解的类中实现每次返回相同的实例可通过@Autowired先将student注入到一个对象,之后在给school赋值的时候使用这个student对象:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

//选择配置注解

//@Configuration

@Component

public class TestConfiguration {

    @Autowired

    private Student stu;

    //生成shool实例的方法

    @Bean

    public School school() {

        School school = new School();

        school.setAddr("");

        school.setStudent(stu);   //这里使用注入的stu

        return school;

    }

    //生成student实例的方法

    @Bean

    public Student student() {

        Student student = new Student();

        student.setName("xiaoming");

        student.setAge(18);

        return student;

    }

}

继续查看结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值