首页 > 代码库 > 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)

跟着刚哥学习Spring框架--通过注解方式配置Bean(四)

组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件。

特定组件包括:

  1、@Component:基本注解,识别一个受Spring管理的组件

  2、@Respository:标识持久层组件

  3、@Service:标识业务层组件

  4、@Controller:标识表现层组件 

Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写. 也可以在注解中通过 value 属性值标识组件的名称

当在组件类上使用了特定的注解之后, 还需要在 Spring 的配置文件中声明<context:component-scan>
  1、base-package 属性指定一个需要扫描的基类包,Spring 容器将会扫描这个基类包里及其子包中的所有类.
  2、当需要扫描多个包时, 可以使用逗号分隔.
  3、<context:include-filter /><context:exclude-filter />

 1 <!-- 扫描@Controller注解 --> 2 <context:component-scan base-package="com.hzg.controller" use-default-filters="false"> 3     <context:include-filter type="annotation"expression="org.springframework.stereotype.Controller" /> 4 </context:component-scan> 5  6 <!-- 配置扫描注解,不扫描@Controller注解 --> 7 <context:component-scan base-package="com.hzg.controller"> 8     <context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller" /> 9 </context:component-scan>

  当使用<context:include-filter />的时候,在<context:component-scan >里必须加上use-default-filters="false",否则不起作用。

  其中属性expression的值不是你的包所在位置,别搞错了,它是你注解的具体类地址。

实例:

  创建包com.hzg.anotation

  创建包com.hzg.anotation.controller

  创建UserController类

 1 @Controller 2 public class UserController { 3  4     @Autowired(required = false) 5     private UserService userService; 6     //@Autowired也可以放在setter方法上,那就去掉上面的Autowired注解 7     public void setUserService(UserService userService) { 8         this.userService = userService; 9     }10 11     public void excute(){12         System.out.println("UserController excute");13         userService.diao();14     }15 }

  创建包com.hzg.anotation.service

  创建UserService类

 1 @Service 2 public class UserService { 3   4      @Autowired 5      private UserRepostory userRepostory; 6      public void diao(){ 7          System.out.println("UserService diao"); 8          userRepostory.save(); 9      }10  }

  创建包com.hzg.anotation.repostory

  创建UserRepostory接口

1 public interface UserRepostory {2     void save();3 }

  创建UserRepostoryImlp类

1 @Repository("userRepostory")2 public class UserRepostoryImlp implements UserRepostory {3 4     @Override5     public void save() {6         System.out.println("UserRepostory save");7     }8 }

  创建configautowire.xml文件

1 <context:component-scan base-package="com.hzg.anotation"></context:component-scan>

  Main方法

1 public static void main(String[] args) {2  ApplicationContext ctx = new ClassPathXmlApplicationContext("configautowire.xml");3  UserController userController = (UserController) ctx.getBean("userController");4  userController.excute();5 }

  输出接口:

UserController excuteUserService diaoUserRepostory save

 

其中:

  1、@Autowired(required = false)中required = false的意思是:如果没有这个类的实例化,那么会赋值成NULL,而不是报错。

  2、@Autowired注解可以为成员变量、方法、构造函数赋值。

  3、@Repository("userRepostory")等同于@Repository(value = "http://www.mamicode.com/userRepostory"),value是默认值,代表给这个Bean

     赋值了id的值,防止有重复的Bean。

  4、如果在UserService类的@Autowired下面使用限定修饰符@Qualifier("userRepostoryImlp"),那么

     @Repository("userRepostory")必须写成@Repository或者写成@Repository("userRepostoryImlp"),否则就有歧义了。

------------------------------------------------------------------------------------------------------------------------

跟着刚哥学习Spring框架系列:

跟着刚哥学习Spring框架--创建HelloWorld项目(一)

跟着刚哥学习Spring框架--Spring容器(二)

跟着刚哥学习Spring框架--通过XML方式配置Bean(三)

跟着刚哥学习Spring框架--通过注解方式配置Bean(四)

跟着刚哥学习Spring框架--AOP(五)

 

跟着刚哥学习Spring框架--通过注解方式配置Bean(四)