• SpringBoot中有几种定义Bean的方式?


    1.@Bean

    @Bean
    public DemoService demoService() {
        return new DemoService();
    }
    
    • 1
    • 2
    • 3
    • 4

    2.@Component

    @Component
    public class DemoService {
    }
    
    • 1
    • 2
    • 3

    3.@Controller、@RestController、@Service、@Repository

    @RestController
    public class DemoController{
        @GetMapping("/test")
        public String test() {
            return "succeed";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    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;
    
    /**
     * 

    @Title DemoControllerAdvice *

    @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; } }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    注意:@ControllerAdvice相当于对于Controller的切面,可以绑定PropertyEditor。

    (类似于AOP,但是底层不是AOP实现。)

    5.@Configuration

    @Configuration
    public class DemoConfig {
    }
    
    • 1
    • 2
    • 3

    注意:@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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    7.BeanDefinition

    这是我们其他所有方法的底层实现。

    MyApplication.java

    @SpringBootApplication
    @Import(DemoImportBeanDefinitionRegistrar.class)
    public class MyApplication {
        public static void main(String[] args) {
            SpringApplication.run(MyApplication.class);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    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;
    
    /**
     * 

    @Title DemoImportBeanDefinationRegistar *

    @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); } }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    8.

    最古老的方式

    @SpringBootApplication
    @ImportResource("classpath:spring.xml")
    public class MyApplication {
        public static void main(String[] args) {
            SpringApplication.run(MyApplication.class);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    spring.xml

    
    <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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    整理完毕,完结撒花~ 🌻





    参考地址:

    1.【最新最全】一周刷完Java面试八股文的变态方法,https://www.bilibili.com/video/BV1NT411W7NA/

  • 相关阅读:
    Web安全测试(五):XSS攻击—存储式XSS漏洞
    力扣21 - 合并两个有序链表【归并排序思维】
    cf #825 Div.2(A~C2)
    Redis 主从复制和哨兵监控,实现Redis高可用配置
    MinIO Enterprise Cache:实现超性能的分布式 DRAM 缓存
    踏进字节的那一瞬间,我泪目了,这437天的外包经历值了....
    Python 0基础_变现_38岁_day 15(匿名函数)
    Kubernetes CKA 模拟题解析【2022最新版】(连载001)
    Python知识点(史上最全)
    opencv车牌识别<一>
  • 原文地址:https://blog.csdn.net/qq_33204709/article/details/130471018