iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > JAVA >探秘Spring中Bean的注解宝典:解读存取Bean的相关注解及用法
  • 120
分享到

探秘Spring中Bean的注解宝典:解读存取Bean的相关注解及用法

springjava后端开发语言青少年编程 2023-08-16 15:08:42 120人浏览 安东尼
摘要

目录 存储Bean对象@Controller(控制器存储)@Service(服务存储)@Repository(仓库存储)@Component(组件存储)@Configuration(配置存储)Bean重命名Bean 获取B

存储Bean对象

将对象存储在 spring 中,有两种注解类型可以实现:

  1. 类注解:@Controller、@Service、@Repository、@Component、@Configuration
  2. 方法注解:@Bean

@Controller(控制器存储)

如下使用@Controller存储Bean代码:

@Controller        public class UserController {        public void SayHi(String name){        System.out.println("你好"+name);    }}

使用获取上下文的方法获取上面存储的对象

public class Test {    public static void main(String[] args) {        //获取spring上下文     ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");    //获取指定的Bean对象名称+类型进行获取      UserController userController =  (UserController) applicationContext.getBean("userController");      userController.SayHi("zcx");    }}

效果为
在这里插入图片描述

@Service(服务存储)

如下使用@Service存储Bean代码:

@Servicepublic class UserService {    public void SayHi(String name){        System.out.println("你好"+name);    }}

输出结果跟@Controller一样

@Repository(仓库存储)

如下使用@Repository存储Bean代码:

@Repositorypublic class UserRepository {    public void SayHi(String name){        System.out.println("你好"+name);    }}

@Component(组件存储)

如下使用@Component存储Bean代码:

@Componentpublic class UserComponent {    public void SayHi(String name){        System.out.println("你好"+name);    }}

@Configuration(配置存储)

如下使用@Configuration存储Bean代码:

@Configurationpublic class UserConfiguration {    public void SayHi(String name){        System.out.println("你好"+name);    }}

需要这么多类注解是让程序员看到类注解之后,就能直接了解当前类
的⽤途,比如:

  • @Controller:表示的是业务逻辑层
  • @Servie:表示的是服务层
  • @Repository:表示的是持久层
  • @Configuration:表示的是配置层

Bean

⽅法注解是放到某个⽅法上的要配合类注解使⽤,如以下代码的实现:

@Controllerpublic class Users {    @Bean    public User user1(){        User user = new User();        return user;    }}
public class Test {    public static void main(String[] args) {        //获取spring上下文     ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");    //获取指定的Bean对象名称+类型进行获取    User user =  (User) applicationContext.getBean("user1");      user.sayHi("zcx");    }}

user类

@Componentpublic class User {    public void sayHi(String name){        System.out.println(name+"你好");    }}

结果
在这里插入图片描述

重命名Bean

可通过设置 name 属性给 Bean 对象进⾏重命名操作

@Bean(name = {"u1", "us1"})public User user1() {User user = new User();return user;}

或者

@Bean(name = {"u1", "us1"})public User user1() {User user = new User();return user;}

获取Bean对象

获取Bean对象就是把对象取出来放入某个类中,也叫对象注入或者对象装配。
对象注入有三种实现方法:

  1. 属性注⼊
  2. 构造⽅法注⼊
  3. Setter 注⼊

下⾯我们按照实际开发中的模式,将 Service 类注⼊到 Controller 类中

属性注入

属性注⼊是使⽤ @Autowired 实现的,实现代码如下:

@Servicepublic class UserService {    public User user1(){        User user = new User();        return user;    }}
@Controllerpublic class UserController {    @Autowired    private UserService userService;    public User getUser(){ return userService.user1();    }}
public class Test {    public static void main(String[] args) {        //获取spring上下文        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");        //获取指定的Bean对象名称+类型进行获取        UserController userController = applicationContext.getBean(UserController.class);      userController.getUser().sayHi("zcx");    }}

在这里插入图片描述

构造方法注入

构造⽅法注⼊是在类的构造⽅法中实现注⼊,实现代码如下:

@Controllerpublic class UserController {    private UserService userService;    @Autowired    public UserController(UserService userService) {        this.userService = userService;    }    public User getUser(){ return userService.user1();    }}

其余代码写法跟属性注入一样

Setter注入

在设置 set ⽅法的时候需要加上 @Autowired 注解

@Controllerpublic class UserController {    private UserService userService;    @Autowired    public void SetUserController(UserService userService) {        this.userService = userService;    }    public User getUser(){ return userService.user1();    }}

@Resource(注入关键字)

@Controllerpublic class UserController {    @Resource    private UserService userService;    public User getUser(){ return userService.user1();    }}

使用@Resource 可以解决同⼀类型多个 Bean 报错问题
可以如下设置方法名

@Resource(name = "user1")

@Autowired 和 @Resource 的区别

  • 出身不同:@Autowired 来⾃于 Spring,⽽ @Resource 来⾃于 jdk 的注解
  • 使⽤时设置的参数不同:相⽐于 @Autowired 来说,@Resource ⽀持更多的参数设置,例如name 设置,根据名称获取 Bean
  • @Autowired 可⽤于 Setter 注⼊、构造函数注⼊和属性注⼊,⽽ @Resource 只能⽤于 Setter 注⼊和属性注⼊,不能⽤于构造函数注⼊

来源地址:https://blog.csdn.net/st200112266/article/details/131875625

--结束END--

本文标题: 探秘Spring中Bean的注解宝典:解读存取Bean的相关注解及用法

本文链接: https://www.lsjlt.com/news/371484.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作