• Spring~五种存储Bean对象的类注解、方法注解(@Bean)以及Bean对象的获取


    目录

    五种存储Bean对象的类注解

    ​@Controller 

    @Service

    @Repository 

    @Component

    @Configuration

    方法注解@Bean

    使用@Bean注解的常见问题

    当一个类型有多个实例对象,使用类型获取就会报错

    在容器中找不到Bean,不论通过什么方式来获取Bean对象都会报错


    五种存储Bean对象的类注解

    五种类注解分别为@Controller、@Service、@Repository、@Component、@Configuration。其中@Configuration是将其注册为配置类对象,在项目启动时需要准备一些配置信息,一般通过配置类来初始化,其他类注解都是注册为普通的Bean对象,主要是在软件分层后,在不同的分层使用

    @Controller 

    @Controller注解是控制器存储,用来接收http请求并返回响应,其默认是单例的方式注册Bean对象

    UserController类

    1. package org.example.controller;
    2. import org.springframework.stereotype.Controller;
    3. @Controller
    4. public class UserController {
    5. }
    1. package org.example;
    2. import org.example.controller.UserController;
    3. import org.example.model.Bean;
    4. import org.springframework.context.ApplicationContext;
    5. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    6. public class APP {
    7. public static void main(String[] args) {
    8. //ApplicationContext是Spring容器的顶级接口
    9. //AnnotationConfigApplicationContext其中的一个实现类,作用是:
    10. //(1)扫描指定的包路径下,使用了Spring框架注解的类
    11. //(2)扫描到后,注册这些类到容器中=>框架帮我们new对象,及注入对象的依赖关系(属性赋值)
    12. ApplicationContext context = new AnnotationConfigApplicationContext("org.example");
    13. //获取Bean对象有两种方式:
    14. // (1)通过bean的类型来获取
    15. UserController bean1 = context.getBean(UserController.class);
    16. //(2)通过bean的id(也叫bean的名称)
    17. UserController bean2 = (UserController) context.getBean("userController");
    18. System.out.println(bean1 == bean2);
    19. }
    20. }

     

    通过代码,可以看出@Controller注解是以单例模式注册Bean对象的 

    @Service

    @Service注解是服务存储,是用于处理业务逻辑的分层,其默认是单例的方式注册Bean对象。

    1. package org.example.service;
    2. import org.springframework.stereotype.Service;
    3. @Service
    4. public class UserService {
    5. }
    1. package org.example;
    2. import org.example.service.UserService;
    3. import org.springframework.context.ApplicationContext;
    4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    5. public class APP {
    6. public static void main(String[] args) {
    7. //ApplicationContext是Spring容器的顶级接口
    8. //AnnotationConfigApplicationContext其中的一个实现类,作用是:
    9. //(1)扫描指定的包路径下,使用了Spring框架注解的类
    10. //(2)扫描到后,注册这些类到容器中=>框架帮我们new对象,及注入对象的依赖关系(属性赋值)
    11. ApplicationContext context = new AnnotationConfigApplicationContext("org.example");
    12. //获取Bean对象有两种方式:
    13. // (1)通过bean的类型来获取
    14. UserService bean1 = context.getBean(UserService.class);
    15. //(2)通过bean的id(也叫bean的名称)
    16. UserService bean2 = (UserService) context.getBean("userService");
    17. System.out.println(bean1 == bean2);
    18. }
    19. }

     

    @Repository 

    @Repository注解是仓库存储,数据访问层,一般是数据库的操作,其默认也是单例的方式注册Bean对象

    @Component

    @Component注解是组件存储,除了明确含义的类注解作用外,其他的作用就可以使用@Component来进行注解,其默认也是单例的方式注册Bean对象

    @Configuration

    @Configuration注解主要是配置存储,Spring容器启动时就初始化配置

    方法注解@Bean

    @Bean方法注解:

    · 只有其所在的类被注册到容器中,方法上的@Bean注解才能生效

    · @Bean注解的方法,所在的类,必须使用上述的五种类注解之一

    · @Bean注解一般使用在@Configuration注解的类中,比较规范

    代码演示

    创建User类

     @Data:lombok注解,表示自动生成getter、setter、hashcode、toString等等

    1. package org.example.model;
    2. import lombok.Data;
    3. @Data
    4. public class User {
    5. private String userName;
    6. }

    创建Config配置类

    1. package org.example.config;
    2. import org.example.model.User;
    3. import org.springframework.context.annotation.Bean;
    4. import org.springframework.context.annotation.Configuration;
    5. @Configuration
    6. public class AppConfig {
    7. //方法上使用Bean注解表示注册一个Bean对象到容器中
    8. //方法的名称,就是bean ID(名称),方法的返回值就是注册的实例对象
    9. //一般是public修饰的实例方法
    10. @Bean
    11. public User TestBean1() {
    12. User user = new User();
    13. user.setUserName("TestBean1");
    14. return user;
    15. }
    16. @Bean
    17. public User TestBean2() {
    18. User user = new User();
    19. user.setUserName("TestBean2");
    20. return user;
    21. }
    22. }

    @Bean注解搭配类注解,将对象存储到Spring容器中,此时就可以通过方法名来获取到注册到容器中的Bean对象

    1. package org.example;
    2. import org.example.config.AppConfig;
    3. import org.example.controller.UserController;
    4. import org.example.model.User;
    5. import org.example.service.UserService;
    6. import org.springframework.context.ApplicationContext;
    7. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    8. public class APP {
    9. public static void main(String[] args) {
    10. //ApplicationContext是Spring容器的顶级接口
    11. //AnnotationConfigApplicationContext其中的一个实现类,作用是:
    12. //(1)扫描指定的包路径下,使用了Spring框架注解的类
    13. //(2)扫描到后,注册这些类到容器中=>框架帮我们new对象,及注入对象的依赖关系(属性赋值)
    14. ApplicationContext context = new AnnotationConfigApplicationContext("org.example");
    15. User user = (User) context.getBean("testBean1");
    16. System.out.println(user);
    17. User user1 = (User) context.getBean("testBean2");
    18. System.out.println(user1);
    19. }

     

    使用@Bean注解的常见问题

    当一个类型有多个实例对象,使用类型获取就会报错

    例如下列代码

    1. package org.example.config;
    2. import org.example.model.User;
    3. import org.springframework.context.annotation.Bean;
    4. import org.springframework.context.annotation.Configuration;
    5. @Configuration
    6. public class AppConfig {
    7. //方法上使用Bean注解表示注册一个Bean对象到容器中
    8. //方法的名称,就是bean ID(名称),方法的返回值就是注册的实例对象
    9. //一般是public修饰的实例方法
    10. @Bean
    11. public User testBean1() {
    12. User user = new User();
    13. user.setUserName("testBean1");
    14. return user;
    15. }
    16. @Bean
    17. public User testBean2() {
    18. User user = new User();
    19. user.setUserName("testBean2");
    20. return user;
    21. }
    22. }

    通过User类型注册了两个Bean对象,此时如果再通过User类型来获取Bean对象就会报错

    1. public class APP {
    2. public static void main(String[] args) {
    3. ApplicationContext context = new AnnotationConfigApplicationContext("org.example");
    4. User user = (User) context.getBean(User.class);
    5. System.out.println(user);
    6. }

     此时就只能通过@Bean注解的方法名来获取Bean对象

    在容器中找不到Bean,不论通过什么方式来获取Bean对象都会报错

    在学习过程中,出现这种问题有两种情况,一种是扫描的包名写错时,就会出现这种错误

    另一种是配置类未加注解时,也出现了这种错误 

     

     

     

     

     

     

     

  • 相关阅读:
    编译原理实验-词法分析
    2022-08-30 第六小组 瞒春 学习笔记
    iOS重签名-超详细,附排错
    商城免费搭建之java商城 开源java电子商务Spring Cloud+Spring Boot+mybatis+MQ+VR全景+b2b2c
    切方块游戏 HTML5+jQuery【附源码】
    贪吃蛇游戏
    Web2.0架构与Web3.0架构
    uniapp条件判断app,H5,微信小程序端
    Windows10 Docker 安装教程
    day44 数据库查询命令
  • 原文地址:https://blog.csdn.net/qq_58284486/article/details/127961887