spring学习总结(八):IOC & DI 配置Bean之注解配置

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/yuchao2015/article/details/53366796

IOC & DI 配置Bean之注解配置


Bean之注解配置

在 classpath 中扫描组件

  • 组件扫描(component scanning):  Spring 能够从 classpath 下自动扫描, 侦测和实例化具有特定注解的组件. 
  • 特定组件包括:
  1. @Component: 基本注解, 标识了一个受 Spring 管理的组件
  2. @Respository: 标识持久层组件
  3. @Service: 标识服务层(业务层)组件
  4. @Controller: 标识表现层组件
  • 对于扫描到的组件, Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写. 也可以在注解中通过 value 属性值标识组件的名称
  • 当在组件类上使用了特定的注解之后, 还需要在 Spring 的配置文件中声明 <context:component-scan>

目录结构:


实测:

TestObject.java

    
    
  1. /**
  2. *
  3. * @ClassName: TestObject
  4. * @Description:测试bean的注解配置形式
  5. * @author: xyc
  6. * @date: 2016年11月27日 下午8:40:12
  7. *
  8. */
  9. @Component
  10. public class TestObject {
  11. public void test(){
  12. System.out.println( “test…”);
  13. }
  14. }
UserRespository.java

    
    
  1. /**
  2. *
  3. * @ClassName: UserRespository
  4. * @Description: 持久层接口
  5. * @author: xyc
  6. * @date: 2016年11月27日 下午8:43:57
  7. *
  8. */
  9. public interface UserRespository {
  10. public void save();
  11. }
UserRespositoryImpl.java

    
    
  1. /**
  2. *
  3. * @ClassName: UserRespositoryImpl
  4. * @Description:持久层实现类
  5. * @author: xyc
  6. * @date: 2016年11月27日 下午8:44:17
  7. *
  8. */
  9. @Repository( “userRespository”)
  10. public class UserRespositoryImpl implements UserRespository{
  11. @Override
  12. public void save() {
  13. System.out.println( “UserRespositoryImpl save…”);
  14. }
  15. }
UserService.java

    
    
  1. /**
  2. *
  3. * @ClassName: UserService
  4. * @Description:业务层实现类
  5. * @author: xyc
  6. * @date: 2016年11月27日 下午8:44:35
  7. *
  8. */
  9. @Service
  10. public class UserService {
  11. public void add(){
  12. System.out.println( “UserService add…”);
  13. }
  14. }
UserController.java

    
    
  1. /**
  2. *
  3. * @ClassName: UserController
  4. * @Description: 控制层
  5. * @author: xyc
  6. * @date: 2016年11月27日 下午8:45:42
  7. *
  8. */
  9. @Controller
  10. public class UserController {
  11. public void execute(){
  12. System.out.println( “UserController execute…”);
  13. }
  14. }
bean-annotation.xml

    
    
  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans xmlns=“http://www.springframework.org/schema/beans”
  3. xmlns:xsi= “http://www.w3.org/2001/XMLSchema-instance”
  4. xmlns:context= “http://www.springframework.org/schema/context”
  5. xsi:schemaLocation= ”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd”>
  7. <!– 配置全局扫描包,指定spring IOC容器扫描的包–>
  8. <context:component-scan base-package=“com.xyc.spring.annotation”> </context:component-scan>
  9. </beans>

配置之后,可以看到所有.java上面就有了一个S,表示加入了IOC容器中

测试一下:
ApplicationAnnotation.java

    
    
  1. /**
  2. *
  3. * @ClassName: ApplicationAnnotation
  4. * @Description:测试利用注解方式配置bean
  5. * @author: xyc
  6. * @date: 2016年11月27日 下午9:19:22
  7. *
  8. */
  9. public class ApplicationAnnotation {
  10. public static void main(String[] args) {
  11. ApplicationContext context = new ClassPathXmlApplicationContext( “bean-annotation.xml”);
  12. UserController userController = (UserController) context.getBean( “userController”);
  13. System.out.println(userController);
  14. UserService userService = (UserService) context.getBean( “userService”);
  15. System.out.println(userService);
  16. UserRespository userRespository = (UserRespository) context.getBean( “userRespository”);
  17. System.out.println(userRespository);
  18. TestObject testObject = (TestObject) context.getBean( “testObject”);
  19. System.out.println(testObject);
  20. }
  21. }

测试结果:


可以看到,IOC容器中的bean都被加载到了.

