SpringBoot定义Bean的几种实现方式
作者:ACGkaka_
本文主要介绍了SpringBoot定义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>
参考地址:
到此这篇关于SpringBoot定义Bean的几种实现方式的文章就介绍到这了,更多相关SpringBoot定义Bean内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!