• spring 注解: 更加简单的存储 Bean


    目录

    1. 更加简单的存储 Bean

    1.1 添加注解

    1.1.1 @Controller【控制器存储】

    1.1.2 @Service【服务存储】

    1.1.3 @Repository【仓库存储】

    1.1.4 @Component【组件存储】

    1.1.5 @Configuration【配置存储】

    1.1.6 类注解存储 Bean 的命名规则(默认命名规则)

    1.1.7 方法注解 Bean

    1.1.8 重命名 Bean


    1. 更加简单的存储 Bean

    前置工作:

    1.1 添加注解

    1️⃣通过类注解实现 Bean 对象的存储:@Controller、@Service、@Repository、@Component、@Configuration

    2️⃣通过方法注解实现 Bean 对象的存储:@Bean

    1.1.1 @Controller【控制器存储】

    效验参数的合法性(安检系统)

    1. @Controller
    2. public class User {
    3. public void sayHi() {
    4. System.out.println("Hi, User");
    5. }
    6. }
    1. public class App {
    2. public static void main(String[] args) {
    3. //1.得到容器
    4. ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    5. //2.得到 Bean 对象
    6. User user = context.getBean("user", User.class);
    7. //3.使用 Bean 对象
    8. user.sayHi();
    9. }
    10. }

    1.1.2 @Service【服务存储】

    业务组装(客服中心)

    使用 @Service 存储 Bean:

    1. @Service
    2. public class UserService {
    3. public void sayHi() {
    4. System.out.println("Hi, UserService");
    5. }
    6. }

    读取 Bean:

    1. public class App {
    2. public static void main(String[] args) {
    3. //1.得到容器
    4. ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    5. //2.得到 Bean 对象
    6. UserService userService = context.getBean("userService", UserService.class);
    7. //3.使用 Bean 对象
    8. userService.sayHi();
    9. }
    10. }

    1.1.3 @Repository【仓库存储】

    实际业务处理(实际办理的业务)

    使用 @Repository 存储 Bean:

    1. @Repository
    2. public class Teacher {
    3. public void sayHi() {
    4. System.out.println("Hi, Teacher");
    5. }
    6. }

    读取 Bean:

    1. public class App {
    2. public static void main(String[] args) {
    3. //1.得到容器
    4. ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    5. //2.得到 Bean 对象
    6. Teacher teacher = context.getBean("teacher", Teacher.class);
    7. //3.使用 Bean 对象
    8. teacher.sayHi();
    9. }
    10. }

    1.1.4 @Component【组件存储】

    工具类层(基础的工具)

    使用 @Component 存储 Bean:

    1. @Component
    2. public class Teacher {
    3. public void sayHi() {
    4. System.out.println("Hi, Teacher");
    5. }
    6. }

    读取 Bean:

    1. public class App {
    2. public static void main(String[] args) {
    3. //1.得到容器
    4. ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    5. //2.得到 Bean 对象
    6. Teacher teacher = context.getBean("teacher", Teacher.class);
    7. //3.使用 Bean 对象
    8. teacher.sayHi();
    9. }
    10. }

    1.1.5 @Configuration【配置存储】

    使用 @Configuration 存储 Bean:

    1. @Configuration
    2. public class Teacher {
    3. public void sayHi() {
    4. System.out.println("Hi, Teacher");
    5. }
    6. }

    读取 Bean:

    1. public class App {
    2. public static void main(String[] args) {
    3. //1.得到容器
    4. ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    5. //2.得到 Bean 对象
    6. Teacher teacher = context.getBean("teacher", Teacher.class);
    7. //3.使用 Bean 对象
    8. teacher.sayHi();
    9. }
    10. }

    1.1.6 类注解存储 Bean 的命名规则(默认命名规则)

    1️⃣默认类对象首字母小写就能获取到 Bean 对象

    Teacher teacher = context.getBean("teacher", Teacher.class);

    2️⃣使用原类名可以获取到 Bean 对象

    UConfig uConfig = context.getBean("UConfig", UConfig.class);

    ✅结论:

    如果首字母是大写,第二个字母是小写,那么 Bean 的名称就是类名小写;

    如果不满足首字母大写和第二个字母小写的情况,那么 Bean 的名称就为原类名。

     在这里 Bean 生成命名的源代码:

    1. public static String decapitalize(String name) {
    2. if (name == null || name.length() == 0) {
    3. return name;
    4. }
    5. if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) &&
    6. Character.isUpperCase(name.charAt(0))){
    7. return name;
    8. }
    9. char chars[] = name.toCharArray();
    10. chars[0] = Character.toLowerCase(chars[0]);
    11. return new String(chars);
    12. }

    1. public class Test {
    2. public static void main(String[] args) {
    3. String name = "Student";
    4. String name2 = "APPConfig";
    5. System.out.println(name + ":" + Introspector.decapitalize(name));
    6. System.out.println(name2 + ":" + Introspector.decapitalize(name2));
    7. }
    8. }

    命名规则:首字母和第二个字母大写返回原类名,否则就是首字母小写

    1.1.7 方法注解 Bean

    1️⃣使用 Bean——使用注意事项: @Bean 注解 必须要配合 五大类注解 一起使用

    1. @Component
    2. public class Articles {
    3. @Bean //将当前方法返回的对象存储到 IoC 容器中
    4. public ArticleInfo articleInfo() {
    5. // 伪代码
    6. ArticleInfo articleInfo = new ArticleInfo();
    7. articleInfo.setAid(1);
    8. articleInfo.setTitle("今天周几");
    9. articleInfo.setContent("今天周一");
    10. articleInfo.setCreatetime(LocalDateTime.now());
    11. return articleInfo;
    12. }
    13. }

    获取对象:

    1. public class App {
    2. public static void main(String[] args) {
    3. //1.得到容器
    4. ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    5. //2.得到 Bean 对象
    6. ArticleInfo articleInfo = context.getBean("articleInfo", ArticleInfo.class);
    7. //3.使用 Bean 对象
    8. System.out.println(articleInfo.toString());
    9. }
    10. }

    2️⃣Bean获取时注意事项:@Bean 的默认命名 = 方法名

    1. public ArticleInfo arc() {
    2. // 伪代码
    3. ArticleInfo articleInfo = new ArticleInfo();
    4. articleInfo.setAid(1);
    5. articleInfo.setTitle("今天周几");
    6. articleInfo.setContent("今天周一");
    7. articleInfo.setCreatetime(LocalDateTime.now());
    8. return articleInfo;
    9. }

    获取 Bean:

    ArticleInfo articleInfo = context.getBean("arc", ArticleInfo.class);

    1.1.8 重命名 Bean

    1️⃣

    @Bean("aaa") 

    2️⃣

    @Bean(name = "bbb")

    3️⃣

    @Bean(value = "ccc")

    重命名扩展:@Bean 支持指定多个名称

    @Bean(value = {"ccc", "ddd"})

    默认命名使用注意事项:当 @Bean 进行重命名之后就不可以使用默认的使用方法获取 Bean 对象 (注意和默认命名区别)

    必须要使用名字获取,例如:

    ArticleInfo articleInfo = context.getBean("article", ArticleInfo.class);

    @Bean 名称注意事项:如果多个 Bean 名称相同,那么程序执行不会报错,但是第一个 Bean 之后的对象就不会存放在容器当中,也就是说只有第一次创建Bean 的时候会将 对象 和 Bean 存储的时候,容器会自动忽略

    1. @Component
    2. public class Articles {
    3. @Bean({"article"}) //将当前方法返回的对象存储到 IoC 容器中
    4. public ArticleInfo getArt() {
    5. // 伪代码
    6. ArticleInfo articleInfo = new ArticleInfo();
    7. articleInfo.setAid(1);
    8. articleInfo.setTitle("今天周几");
    9. articleInfo.setContent("今天周一");
    10. articleInfo.setCreatetime(LocalDateTime.now());
    11. return articleInfo;
    12. }
    13. @Bean({"article"}) //将当前方法返回的对象存储到 IoC 容器中
    14. public ArticleInfo getArt1() {
    15. // 伪代码
    16. ArticleInfo articleInfo = new ArticleInfo();
    17. articleInfo.setAid(2);
    18. articleInfo.setTitle("今天学习了什么");
    19. articleInfo.setContent("注解");
    20. articleInfo.setCreatetime(LocalDateTime.now());
    21. return articleInfo;
    22. }
    23. @Bean({"article"}) //将当前方法返回的对象存储到 IoC 容器中
    24. public ArticleInfo getArt2() {
    25. // 伪代码
    26. ArticleInfo articleInfo = new ArticleInfo();
    27. articleInfo.setAid(1);
    28. articleInfo.setTitle("今天写博客了吗");
    29. articleInfo.setContent("写了");
    30. articleInfo.setCreatetime(LocalDateTime.now());
    31. return articleInfo;
    32. }
    33. }

  • 相关阅读:
    传统算法与神经网络算法,神经网络与算法的关系
    【经验分享】统计学算法大全及方法适用场景(必看)
    react是否支持给标签设置自定义的属性,比如给video标签设置webkit-playsinline?
    最新AI系统ChatGPT源码+支持OpenAI全模型+国内AI模型+AI绘画
    模板学堂丨禅道业务数据分析大屏
    VS code扩展开发详解
    如何使用 二次号查询API
    C++ Reference: Standard C++ Library reference: C Library: cstdio: fgets
    水果FL Studio/Cubase/Studio one音乐宿主软件对比
    总结Java 并发编程 72 变
  • 原文地址:https://blog.csdn.net/m0_72161237/article/details/133707650