相关推荐recommended
探秘Spring中Bean的注解宝典:解读存取Bean的相关注解及用法
作者:mmseoamin日期:2024-02-20

目录

  • 存储Bean对象
    • @Controller(控制器存储)
    • @Service(服务存储)
    • @Repository(仓库存储)
    • @Component(组件存储)
    • @Configuration(配置存储)
    • Bean
      • 重命名Bean
      • 获取Bean对象
        • 属性注入
        • 构造方法注入
        • Setter注入
        • @Resource(注入关键字)

          存储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");
              }
          }
          

          效果为

          探秘Spring中Bean的注解宝典:解读存取Bean的相关注解及用法,在这里插入图片描述,第1张

          @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+"你好");
                }
            }
            

            结果

            探秘Spring中Bean的注解宝典:解读存取Bean的相关注解及用法,在这里插入图片描述,第2张

            重命名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");
                }
            }
            

            探秘Spring中Bean的注解宝典:解读存取Bean的相关注解及用法,在这里插入图片描述,第3张

            构造方法注入

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

            @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 注⼊和属性注⼊,不能⽤于构造函数注⼊