[JAVAee]Spring使用注解来存储与获取Bean对象

前置内容:[JAVAee]Spring项目的创建与基本使用_HY_PIGIE的博客-CSDN博客

先前我们在项目中注册类到spring中,要在xml中一行一行的手动添加bean标签.如果对象数目一多起来,就会显得非常繁琐.

本文章介绍了使用另一种方法,使用注解的方式快捷的完成Bean对象的存储与获取.

目录

配置spring.xml文件

注解存储Bean对象 

通过类注解存储Bean对象

类注解间的关系

通过方法注解存储Bean对象

方法注解的重命名 

注解获取Bean对象

Autowired注解的方式 

@Resource与@Autowired的区别 

同一个类多个@Bean的方法


配置spring.xml文件

拷贝下面的内容,注意替换base-packge为所要加入的类的包

<?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:content="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">
    <content:component-scan base-package="demo"></content:component-scan>
</beans>

按照上面的配置,spring只能扫描demo包下的类.其他地方的类都不能被识别到,就相当于没有加入到spring当中.

注解存储Bean对象 

存储Bean对象的注解有两类,类注解与方法注解.

类注解有:

@Controller、@Service、@Repository、@Component、@Configuration.

方法注解有:

@Bean

通过类注解存储Bean对象

五个类注解都可以将类注册到spring当中,并创建出Bean对象 

Controller注解

package demo;

import org.springframework.stereotype.Controller;

@Controller//加入Controller注解,将类注册到spring当中
public class UserController {
    private String name;
    public void hello(){
        System.out.println("controller-hello");
    }
}

 Service注解

package demo;

import org.springframework.stereotype.Service;

@Service//添加Service注解
public class UserService {
    private String name;
    public void hello(){
        System.out.println("service-hello");
    }
}

Repository注解 

package demo;

import org.springframework.stereotype.Repository;

@Repository//添加Repository注解
public class UserRepository {
    private String name;
    public void hello(){
        System.out.println("repository-hello");
    }
}

Configuration注解 

package demo;

import org.springframework.context.annotation.Configuration;

@Configuration//添加Configuration注解
public class UserConfiguration {
    private String name;
    public void hello(){
        System.out.println("configuration-hello");
    }
}

Component注解 

package demo;

import org.springframework.stereotype.Component;

@Component//添加Component注解
public class UserComponent {
    private String name;
    public void hello(){
        System.out.println("component-hello");
    }
}

分别获取Bean对象并使用

//获取spring上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        //分别通过getBean方法得到刚刚通过注解注册的类创建得到的Bean对象
        UserController userController = (UserController) context.getBean("userController");
        userController.hello();

        UserService userService = (UserService) context.getBean("userService");
        userService.hello();

        UserRepository userRepository = (UserRepository) context.getBean("userRepository");
        userRepository.hello();

        UserConfiguration userConfiguration = (UserConfiguration) context.getBean("userConfiguration");
        userConfiguration.hello();

        UserComponent userComponent = (UserComponent) context.getBean("userComponent");
        userComponent.hello();

 通过注解注册类,在获取Bean对象的时候注意是通过id获取的.

根据spring规定,id默认为被注册的类的首字母小写形式(通常类的首字母为大写嘛).

注意:如果类的开头两个字母都是大写,则直接使用类名作为id来获取Bean对象

例如,类名为:ABc,则ABc为id

        类名为:Abc,则abc为id

类注解间的关系

五个类注解,分别为:

①Controller-控制器:归属于业务逻辑层,用来控制用户的行为,检查用户的数据是否有效.

②Service-服务:归属于服务层,调用持久化类实现相应的功能,但不直接与数据库进行交互.

                        [持久化类,与数据库打交道的类.将接收到的数据存放到数据库中存储.]

③Repository-仓库:归属于持久层,直接与数据库打交道.通常每一个表都有一个对应的Repository

④Configuration-配置:归属于配置层,配置当前项目信息.

⑤Component-组成:归属于公共工具类.

①~④其实都包含着Component,相当于①~④都为Component的"子类"

这么多类注解,都可以把Bean对象存储到spring当中.分成五大类只是为了程序员可以通过类注解了解当前类的作用是什么. 

相当于项目开发的一种形式吧.

