欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot定義Bean的幾種實現(xiàn)方式

 更新時間:2023年05月22日 14:32:35   作者:ACGkaka_  
本文主要介紹了SpringBoot定義Bean的幾種實現(xiàn)方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

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相當(dāng)于對于Controller的切面,可以綁定PropertyEditor。

(類似于AOP,但是底層不是AOP實現(xiàn)。)

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

這是我們其他所有方法的底層實現(xiàn)。

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注解的實現(xiàn)類
?*
?* @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面試八股文的變態(tài)方法

到此這篇關(guān)于SpringBoot定義Bean的幾種實現(xiàn)方式的文章就介紹到這了,更多相關(guān)SpringBoot定義Bean內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java中的關(guān)鍵字synchronized 詳解

    Java中的關(guān)鍵字synchronized 詳解

    這篇文章主要介紹了Java中的關(guān)鍵字synchronized,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • java中BIO、NIO、AIO都有啥區(qū)別

    java中BIO、NIO、AIO都有啥區(qū)別

    這篇文章主要介紹了java中BIO、NIO、AIO都有啥區(qū)別,IO模型就是說用什么樣的通道進行數(shù)據(jù)的發(fā)送和接收,Java共支持3種網(wǎng)絡(luò)編程IO模式:BIO,NIO,AIO,文中有非常詳細的代碼示例,對正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • Java實現(xiàn)redis分布式鎖的三種方式

    Java實現(xiàn)redis分布式鎖的三種方式

    本文主要介紹了Java實現(xiàn)redis分布式鎖的三種方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Java8中的default方法詳解

    Java8中的default方法詳解

    這篇文章主要介紹了Java8中的default方法詳解,Java 8新增了default方法,它可以在接口添加新功能特性,而且還不影響接口的實現(xiàn)類,需要的朋友可以參考下
    2015-03-03
  • Log4j.properties配置及其使用

    Log4j.properties配置及其使用

    本文主要介紹了Log4j.properties配置及其使用,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • SpringMVC使用@PathVariable接收參數(shù)過程解析

    SpringMVC使用@PathVariable接收參數(shù)過程解析

    這篇文章主要介紹了SpringMVC使用@PathVariable接收參數(shù)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • eclipse springboot工程打war包方法及再Tomcat中運行的方法

    eclipse springboot工程打war包方法及再Tomcat中運行的方法

    這篇文章主要介紹了eclipse springboot工程打war包方法及再Tomcat中運行的方法,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-08-08
  • Java并發(fā)編程之關(guān)鍵字volatile知識總結(jié)

    Java并發(fā)編程之關(guān)鍵字volatile知識總結(jié)

    今天帶大家學(xué)習(xí)java的相關(guān)知識,文章圍繞著Java關(guān)鍵字volatile展開,文中有非常詳細的知識總結(jié),需要的朋友可以參考下
    2021-06-06
  • mapstruct的用法之qualifiedByName示例詳解

    mapstruct的用法之qualifiedByName示例詳解

    qualifiedByName的意思就是使用這個Mapper接口中的指定的默認方法去處理這個屬性的轉(zhuǎn)換,而不是簡單的get?set,今天通過本文給大家介紹下mapstruct的用法之qualifiedByName示例詳解,感興趣的朋友一起看看吧
    2022-04-04
  • 詳解Java中LinkedHashMap

    詳解Java中LinkedHashMap

    本文主要介紹了Java中LinkedHashMap的相關(guān)知識,具有很好的參考價值。下面跟著小編一起來看下吧
    2017-05-05

最新評論