Spring更简单的读取和存储对象(一) · 存储Bean对象 · 配置扫描路径 · 五种类注解存储Bean对象 · 方法注解@Bean的使用

之前我们学过存储对象到容器,再从容器里面取出来。这次我们要学习更加简单的操作 Bean 对象的方法。

在 Spring 中想要更加简单的存储和读取对象的核心是使用注解。

存储 Bean 对象

之前我们存储 Bean 时,需要在 spring-config.xml 文件中添加一行 bean 注册内容才行。

而现在,只需要一个注解就可以代替之前要写的一行配置,不过在开始存储对象之前,我们先要做些准备工作。


一、前置工作:配置扫描路径(重要)

想要将对象成功的存储到 Spring 中,我们需要配置一下存储对象的扫描包路径,只有被配置的包下的所有类,添加了注解才能被正确的识别并保存到 Spring 中。

在 spring-config.xml 添加如下配置:

<?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="com.bit.service">
    </content:component-scan>

</beans>

如果不在配置的扫描包下的类对象,即使添加了注解,也是不能被存储到 Spring 中的。

其中标红的一行为注册扫描的包。


二、添加注解存储 Bean 对象

想要将对象存储到 Spring 中,有两种六个注解类型可以实现:

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

2.1 @Controller(控制器存储)

使用 @Controller 存储的 bean 代码如下:

@Controller
public class UserController {
    public void sayHi(String name) {
        System.out.println("hi," + name);
    }
}

此时我们使用之前读取对象的方式来读取上面的 UserController 对象.

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    UserController userC = (UserController) context.getBean("userController");
    userC.sayHi("controller");
}

执行结果.


2.2 @Service(服务存储)

使用 @Service 存储 bean 的代码.

@Service
public class UserService {
    public void sadHi(String name) {
        System.out.println("hi," + name);
    }
}

读取 bean 的代码.

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    UserService userService = (UserService) context.getBean("userService");
    userService.sadHi("service");
}

运行结果.


2.3 @Repository(仓库存储)

使用 @Repository 存储 bean 的代码如下所示.

@Repository
public class UserRepository {
    public void sayHi(String name){
        System.out.println("hi," + name);
    }
}

读取 bean 的代码.

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    UserRepository userRepository = (UserRepository) context.getBean("userRepository");
    userRepository.sayHi("Repository");
}

2.4 @Component(组件存储)

使用 @Component 存储 bean 的代码如下.

@Component
public class UserComponent {
    public void sayHi(String name) {
        System.out.println("hi," + name);
    }
}

读取 bean 的代码.

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    UserComponent userComponent = (UserComponent) context.getBean("userComponent");
    userComponent.sayHi("Component");
}

2.5 @Configuration(配置存储)

使用 @Configuration 存储 bean 的代码如下.

@Configuration
public class UserConfiguration {
    public void sayHi(String name) {
        System.out.println("hi," + name);
    }
}

读取 bean 的代码.

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    UserConfiguration userConfiguration = (UserConfiguration) context.getBean("userConfiguration");
    userConfiguration.sayHi("configuration");
}

三、为什么要这么多类注解?

我们发现,类注解的五个注解,几乎没有什么区别,功能一样。
为什么需要这么多类注解呢?

其实这和我们路上跑的汽车车牌一样,广东的车牌号是:粤字 开头,广州的车牌号是粤A 开头,深圳是粤B 开头,惠州是粤L。
这样做的好处除了可以节约号码之外,更重要的作用是能直观的标识一辆车的归属地。

这些类注解也是同样的功能,能让程序员很直观得了解当前类的用途,例如:

@Controller:表示是业务逻辑层,校验请求是否合法。
@Service:表示服务层,分配请求路径。
@Repository:表示持久层,将数据进行持久化处理。
@Configuration:表示配置层,配置当前项目。
@Component:表示组件层,进行数据的传递和承载。

程序的工程分层,调用流程如下.


3.1 类注解之间的关系

查看 @Controller / @Service / @Repository / @Configuration 等注解的源码发现.

这些注解里面都有一个注解 @Component,说明它们本身就是属于 @Component 的 “子类”


3.2 注意 Bean 的命名

通过上面示例,我们可以看出,通常我们 bean 使用都是标准的大驼峰命名,而读取的时候首字母小写就可以获取到 bean 了。

然鹅,当我们首字母和第二个字母都是大写时,就不能正常读取到 bean 了。

这时候,我们就要查询 Spring 关于 bean 存储时生成的命名规则了。

我们可以在 IDEA 中使用搜索关键字 “beanName” (我的IDEA直接双击shift开启搜索):

我们在最后找到了 bean 对象的命名规则的方法:

它使用的是 JDK Introspector 中的 decapitalize 方法
源码如下:

所以回到之前报错的代码,我们只需要修改两个都是大写即可。


四、方法注解 @Bean

类注解是添加到某个类上的,而方法注解是放到某个方法上的。

创建一个测试的 UserInfo 实体类

public class UserInfo {
    private int id;
    private String name;
    private String password;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

在 service 目录下,创建 UserBeans 类,写测试代码。

public class UserBeans {

    @Bean
    public UserInfo user(){
        UserInfo userInfo = new UserInfo();
        userInfo.setId(1);
        userInfo.setName("java");
        userInfo.setPassword("123456");
        return userInfo;
    }
}

最后在 main 方法中取 bean 对象运行

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    UserInfo userInfo = context.getBean("user", UserInfo.class);
    System.out.println(userInfo.toString());
}

可是,我们写完上述代码,尝试获取 bean 对象中的 user1 时发现,根本获取不到.


4.1 方法注解要配合类注解使用

在 Spring 框架的设计中,方法注解 @Bean 要配合类注解才能将对象正常的存储到 Spring 容器中。

@Component
public class UserBeans {

    @Bean
    public UserInfo user(){
        UserInfo userInfo = new UserInfo();
        userInfo.setId(1);
        userInfo.setName("java");
        userInfo.setPassword("123456");
        return userInfo;
    }
}

再次运行代码.


4.2 重命名 Bean

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

@Component
public class UserBeans {

    @Bean(name = {"u1"})
    public UserInfo user(){
        UserInfo userInfo = new UserInfo();
        userInfo.setId(1);
        userInfo.setName("java");
        userInfo.setPassword("123456");
        return userInfo;
    }
}

此时我们使用 u1 就可以获取到 UserInfo 对象了.

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
    UserInfo userInfo = context.getBean("u1", UserInfo.class);
    System.out.println(userInfo.toString());
}

运行结果.

补充说明:

这个重命名的 name 其实是一个数组,一个 bean 可以有多个名字。

    @Bean(name = {"u1", "userInfo"})
    public UserInfo user(){
        UserInfo userInfo = new UserInfo();
        userInfo.setId(1);
        userInfo.setName("java");
        userInfo.setPassword("123456");
        return userInfo;
    }

并且 "name = " 可以省略。

    @Bean({"u1", "userInfo"})
    public UserInfo user(){
        UserInfo userInfo = new UserInfo();
        userInfo.setId(1);
        userInfo.setName("java");
        userInfo.setPassword("123456");
        return userInfo;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值