zoukankan      html  css  js  c++  java
  • [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

    内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

    本人互联网技术爱好者,互联网技术发烧友

    微博:伊直都在0221

    QQ:951226918

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

    1.在 classpath 中扫描组件

      1)组件扫描(component scanning): Spring 能够从 classpath 下自动扫描, 侦测和实例化具有特定注解的组件.

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

        例如:UserOperator --> userOperator 

       3)特定组件包括:

        ① @Component: 基本注解, 标识了一个受 Spring 管理的组件

        ② @Respository: 标识持久层组件

        ③ @Service: 标识服务层(业务层)组件

          ④ @Controller: 标识表现层组件

      4)当在组件类上使用了特定的注解之后, 还需要在 Spring 的配置文件中声明 <context:component-scan>

        ①  base-package 属性指定一个需要扫描的基类包Spring 容器将会扫描这个基类包里及其子包中的所有类.

        ②  当需要扫描多个包时, 可以使用逗号分隔.

        ③  如果仅希望扫描特定的类而非基包下的所有类,可使用 resource-pattern 属性过滤特定的类,示例:

           

                

        ④ <context:include-filter> 子节点表示要包含的目标类

        ⑤ <context:exclude-filter> 子节点表示要排除在外的目标类

        ⑥ <context:component-scan> 下可以拥有若干个 <context:include-filter> 和 <context:exclude-filter> 子节点

             

    2.注解配置bean的关联关系:<context:component-scan> 元素还会自动注册 AutowiredAnnotationBeanPostProcessor 实例, 该实例可以自动装配具有 @Autowired 和 @Resource 、@Inject注解的属性.

      1)使用 @Autowired 自动装配 Bean

        ①  @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 的名称作为键值

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

         ① Spring 还支持 @Resource 和 @Inject 注解,这两个注解和 @Autowired 注解的功用类似

         ② @Resource 注解要求提供一个 Bean 名称的属性,若该属性为空,则自动采用标注处的变量或方法名作为 Bean 的名称

         ③ @Inject 和 @Autowired 注解一样也是按类型匹配注入的 Bean, 但没有 reqired 属性

         ④ 建议使用 @Autowired 注解

    3.代码结构

     

    TestObject.java

    1 package com.jason.spring.beans.annotation;
    2 
    3 import org.springframework.stereotype.Component;
    4 
    5 @Component
    6 public class TestObject {
    7 
    8 }

    UserService.java

     1 package com.jason.spring.beans.annotation.service;
     2 
     3 import org.springframework.beans.factory.annotation.Autowired;
     4 import org.springframework.stereotype.Service;
     5 
     6 import com.jason.spring.beans.annotation.repository.UserRepository;
     7 
     8 @Service
     9 public class UserService {
    10 
    11     @Autowired
    12     private UserRepository userRepository;
    13 
    14     public void add() {
    15         System.out.println("UserService add ...");
    16         userRepository.save();
    17     }
    18 
    19 }

    UserController.java

     1 package com.jason.spring.beans.annotation.controller;
     2 
     3 import org.springframework.beans.factory.annotation.Autowired;
     4 import org.springframework.stereotype.Controller;
     5 
     6 import com.jason.spring.beans.annotation.service.UserService;
     7 
     8 @Controller
     9 public class UserController {
    10 
    11     @Autowired
    12     private UserService userService;
    13 
    14     public void execute() {
    15         System.out.println("UserController execute ...");
    16         userService.add();
    17     }
    18 
    19 }

    UserRepositoty.java

    1 package com.jason.spring.beans.annotation.repository;
    2 
    3 public interface UserRepository {
    4     void save();
    5 }

     

    UserRepositoryImpl.java

     1 package com.jason.spring.beans.annotation.repository;
     2 
     3 import org.springframework.stereotype.Repository;
     4 
     5 
     6 @Repository("userRepositoryImpl")
     7 public class UserRepositoryImpl implements UserRepository{
     8 
     9     @Override
    10     public void save() {
    11         System.out.println("added ! ! !");
    12         
    13     }
    14 
    15 }

     Main.java

     1 package com.jason.spring.beans.annotation;
     2 
     3 import org.springframework.context.ApplicationContext;
     4 import org.springframework.context.support.ClassPathXmlApplicationContext;
     5 
     6 import com.jason.spring.beans.annotation.controller.UserController;
     7 import com.jason.spring.beans.annotation.repository.UserRepositoryImpl;
     8 import com.jason.spring.beans.annotation.service.UserService;
     9 
    10 public class Main {
    11     
    12     public static void main(String[] args) {
    13         
    14         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-annotation.xml");
    15         
    16         UserRepositoryImpl uri = (UserRepositoryImpl) ctx.getBean("userRepositoryImpl");
    17         System.out.println(uri);
    18         
    19         TestObject to = (TestObject) ctx.getBean("testObject");
    20         System.out.println(to);
    21         
    22         
    23         UserService  us  = (UserService) ctx.getBean("userService");
    24         System.out.println(us);
    25         
    26         
    27         UserController uc = (UserController) ctx.getBean("userController");
    28         System.out.println(uc);
    29         uc.execute();
    30         
    31         
    32         
    33     }
    34 
    35 }

    beans-anntation.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.0.xsd">
     7 
     8 
     9     <!-- 指定Spring IOC 容器扫描的包 -->
    10     <!-- 可以通过resource-pattern 指定扫描的资源  
    11         <context:component-scan 
    12             base-package="com.jason.spring.beans.annotation"
    13             resource-pattern="repository/*.class">
    14         
    15         </context:component-scan>
    16         
    17     -->
    18      <!-- 
    19          <context:exclude-filter> 排除那些指定表达式的组件
    20          <context:include-filter> 指定包含哪些表达式的组件,该子节点需要 设置use-default-filters="false"
    21       
    22      <context:component-scan base-package="com.jason.spring.beans.annotation" >
    23          
    24          <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> 
    25          <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    26         
    27          <context:exclude-filter type="assignable" expression="com.jason.spring.beans.annotation.repository.UserRepositoryImpl"/>
    28      </context:component-scan>
    29      -->
    30      <context:component-scan base-package="com.jason.spring.beans.annotation"></context:component-scan>
    31 
    32 </beans>
  • 相关阅读:
    绝对布局absoluteLayout
    表格布局tabelLayout
    DP问题如何确定状态
    秒杀多线程第四篇 一个经典的多线程同步问题
    循环队列
    【Unity3d】【项目学习心得】从资源server下载资源(一)
    矩阵经典题目六:poj 3070 Fibonacci
    Mean Shift具体介绍
    Android ViewPager使用具体解释
    Android中ExpandableListView控件基本使用
  • 原文地址:https://www.cnblogs.com/jasonHome/p/5968138.html
Copyright © 2011-2022 走看看