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

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

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

微博:伊直都在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>

转载于:https://www.cnblogs.com/jasonHome/p/5968138.html

[原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)...相关推荐

  1. SpringCloud学习笔记(1)- Spring Cloud Alibaba

    文章目录 SpringCloud学习笔记(1)- Spring Cloud Alibaba 服务治理 Nacos 服务注册 Nacos 服务发现与调用 Ribbon 负载均衡 Sentinel 服务限 ...

  2. SpringCloud学习笔记(1)- Spring Cloud Netflix

    文章目录 SpringCloud学习笔记(1)- Spring Cloud Netflix 单体应用存在的问题 Spring Cloud Eureka Eureka Server代码实现 Eureka ...

  3. SpringCloud学习笔记(6)----Spring Cloud Netflix之负载均衡-Ribbon的使用

    1. 什么是负载均衡? 负载均衡,就是分发请求流量到不同的服务器. 负载均衡一般分为两种 1. 服务器端负载均衡(nginx) 2. 客户端负载均衡(Ribbon) 2. 服务提供者(spring-c ...

  4. Spring Security技术栈学习笔记(八)Spring Security的基本运行原理与个性化登录实现

    正如你可能知道的两个应用程序的两个主要区域是"认证"和"授权"(或者访问控制).这两个主要区域是Spring Security的两个目标."认证&qu ...

  5. 学习笔记:cache 和spring cache 技术(1)

    title: 学习笔记:cache 和spring cache 技术(1) author: Eric liu tags: [] categories: hexo 缓存是实际工作中非常常用的一种提高性能 ...

  6. Spring Cloud 学习笔记(四)-Spring Cloud Hystrix

    Spring Cloud 学习笔记(四)-Spring Cloud Hystrix 由于前一阵子项目的原因,今天才继续弄上,今天想学习一下Hystrix组件 这个组件还挺抽象的,最开始我一直没太明白, ...

  7. Spring学习笔记:第一个Spring Boot程序HelloWorld

    Spring学习笔记:第一个Spring Boot程序HelloWorld 一.跟着 Spring 了解技术趋势 1.看看 Spring 5.x 的改变暗示了什么 2.Spring Boot 和 Sp ...

  8. Spring Security技术栈学习笔记(十三)Spring Social集成第三方登录验证开发流程介绍

    开发第三方登录,我们必须首先要了解OAuth协议(本文所讲述的OAuth协议指的是OAuth2协议),本文首先简单介绍OAuth协议,然后基于Spring Social来阐述开发第三方登录需要做哪些准 ...

  9. 【长篇博文】Docker学习笔记与深度学习环境的搭建和部署(二)

    长篇博文记录学习流程不容易,请关注.转发.点赞.评论,谢谢! 上一篇文章:Docker学习笔记与深度学习环境的搭建和部署(一) 文章末尾附加nvidia455.23.cuda11.1.cudnn8.0 ...

  10. 开源鸿蒙南向嵌入学习笔记——NAPI框架学习(一)

    开源鸿蒙南向嵌入学习笔记--NAPI框架学习(一) 前言--系列介绍 本系列文章主要是记录笔者在鸿蒙南向的学习与工作中的知识点笔记记录,其中不止会针对鸿蒙中的学习问题进行思考与记录,也会对涉及到的一些 ...

最新文章

  1. 【天命奇御】成就进度62/71的通关攻略(1·开篇前言)
  2. Fabric动态增加组织【资料】
  3. ubuntu apt-mirror 同步源到本地
  4. 如何认识物联网?还云里雾里不?
  5. 【讲●解】KMP算法
  6. [Swift]LeetCode551. 学生出勤纪录 I | Student Attendance Record I
  7. 基于Amarok的跨平台音乐播放器:Clementine mac版
  8. 关于c语言编写 单项链表 的创建、插入、修改、删除、显示、退出 的程序案例
  9. 第 8 天 多线程与多进程
  10. java声明代码是什么_java安全编码指南之:声明和初始化说明
  11. 如何转换并压缩png格式图片
  12. Java开发中巧妙使用链表来实现模拟栈的入栈出栈操作
  13. Masonry 控件详解
  14. 【Android应用】 九宫格日志
  15. 开源音乐软件——落雪
  16. 【关于Spring那些事】——与君初相识
  17. 方程求根的迭代法——牛顿迭代法
  18. 日语学习之——长音促音
  19. 华**见面试讲师(面试经验)
  20. STM32CubeMX系列|ADC模数转换

热门文章

  1. Come On, Baby!
  2. Ajax叠加(Ajax返回数据用Ajax发出)
  3. 流畅的Python---list排序和保持有序序列
  4. 解决跨域form表单post提交时Forbidden的问题。
  5. Codeforces Round #535 (Div. 3) 解题报告
  6. 【shell 练习5】编写简单的多级菜单
  7. ef core中使用code first
  8. JavaScript算法 之 选择排序
  9. CentOS 关闭防火墙和selinux
  10. cocos2d-x学习之旅(九): 2.2 盘古开天辟地,进入游戏世界