【IocDI】_存储Bean的五大类注解及getBean的使用
目录
1. Bean的存储
1.1 类注解
1.1.1 @Controller:控制器存储
1.1.2 @Service:服务存储
1.1.3 @Repository:仓库存储
1.1.4 @Component:组件存储
1.1.5 @Configuration:配置存储
1.2 五大类注解之间的关系
2. getBean的使用
2.1 关于Spring context
2.2 关于getBean方法获取Bean的方式
2.3 根据不同参数获取对象
2.4 获取不同的Bean
1. Bean的存储
在上文实现IoC基本存取功能的两个注解中,要把某个对象交给IOC容器管理,需要在类上添加⼀个@Component注解。
Spring框架为了更好的服务web应⽤程序, 提供了更丰富的注解。
共有两类注解类型可以实现:
(1) 类注解:@Controller、@Service、@Repository、@Component、@Configuration;
(2)方法注解:@Bean;
1.1 类注解
1.1.1 @Controller:控制器存储
在Controller包下创建一个UserController类:
package com.example.iocdemo1.Controller;
import org.springframework.stereotype.Controller;
@Controller
public class UserController {
public void doController(){
System.out.println("do Controller...");
}
}
@Controller对应Controller层;
1.1.2 @Service:服务存储
在Service包下创建一个UserService类:
package com.example.iocdemo1.Service;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public void doService(){
System.out.println("doService...");
}
}
@Service对应Service层;
1.1.3 @Repository:仓库存储
在Repository包下创建一个UserRepository类:
package com.example.iocdemo1.Repo;
import org.springframework.stereotype.Repository;
@Repository
public class UserRepository {
public void doRepository(){
System.out.println("doRepository...");
}
}
@Repository对应Dao层;
1.1.4 @Component:组件存储
在Component包下创建一个UserComponent类:
package com.example.iocdemo1.Component;
import org.springframework.stereotype.Component;
@Component
public class UserComponent {
public void doComponent(){
System.out.println("doComponent...");
}
}
在系统中,对于非Controller层、非Service层和非Dao层的其他bean,使用@Component注解实现存储;
1.1.5 @Configuration:配置存储
在Config包下创建一个UserConfig类:
package com.example.iocdemo1.Config;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfig {
public void doConfig(){
System.out.println("doConfig...");
}
}
在系统中,对于配置相关的bean使用@Configuration实现存储;
1.2 五大类注解之间的关系
1、Spring提供的以上更为细化的注解,可以帮助程序员直观了解到当前类的用途:
@Controller:控制层,接收请求, 对请求进行处理, 并进行响应;
@Servie:业务逻辑层, 处理具体的业务逻辑;
@Repository:数据访问层,也称为持久层。负责数据访问操作;
(@Controller、@Service、@Repository与三层架构分别对应)
@Configuration:配置层, 处理项⽬中的⼀些配置信息;
2、@Controller、@Service、@Repository、@Configuration都是@Component的衍生类,都是基于@Component实现的:(Alt+点击查看注解声明)
3、如果某一个Bean需要被外界访问,则必须使用@Controller:
@Controller是访问程序的入口。
4、五大注解只能加在类上,并且只能加在自己的代码上。如果引入了一个第三方的Jar包也希望交给Spring管理,则无法加五大注解。可以使用@Bean注解,这是一个方法注解。具体内容另见后文。
2. getBean的使用
2.1 关于Spring context
对于Spring项目,项目创建成功后,在左侧目录中可见一个启动类:
在启动类中对以上五大类注解是否实现将对应bean存放到Spring容器中进行测试。
启动类初始页面如下:
采用ApplicationContext 类型的变量接收Spring应用启动的返回值,接收的是Spring的运行环境,Spring的运行环境即Spring context,通常将其称为Spring上下文:
@SpringBootApplication
public class IoCDemo1Application {
public static void main(String[] args) {
// Spring上下文(表示启动Spring应用,返回Spring的运行环境)
ApplicationContext context=
SpringApplication.run(IoCDemo1Application.class, args);
}
}
2.2 关于getBean方法获取Bean的方式
context是一个ApplicationContext类型的变量,提供了一个getBean方法,根据传参不同可通过不同方式获取对象:
现以存储器对象(Controller)为例,使用getBean方法根据类型获取UserController对象并调用其方法:
package com.example.iocdemo1;
import com.example.iocdemo1.Controller.UserController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class IoCDemo1Application {
public static void main(String[] args) {
// Spring上下文
ApplicationContext context=
SpringApplication.run(IoCDemo1Application.class, args);
// 根据类型获取Bean
UserController bean = context.getBean(UserController.class);
bean.doController();
}
}
此时在控制台,就会有对应的输出。过程分析如下:
若无对应的@Controller注解,(现注释掉@Controller注解,再启动程序),则运行项目时会报未找到Bean异常:
2.3 根据不同参数获取对象
在getBean的各种参数调用中,最常用的是根据类型、名称、类型和名称三种方式。
现以服务对象(Service)为例,使用getBean方法根据类型获取UserService对象并调用其方法:
package com.example.iocdemo1;
import com.example.iocdemo1.Service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class IoCDemo1Application {
public static void main(String[] args) {
// Spring上下文
ApplicationContext context=
SpringApplication.run(IoCDemo1Application.class, args);
// 从context中根据类型获取Bean
UserService userService1=context.getBean(UserService.class);
userService1.doService();
// 从context中根据名称获取Bean
UserService userService2=(UserService)context.getBean("userService");
userService2.doService();
// 从context中根据名称和类型获取Bean
UserService userService3 = context.getBean("userService", UserService.class);
userService3.doService();
}
}
此时在控制台就会有bean的方法的相关输出:
注:关于根据名称获取Bean的细节:Bean的名称规范:
Spring官网对于bean的介绍如下:
若需了解更多内容,详见链接如下:Bean Overview :: Spring Framework
2.4 获取不同的Bean
对应1.1 部分关于@Controller、@Service、@Repository、@Configuration、@Component实现的不同Bean,以通过类型获取Bean为例,获取不同的Bean:
package com.example.iocdemo1;
import com.example.iocdemo1.Component.UserComponent;
import com.example.iocdemo1.Config.UserConfig;
import com.example.iocdemo1.Controller.UserController;
import com.example.iocdemo1.Repo.UserRepository;
import com.example.iocdemo1.Service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class IoCDemo1Application {
public static void main(String[] args) {
// Spring上下文
ApplicationContext context=
SpringApplication.run(IoCDemo1Application.class, args);
UserController controller = context.getBean(UserController.class);
controller.doController();
UserService userService1=context.getBean(UserService.class);
userService1.doService();
UserRepository repository = context.getBean(UserRepository.class);
repository.doRepository();
UserComponent component = context.getBean(UserComponent.class);
component.doComponent();
UserConfig config = context.getBean(UserConfig.class);
config.doConfig();
}
}
控制台输出如下: