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

存储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代码:

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

输出结果跟@Controller一样

@Repository(仓库存储)

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

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

@Component(组件存储)

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

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

@Configuration(配置存储)

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

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

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

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

Bean

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

@Controller
public 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类

@Component
public 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 实现的,实现代码如下:

@Service
public class UserService {

    public User user1(){
        User user = new User();
        return user;
    }
}

@Controller
public 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");
    }
}

在这里插入图片描述

构造方法注入

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

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

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

Setter注入

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

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

@Resource(注入关键字)

@Controller
public 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 注⼊和属性注⼊,不能⽤于构造函数注⼊
  • 78
    点赞
  • 78
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 98
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 98
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

手插口袋谁也不爱♡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值