6、Spring 中定义 Bean 的八种方式
大约 1 分钟
6、Spring 中定义 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);
}
}
<bean />
8.最古老的方式
@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/;