<context:component-scan>属性说明:

  • base-package 属性指定一个需要扫描的基类包,Spring 容器将会扫描这个基类包里及其子包中的所有类. 
  • 当需要扫描多个包时, 可以使用逗号分隔.
  • 如果仅希望扫描特定的类而非基包下的所有类,可使用resource-pattern 属性过滤特定的类,示例:

    
    
  1. <!– 指定spring IOC容器扫描的包 可以通过resource-pattern 指定扫描的资源,如下只扫描servcie层下面的 –>
  2. <context:component-scan base-package=“com.xyc.spring.annotation” resource-pattern=“service/*.class”> </context:component-scan>

<context:component-scan>子节点说明:
  • <context:include-filter> 子节点表示要包含的目标类
  • <context:exclude-filter> 子节点表示要排除在外的目标类
  • <context:component-scan> 下可以拥有若干个 <context:include-filter> 和 <context:exclude-filter> 子节点
<context:include-filter> 和 <context:exclude-filter> 子节点支持多种类型的过滤表达式:

通过annotaion方式过滤 exclude-filter


    
    
  1. <!– context:exclude-filter 子节点指定排除那些指定表达式的组件 如下排除持久层–>
  2. <context:component-scan base-package=“com.xyc.spring.annotation”>
  3. <context:exclude-filter type=“annotation” expression=“org.springframework.stereotype.Repository”/>
  4. </context:component-scan>

通过annotaion方式过滤 include-filter


     
     
  1. <!– context:include-filter 子节点指定包含哪些指定表达式的组件 如下只扫描持久层,但是该子节点需要use-default-filters 配合使用
  2. use-default-filters :默认true,扫描所有 设置为false,不要走默认
  3. –>
  4. <context:component-scan base-package=“com.xyc.spring.annotation” use-default-filters=“false”>
  5. <context:include-filter type=“annotation” expression=“org.springframework.stereotype.Repository”/>
  6. </context:component-scan>

通过assignable过滤 exclude-filter


    
    
  1. <!-- 通过类名的方式过滤 exclude-filter -->
  2. <context:component-scan base-package="com.xyc.spring.annotation">
  3. <context:exclude-filter type="assignable" expression="com.xyc.spring.annotation.service.UserService"/>
  4. </context:component-scan>

通过assignable过滤 include-filter


     
     
  1. <!– 通过类名的方式过滤 include-filter –>
  2. <context:component-scan base-package=“com.xyc.spring.annotation” use-default-filters=“false”>
  3. <context:include-filter type=“assignable” expression=“com.xyc.spring.annotation.service.UserService”/>
  4. </context:component-scan>

组件装配

使用 @Autowired 自动装配 Bean

<context:component-scan> 元素还会自动注册 AutowiredAnnotationBeanPostProcessor 实例, 该实例可以自动装配具有 @Autowired 和 @Resource 、@Inject注解的属性.
@Autowired 注解自动装配具有兼容类型的单个 Bean属性
  • 构造器, 普通字段(即使是非 public), 一切具有参数的方法都可以应用@Authwired 注解
  • 默认情况下, 所有使用 @Authwired 注解的属性都需要被设置. 当 Spring 找不到匹配的 Bean 装配属性时, 会抛出异常, 若某一属性允许不被设置, 可以设置 @Authwired 注解的 required 属性为 false
  • 默认情况下, 当 IOC 容器里存在多个类型兼容的 Bean 时, 通过类型的自动装配将无法工作. 此时可以在 @Qualifier 注解里提供 Bean 的名称. Spring 允许对方法的入参标注 @Qualifiter 已指定注入 Bean 的名称
  •  @Authwired 注解也可以应用在数组类型的属性上, 此时 Spring 将会把所有匹配的 Bean 进行自动装配.
  • @Authwired 注解也可以应用在集合属性上, 此时 Spring 读取该集合的类型信息, 然后自动装配所有与之兼容的 Bean. 
  • @Authwired 注解用在 java.util.Map 上时, 若该 Map 的键值为 String, 那么 Spring 将自动装配与之 Map 值类型兼容的 Bean, 此时 Bean 的名称作为键值

@Authwired注解说明:

如下:去掉Component注解,则spring IOC容器就不存在这个bean

    
    
  1. /**
  2. *
  3. * @ClassName: TestObject
  4. * @Description:测试bean的注解配置形式
  5. * @author: xyc
  6. * @date: 2016年11月27日 下午8:40:12
  7. *
  8. */
  9. /*@Component*/
  10. public class TestObject {
  11. public void test(){
  12. System.out.println( “test…”);
  13. }
  14. }

在这边注入的话,就会出错,如果加上required =false 表示 IOC容器中有的话就注入,没有就不注入


    
    
  1. /*
  2. * @ClassName: UserRespositoryImpl
  3. * @Description:持久层实现类
  4. * @author: xyc
  5. * @date: 2016年11月27日 下午8:44:17
  6. /
  7. @Repository( "userRespository")
  8. public class UserRespositoryImpl implements UserRespository{
  9. @Autowired(required= false)
  10. private TestObject testObject;
  11. @Override
  12. public void save() {
  13. System.out.println( "UserRespositoryImpl save...");
  14. //testObject.test();
  15. }
  16. }


@Qualifier注解说明:

添加类同样实现UserRespository接口,接口下有多个实现类

    
    
  1. @Repository
  2. public class TestRespositoryImpl implements UserRespository{
  3. @Override
  4. public void save() {
  5. System.out.println( “TestRespositoryImpl save…”);
  6. }
  7. }



    
    
  1. /**
  2. *
  3. * @ClassName: UserRespositoryImpl
  4. * @Description:持久层实现类
  5. * @author: xyc
  6. * @date: 2016年11月27日 下午8:44:17
  7. *
  8. */
  9. @Repository
  10. public class UserRespositoryImpl implements UserRespository{
  11. @Autowired(required= false)
  12. private TestObject testObject;
  13. @Override
  14. public void save() {
  15. System.out.println( “UserRespositoryImpl save…”);
  16. //testObject.test();
  17. }
  18. }

在service层通过@qualifier指定注入哪个bean,这个是第一种方式


    
    
  1. @Service
  2. public class UserService {
  3. @Autowired
  4. @Qualifier( "testRespositoryImpl")
  5. private UserRespository userRespository;
  6. public void add(){
  7. System.out.println( "UserService add...");
  8. userRespository.save();
  9. }
  10. }

第二种方式指定给@Repository指定bean的名称.

@Autowired
 private UserRespository bean的名称;

使用 @Resource 或 @Inject 自动装配 Bean

  • Spring 还支持 @Resource 和 @Inject 注解,这两个注解和 @Autowired 注解的功用类似
  • @Resource 注解要求提供一个 Bean 名称的属性,若该属性为空,则自动采用标注处的变量或方法名作为 Bean 的名称
  • @Inject 和 @Autowired 注解一样也是按类型匹配注入的 Bean, 但没有 reqired 属性
  • 建议使用 @Autowired 注解
        </div>
            </div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值