🍎一. 五大类存储 Bean 对象
上一篇博客我们已经可以实现基本的 Spring 读取和存储对象的操作了,但在操作的过程中我们发现读取和存储对象并没有想象中的那么“简单”,所以接下来我们要学习更加简单的操作 Bean 对象的⽅法
在 Spring 中想要更简单的存储和读取对象的核⼼是使⽤注解,也就是我们接下来要学习 Spring 中的相关注解,来存储和读取 Bean 对象
🍒1.1 配置spring-config.xml扫描路径
我们在resources文件下系创建一个带有.xml
类型的文件,不是必须要设置成spring-config.xml
这个名称的文件,只是方便我们辨识,或者工作中使用
之前我们存储 Bean 时,需要在 spring-config 中添加⼀⾏ bean 注册内容才⾏,这样会很麻烦,有可能我们在编写程序时忘记注册Bean的属性就会报错,不仅降低了工作效率,还增加了编程时产生的错误,⽽现在我们只需要⼀个注解就可以替代之前要写⼀⾏配置的尴尬了,不过在开始存储对象之前,我们先要来点准备:⼯作配置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.beans"></content:component-scan> </beans>
🍒1.2 添加五大类注解存储 Bean 对象
想要将对象存储在 Spring 中,有两种注解类型可以实现:
- 类注解:
@Controller、@Service、@Repository、@Component、@Configuration
🍉1.2.1 @Controller(控制器存储)
使⽤ @Controller 存储 bean 的代码如下所示:
@Controller public class UserController { public void sayHi(String name){ System.out.println("Hi"+ name); } }
读取 bean 的代码:
public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); UserController controller = context.getBean("userController",UserController.class); UserService service = context.getBean("userService",UserService.class); controller.sayHi("张三"); } }
🍉1.2.2 @Service(服务存储)
使⽤ @Service 存储 bean 的代码如下所示:
@Service public class UserService { public void sayHi(String name) { System.out.println("Hi" + name); } }
读取 bean 的代码:
public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); UserController controller = context.getBean("userController",UserController.class); UserService service = context.getBean("userService",UserService.class); service.sayHi("李四"); } }
🍉1.2.3 @Repository(仓库存储)
以上同理
@Repository public class UserRepository { public void sayHi(String name){ System.out.println("你好:" + name); } }
public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); UserController controller = context.getBean("userController",UserController.class); UserService service = context.getBean("userService",UserService.class); UserRepository repository = context.getBean("userRepository",UserRepository.class); // controller.sayHi("张三"); // service.sayHi("李四"); repository.sayHi("王五"); } }
🍉1.2.4 @Component(组件存储)
以上同理
@Component public class UserComponent { public void sayHi(String name){ System.out.println("你好:" + name); } }
public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); UserController controller = context.getBean("userController",UserController.class); UserService service = context.getBean("userService",UserService.class); UserRepository repository = context.getBean("userRepository",UserRepository.class); UserComponent component = context.getBean("userComponent",UserComponent.class); // controller.sayHi("张三"); // service.sayHi("李四"); // repository.sayHi("王五"); component.sayHi("赵六"); } }
🍉1.2.5 @Configuration(配置存储)
以上同理
@Configuration public class UserConfiguration { public void sayHi(String name){ System.out.println("你好:" + name); } }
public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); UserController controller = context.getBean("userController",UserController.class); UserService service = context.getBean("userService",UserService.class); UserRepository repository = context.getBean("userRepository",UserRepository.class); UserComponent component = context.getBean("userComponent",UserComponent.class); UserConfiguration configuration = context.getBean("userConfiguration",UserConfiguration.class); // controller.sayHi("张三"); // service.sayHi("李四"); // repository.sayHi("王五"); // component.sayHi("赵六"); configuration.sayHi("七七"); } }
🍒1.3 五大注解类小结
🍉1.3.1 为什么使用五大类注解
我们拿去银行办业务举例:
@Controller
层就是保安,先要进行检查验证,然后到达Service
服务厅询问业务,不同的业务来到Repository
,不同的窗口,然后进行相应的工作人员办理业务!
🍉1.3.2 五大类注解之间的关系
我们可以在注解类的源码内看到@Controller、@Service、@Repository、@Configuration都是继承@Component
也就是说@Component是@Controller、@Service、@Repository、@Configuration这四个类的父类