注解开发定义bean

  1.  使用@Component在要配置的bean类前添加注解。  注:括号相当于bean id可以省略

    @Component("bookDao")

    public class BookDaoImpl implements BookDao

    {

 

    }

  2.  核心配置文件添加扫描配置

    <context:component-scan base-package="com.feiyan"/>

@Component注解的三个衍生注解  注:没有什么特殊作用,只是为了区分一下而已

  1.  @Controller  用于表现层bean定义

  2.  @Service  用于业务层bean定义

  3.  @Repository  用于数据层bean定义

纯注解开发

  1.  使用java类代替核心配置文件

    @Configuration

    @ComponentScan("com.feiyan")

    public class SpringConfig

    {

    }

    ¤  @Configuration注解用于设定当前类为配置类

    ¤  @ComponentScan()注解用于设定扫描路径,只能添加一次,多路径使用数组。例:@ComponentScan({"com.feiyan", "com.feiyan2"})

  2.  获取容器方式更改为从类获取

    ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);

@ComponentScan("com.feiyan")