SpringBoot中有几种定义Bean的方式?
目录
- 1.@Bean
- 2.@Component
- 3.@Controller、@RestController、@Service、@Repository
- 4.@ControllerAdvice、@RestControllerAdvice
- 5.@Configuration
- 6.@Import
- 7.BeanDefinition
- 8.\<bean /\>
1.@Bean
@Bean
public DemoService demoService() {
return new DemoService();
}
2.@Component
@Component
public class DemoService {
}
3.@Controller、@RestController、@Service、@Repository
@RestController
public class DemoController{
@GetMapping("/test")
public String test() {
return "succeed";
}
}
4.@ControllerAdvice、@RestControllerAdvice
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
/**
* <p> @Title DemoControllerAdvice
* <p> @Description Controller增强
*
* @author ACGkaka
* @date 2023/4/25 21:07
*/
@ControllerAdvice
public class DemoControllerAdvice implements ResponseBodyAdvice {
@Override
public boolean supports(MethodParameter methodParameter, Class aClass) {
return true;
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
System.out.println("body is: " + body);
return body;
}
}
注意:@ControllerAdvice相当于对于Controller的切面,可以绑定PropertyEditor。
(类似于AOP,但是底层不是AOP实现。)
5.@Configuration
@Configuration
public class DemoConfig {
}
注意:@Configuration 主要标识一个Bean是一个配置Bean,利用这个Bean可以对Spring进行配置,比如扫描路径、定义其他的Bean。
6.@Import
@SpringBootAppilcation
@Import(Demo.class)
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class);
}
}
7.BeanDefinition
这是我们其他所有方法的底层实现。
MyApplication.java
@SpringBootApplication
@Import(DemoImportBeanDefinitionRegistrar.class)
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class);
}
}
DemoImportBeanDefinitionRegistrar.java
import com.demo.service.DemoService;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
/**
* <p> @Title DemoImportBeanDefinationRegistar
* <p> @Description @Import注解的实现类
*
* @author ACGkaka
* @date 2023/4/25 21:18
*/
public class DemoImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
// 定义Bean
beanDefinition.setBeanClass(DemoService.class);
// 注册Bean
registry.registerBeanDefinition("demoService", beanDefinition);
}
}
8.<bean />
最古老的方式
@SpringBootApplication
@ImportResource("classpath:spring.xml")
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class);
}
}
spring.xml
<?xml version="1.0" encoding="UTF-7"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="demoService" class="com.demo.service.DemoService" />
</beans>
整理完毕,完结撒花~ 🌻
参考地址:
1.【最新最全】一周刷完Java面试八股文的变态方法,https://www.bilibili.com/video/BV1NT411W7NA/