Spring基于注解存储对象

前言

上一篇文章中已经介绍了在Spring中存储Bean和取Bean的方法. 而在 Spring 中想要更简单的存储和读取对象的核⼼是使⽤注解. 这篇文章介绍如何基于注解存储对象

基于注解存储对象

基于注解存储对象第一步仍然是先修改spring配置文件. 加入代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       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">
    <context:component-scan base-package="com.annotation"></context:component-scan>
</beans>

其中代码<context:component-scan base-package="com.annotation"></context:component-scan>是在注册要扫描的包, spring启动后, 会自动在指定的包(此处是com.annotation)下扫描, 找到加了注解的类, 然后创建Bean对象并存储到spring容器中. 接下来介绍存储Bean对象的注解.

用于存储Bean对象的注解有五大类注解和一个方法注解.
五大类注解分别是: @Controller, @Service, @Repository, @Component, @Configuration
方法注解: @Bean



@Controller (控制器存储)

@Controller
public class UseController {
   
    public void print(){
   
        System.out.println("do_useController");
    }
}

@Service (服务存储)

@Service
public class UseService {
   
    public void print(){
   
        System.out.println("do_useService");
    }
}

@Repository (仓库存储)

@Repository
public class UseRepository {
   
    public void print(){
   
        System.out.println("do_useRepository");
    }
}

@Component (组件存储)

@Component
public class UseComponent {
   
    public void print(){
   
        System.out.println("do_useComponent");
    }
}

@Configuration (配置存储)

@Configuration
public class UseConfiguration {
   
    public void print(){
   
        System.out.println("do_useConfiguration");
    }
}


在五大类注解中, @Controller, @Service, @Repository, @configuration都是@Component的子类.

对于五大类注解的Bean对象命名规则, 观察底层实现源码之后发现. 如果类名前两位都是大写, 则Bean对象名与类名一致. 如果类名第一位大写, 第二位小写, 则Bean对象名是类名第一位小写, 其他位不变.


@Bean(方法注解)

@Bean要搭配类注解一起使用, 使用 @Bean 注解标记的方法将会被Spring容器注册为一个Bean, 并且该Bean的实例将由该方法返回。

@Component
public class UserBeans {
   
    @Bean
    public User user1(){
   
        User user = new User();
        user.setId(1);
        user.setName("zhangsan");
        user.setPassword("lisi");
        return user;
    }
}
	 User user = applicationContext.getBean("user1", User.class);
     System.out.println(user.getPassword());

在这里插入图片描述


@Bean注解中可以用namevalue指定Bean的对象名, 如果没有指定默认对象名默认为方法名. 上面的例子中没有指定对象名, 下面写一个指定对象名的例子.

@Component
public class UserBeans {
   
    @Bean(name = {
   "user0", "user"})
    public User user1(){
   
        User user = new User();
        user.setId(1);
        user.setName("zhangsan");
        user.setPassword("lisi");
        return user;
    }
}
    User user = applicationContext.getBean("user0", User.class);
    System.out.println(user.getPassword());

上面的这个例子在获取Bean对象时, 可以用user0, 也可以用user, 但是默认的方法名(user1)不能使用.


相关推荐

  1. spring(二):基于注解实现依赖注入

    2023-12-07 20:40:09       30 阅读
  2. spring2基于注解开发

    2023-12-07 20:40:09       9 阅读
  3. Spring new对象注解失效

    2023-12-07 20:40:09       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-07 20:40:09       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-07 20:40:09       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-07 20:40:09       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-07 20:40:09       20 阅读

热门阅读

  1. Linux /etc/hosts文件

    2023-12-07 20:40:09       38 阅读
  2. pip的基本命令和使用:程序猿的必备技能

    2023-12-07 20:40:09       34 阅读
  3. Node.js之path路径模块

    2023-12-07 20:40:09       36 阅读
  4. 浅谈对ASP.NET MVC(微软Web开发框架)的理解

    2023-12-07 20:40:09       38 阅读
  5. 2023-12-04 AIGC-Stable Diffusion和SadTalker-搭建及使用

    2023-12-07 20:40:09       37 阅读
  6. C++h弧度转成角度

    2023-12-07 20:40:09       37 阅读
  7. pip的常见60条基本命令和使用详解

    2023-12-07 20:40:09       32 阅读
  8. 第一章 使用CMake与VS2022编译Opencv

    2023-12-07 20:40:09       37 阅读
  9. 【微软技术栈】基于任务的异步编程

    2023-12-07 20:40:09       32 阅读
  10. WebSocket

    2023-12-07 20:40:09       40 阅读
  11. Django回顾【五】

    2023-12-07 20:40:09       29 阅读