@Component

    1. Application ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    2. Service名称 变量 = ctx.getBean(Service名称.class);
    3. System.out,println("变量名称");
    1. @Component
    2. public class BookDaoImpl implements BookDao{
    3. }

    或者

    1. @Component("bookDao")//可以理解为id
    2. public class BookDaoImpl implements BookDao{
    3. }

    核心配置文件中通过组件的扫描加载bean

    1. <context:component-scan base-package="com.chentianyu" />
    • @Component注解的三个衍生注解
      • @Controller: 用于表现层的bean定义
      • @Service: 用于业务层的bean定义
      • @Repository: 用于数据层bean定义

    这三个都是跟@Component功能一致,只是用来区分三层的区分