• [JAVAee]Spring使用注解来存储与获取Bean对象


    前置内容:[JAVAee]Spring项目的创建与基本使用_HY_PIGIE的博客-CSDN博客

    先前我们在项目中注册类到spring中,要在xml中一行一行的手动添加bean标签.如果对象数目一多起来,就会显得非常繁琐.

    本文章介绍了使用另一种方法,使用注解的方式快捷的完成Bean对象的存储与获取.

    目录

    配置spring.xml文件

    注解存储Bean对象 

    通过类注解存储Bean对象

    类注解间的关系

    通过方法注解存储Bean对象

    方法注解的重命名 

    注解获取Bean对象

    Autowired注解的方式 

    @Resource与@Autowired的区别 

    同一个类多个@Bean的方法


    配置spring.xml文件

    拷贝下面的内容,注意替换base-packge为所要加入的类的包

    1. "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:content="http://www.springframework.org/schema/context"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    6. <content:component-scan base-package="demo">content:component-scan>
    7. beans>

    按照上面的配置,spring只能扫描demo包下的类.其他地方的类都不能被识别到,就相当于没有加入到spring当中.

    注解存储Bean对象 

    存储Bean对象的注解有两类,类注解与方法注解.

    类注解有:

    @Controller、@Service、@Repository、@Component、@Configuration.

    方法注解有:

    @Bean

    通过类注解存储Bean对象

    五个类注解都可以将类注册到spring当中,并创建出Bean对象 

    Controller注解

    1. package demo;
    2. import org.springframework.stereotype.Controller;
    3. @Controller//加入Controller注解,将类注册到spring当中
    4. public class UserController {
    5. private String name;
    6. public void hello(){
    7. System.out.println("controller-hello");
    8. }
    9. }

     Service注解

    1. package demo;
    2. import org.springframework.stereotype.Service;
    3. @Service//添加Service注解
    4. public class UserService {
    5. private String name;
    6. public void hello(){
    7. System.out.println("service-hello");
    8. }
    9. }

    Repository注解 

    1. package demo;
    2. import org.springframework.stereotype.Repository;
    3. @Repository//添加Repository注解
    4. public class UserRepository {
    5. private String name;
    6. public void hello(){
    7. System.out.println("repository-hello");
    8. }
    9. }

    Configuration注解 

    1. package demo;
    2. import org.springframework.context.annotation.Configuration;
    3. @Configuration//添加Configuration注解
    4. public class UserConfiguration {
    5. private String name;
    6. public void hello(){
    7. System.out.println("configuration-hello");
    8. }
    9. }

    Component注解 

    1. package demo;
    2. import org.springframework.stereotype.Component;
    3. @Component//添加Component注解
    4. public class UserComponent {
    5. private String name;
    6. public void hello(){
    7. System.out.println("component-hello");
    8. }
    9. }

    分别获取Bean对象并使用

    1. //获取spring上下文对象
    2. ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    3. //分别通过getBean方法得到刚刚通过注解注册的类创建得到的Bean对象
    4. UserController userController = (UserController) context.getBean("userController");
    5. userController.hello();
    6. UserService userService = (UserService) context.getBean("userService");
    7. userService.hello();
    8. UserRepository userRepository = (UserRepository) context.getBean("userRepository");
    9. userRepository.hello();
    10. UserConfiguration userConfiguration = (UserConfiguration) context.getBean("userConfiguration");
    11. userConfiguration.hello();
    12. UserComponent userComponent = (UserComponent) context.getBean("userComponent");
    13. userComponent.hello();

     通过注解注册类,在获取Bean对象的时候注意是通过id获取的.

    根据spring规定,id默认为被注册的类的首字母小写形式(通常类的首字母为大写嘛).

    注意:如果类的开头两个字母都是大写,则直接使用类名作为id来获取Bean对象

    例如,类名为:ABc,则ABc为id

            类名为:Abc,则abc为id

    类注解间的关系

    五个类注解,分别为:

    ①Controller-控制器:归属于业务逻辑层,用来控制用户的行为,检查用户的数据是否有效.

    ②Service-服务:归属于服务层,调用持久化类实现相应的功能,但不直接与数据库进行交互.

                            [持久化类,与数据库打交道的类.将接收到的数据存放到数据库中存储.]

    ③Repository-仓库:归属于持久层,直接与数据库打交道.通常每一个表都有一个对应的Repository

    ④Configuration-配置:归属于配置层,配置当前项目信息.

    ⑤Component-组成:归属于公共工具类.

    ①~④其实都包含着Component,相当于①~④都为Component的"子类"

    这么多类注解,都可以把Bean对象存储到spring当中.分成五大类只是为了程序员可以通过类注解了解当前类的作用是什么. 

    相当于项目开发的一种形式吧.

    通过方法注解存储Bean对象

    @Bean-方法注解

    要搭配类注解来使用:

    只有将类进行了注解spring才会将此类进行扫描,才能获取到其中的方法

    1. @Component//使用了方法注解的类要搭配类注解来使用
    2. //因为spring要先扫描到UserBean类才能接着扫描此类里面的添加了注解的userBean方法
    3. public class UserBean {
    4. private String name;
    5. @Bean//Bean方法注解,将方法返回的对象存储到spring中变成Bean对象
    6. public UserBean userBean(){
    7. UserBean userBean = new UserBean();
    8. userBean.name = "wow";
    9. return userBean;
    10. }
    11. }
    1. //默认是通过方法的方法名作为id来获取bean对象
    2. UserBean userBean = (UserBean) context.getBean("userBeanMethod");

    方法注解的重命名 

    注意:重命名后就不能使用方法名作为id获取Bean对象了 

    1. @Bean(name = {"user","wualala"})//还可以对其id进行重命名
    2. //注意是重命名噢,重命名之和就不能用方法名来获取bean对象了
    3. //注解中的name也可以是一个数组的形式,即有多个id.

    注解获取Bean对象

    获取Bean对象,也可以称为对象配备对象注入.

    主要使用Autowired注解与Resource注解

    被注入的对象的类也要加上类注解噢

    获取Bean对象的方法主要有三种:

    1. 属性注入
    2. 构造方法注入
    3. setter注入

    Autowired注解的方式 

    1.属性注入

    使用Autowired注解

    1. @Controller//加入Controller注解,将类注册到spring当中
    2. public class UserController {
    3. @Autowired//使用注解注入Bean对象userService
    4. //在注入后就可以在UserController对象中使用userService对象
    5. private UserService userService;
    6. public UserService getUserService(){
    7. return userService;
    8. }
    9. }

    2.构造方法注入

    spring收到请求后,根据UserService类创建一个对象,并传到Autowired注解的构造方法中 

    1. @Controller//加入Controller注解,将类注册到spring当中
    2. public class UserController {
    3. private UserService userService;
    4. @Autowired//注解
    5. public UserController(UserService userService){
    6. this.userService = userService;
    7. }
    8. public UserService getUserService(){
    9. return userService;
    10. }
    11. }

    如果只有一个构造方法,则可以不用Autowired注解 

    3.setter注入

    在属性的setter方法上使用注解注入对象

    1. @Controller//加入Controller注解,将类注册到spring当中
    2. public class UserController {
    3. private UserService userService;
    4. public UserService getUserService(){
    5. return userService;
    6. }
    7. @Autowired//setter方法上加上注释
    8. public void setUserService(UserService userService) {
    9. this.userService = userService;
    10. }
    11. }

    @Resource与@Autowired的区别 

    还有另一种注入对象的注解:

    @Resource,其和@Autowired使用的方法是一致的.

    但也有主要的区别:

    1. @Autowired来自于Spring,@Resource来自于JDK的注解
    2. 可设置的参数不同,@Autowried不可设置id.@Resource可以设置name,根据id获取bean对象
    3. @Autowried可以使用属性注入,构造方法注入与setter注入,而@Resource只能使用属性注入与setter注入,不能使用构造方法的注入.

    同一个类多个@Bean的方法

    在UserBean类中注解了多个方法存储Bean对象

    在UserController类中注入的时候,需要将注入的Bean对象进行命名.

    不然会因为同一个类的Bean对象不唯一而报错

    但@Autowried本身并不能进行重命名,需要搭配另一个注解@Qualifier来进行重命名

    或直接使用@Resource注解

    同一个类有多个Bean存储

    1. @Component//使用了方法注解的类要搭配类注解来使用
    2. //因为spring要先扫描到UserBean类才能接着扫描此类里面的添加了注解的userBean方法
    3. public class UserBean {
    4. private String name;
    5. @Bean
    6. public UserBean user1(){
    7. UserBean userBean = new UserBean();
    8. userBean.name = "haha";
    9. return userBean;
    10. }
    11. @Bean
    12. public UserBean user2(){
    13. UserBean userBean = new UserBean();
    14. userBean.name = "dongdong";
    15. return userBean;
    16. }
    17. public void getName(){
    18. System.out.println(name);
    19. }
    20. }

    获取指定Bean对象方法一: 

    @Autowried + @Qualifier

    1. @Controller
    2. public class UserController {
    3. @Autowired
    4. @Qualifier("user2")//注解定义名称
    5. private UserBean userBean;
    6. public UserBean getUserBean() {
    7. return userBean;
    8. }
    9. }

    获取指定Bean对象方法二:

    1. @Controller
    2. public class UserController {
    3. @Resource(name = "user1")
    4. private UserBean userBean;
    5. public UserBean getUserBean() {
    6. return userBean;
    7. }
    8. }

  • 相关阅读:
    SpringCache和Redis结合基本使用
    电子学会2021年6月青少年软件编程(图形化)等级考试试卷(二级)答案解析
    Treap 学习笔记
    线性表的概念
    并列句------六级
    C++中resize和reserve
    使用shell脚本安装hadoop高可用集群
    (赠源码)基于SSM&Mysql网上购物系统12503-计算机毕业设计
    是面试官放水,还是公司太缺人了?华为原来这么容易就进了...
    .NET C#基础(6):命名空间 - 有名字的作用域
  • 原文地址:https://blog.csdn.net/weixin_67719939/article/details/132865582