Spring更简单的读取和存储对象

Spring更简单的读取和存储对象

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

1.存储Bean对象

之前我们存储Bean对象时,需要在spring-config.xml文件中添加一行信息,如图
在这里插入图片描述
⽽现在我们只需要⼀个注解就可以替代之前要写⼀⾏配置的麻烦了。

1.1配置扫描路径

在 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="example"></content:component-scan>
</beans>

其中包名是Spring扫描的路径

1.2添加注解

即告诉Spring要扫描哪些类,所以需要使用五大注解告诉Spring,要管理哪些类
想要将对象存储在 Spring 中,有两种注解类型可以实现:

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

使⽤ @Controller 存储 bean 的代码如下所示:

@Controller
public class UserController {
    public void doController(){
        System.out.println("hi,Controller");


    }
}
1.2.2@Service

使⽤ @Service 存储 bean 的代码如下所示:

@Service
public class UserService {
    public void doService(){
        System.out.println("hi,service");
    }
}
1.2.3@Repository

使⽤ @Repository 存储 bean 的代码如下所示:

@Repository
public class UserRepository {
    public void doRepository(){
        System.out.println("hi,repository");
    }
}
1.2.4 @Component

使⽤ @Component 存储 bean 的代码如下所示:

@Component
public class UComponent {
    public void doComponent(){
        System.out.println("hi,component");
    }
}
1.2.5@Configuration

使⽤ @Configuration 存储 bean 的代码如下所示:

@Configuration
public class UserConfiguration {
    public void doConfiguration(){
        System.out.println("hi,configuration ");
    }
}

读取 bean 的代码:

public class Main {
    public static void main(String[] args) {
        //获取Spring上下文
        ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");
        
        //使用五大注解获取对象
        UserController userController= (UserController) context.getBean("userController");//和类名一样并把首字母改为小写,两个都大写时不变,就大写就行
        userController.doController();//调用bean方法

        UserService userservice= (UserService) context.getBean("userService");
        userservice.doService();

        UserRepository userrepository= (UserRepository) context.getBean("userRepository");
        userrepository.doRepository();

       UComponent usercomponent= (UComponent) context.getBean("UComponent");
       usercomponent.doComponent();


        UserConfiguration userConfiguration= (UserConfiguration) context.getBean("userConfiguration");
        userConfiguration.doConfiguration();
    }
}

结果如图:
在这里插入图片描述

1.3 五个注解的含义与关系

之所以有多个注解,就是让程序员看到类注解之后,就能直接了解当前类
的⽤途,
@Controller:表示的是业务逻辑层,验证接口参数的正确性或参数的转换
@Servie:服务层;
@Repository:持久层,仓库,数据库
@Component:组件
@Configuration:配置层

关系:通过以下源码我们可以看出,其他四类注解(@Controller
@Servie @Repository @Configuration:)本身就是属于 @Component 的“⼦类”
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1.3.1Bean命名规则

我们可以知道Bean对象是通过首字母大写来命名的,读取时存在以下情况
1)若Bean命名时前两个字母都大写,则通过Bean名称来获取对象,如:

@Component
//前俩个字母都大写
public class UComponent {
    public void doComponent(){
        System.out.println("hi,component");
    }
}
//获取对象
 UComponent usercomponent= (UComponent)   context.getBean("UComponent");
       usercomponent.doComponent();

2)若只有一个首字母大写,则将首字母改成小写来获取对象,如:

@Controller
//只有一个首字母大写
public class UserController {
    public void doController(){
        System.out.println("hi,Controller")
    }
}
UserController userController= (UserController) context.getBean("userController");//把首字母改为小写,
  userController.doController();//调用bean方法

3)进行重命名

@Configuration("conf")//重命名了
public class UserConfiguration {
    public void doConfiguration(){
        System.out.println("hi,configuration 重命名了");
    }
}
UserConfiguration userConfiguration= (UserConfiguration) context.getBean("conf");
        userConfiguration.doConfiguration();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值