通过方法注解存储Bean对象

@Bean-方法注解

要搭配类注解来使用:

只有将类进行了注解spring才会将此类进行扫描,才能获取到其中的方法

@Component//使用了方法注解的类要搭配类注解来使用
          //因为spring要先扫描到UserBean类才能接着扫描此类里面的添加了注解的userBean方法
public class UserBean {
    private String name;
    
    @Bean//Bean方法注解,将方法返回的对象存储到spring中变成Bean对象
    public UserBean userBean(){
        UserBean userBean = new UserBean();
        userBean.name = "wow";
        return userBean;
    }
}
//默认是通过方法的方法名作为id来获取bean对象
UserBean userBean = (UserBean) context.getBean("userBeanMethod");

方法注解的重命名 

注意:重命名后就不能使用方法名作为id获取Bean对象了 

@Bean(name = {"user","wualala"})//还可以对其id进行重命名
                          //注意是重命名噢,重命名之和就不能用方法名来获取bean对象了
                          //注解中的name也可以是一个数组的形式,即有多个id.

注解获取Bean对象

获取Bean对象,也可以称为对象配备对象注入.

主要使用Autowired注解与Resource注解

被注入的对象的类也要加上类注解噢

获取Bean对象的方法主要有三种:

  1. 属性注入
  2. 构造方法注入
  3. setter注入

Autowired注解的方式 

1.属性注入

使用Autowired注解

@Controller//加入Controller注解,将类注册到spring当中
public class UserController {

    @Autowired//使用注解注入Bean对象userService
                //在注入后就可以在UserController对象中使用userService对象
    private UserService userService;

    public UserService getUserService(){
        return userService;
    }
}

2.构造方法注入

spring收到请求后,根据UserService类创建一个对象,并传到Autowired注解的构造方法中 

@Controller//加入Controller注解,将类注册到spring当中
public class UserController {

    private UserService userService;

    @Autowired//注解
    public UserController(UserService userService){
        this.userService = userService;
    }

    public UserService getUserService(){
        return userService;
    }
}

如果只有一个构造方法,则可以不用Autowired注解 

3.setter注入

在属性的setter方法上使用注解注入对象

@Controller//加入Controller注解,将类注册到spring当中
public class UserController {

    private UserService userService;
    public UserService getUserService(){
        return userService;
    }
    @Autowired//setter方法上加上注释
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}

@Resource与@Autowired的区别 

还有另一种注入对象的注解:

@Resource,其和@Autowired使用的方法是一致的.

但也有主要的区别:

  1. @Autowired来自于Spring,@Resource来自于JDK的注解
  2. 可设置的参数不同,@Autowried不可设置id.@Resource可以设置name,根据id获取bean对象
  3. @Autowried可以使用属性注入,构造方法注入与setter注入,而@Resource只能使用属性注入与setter注入,不能使用构造方法的注入.

同一个类多个@Bean的方法

在UserBean类中注解了多个方法存储Bean对象

在UserController类中注入的时候,需要将注入的Bean对象进行命名.

不然会因为同一个类的Bean对象不唯一而报错

但@Autowried本身并不能进行重命名,需要搭配另一个注解@Qualifier来进行重命名

或直接使用@Resource注解

同一个类有多个Bean存储

@Component//使用了方法注解的类要搭配类注解来使用
          //因为spring要先扫描到UserBean类才能接着扫描此类里面的添加了注解的userBean方法
public class UserBean {
    private String name;

    @Bean
    public UserBean user1(){
        UserBean userBean = new UserBean();
        userBean.name = "haha";
        return userBean;
    }

    @Bean
    public UserBean user2(){
        UserBean userBean = new UserBean();
        userBean.name = "dongdong";
        return userBean;
    }

    public void getName(){
        System.out.println(name);
    }
}

获取指定Bean对象方法一: 

@Autowried + @Qualifier

@Controller
public class UserController {

    @Autowired
    @Qualifier("user2")//注解定义名称
    private UserBean userBean;

    public UserBean getUserBean() {
        return userBean;
    }
}

获取指定Bean对象方法二:

@Controller
public class UserController {

    @Resource(name = "user1")
    private UserBean userBean;

    public UserBean getUserBean() {
        return userBean;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值