SpringBoot定義Bean的幾種實現(xiàn)方式
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,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03SpringMVC使用@PathVariable接收參數(shù)過程解析
這篇文章主要介紹了SpringMVC使用@PathVariable接收參數(shù)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10eclipse springboot工程打war包方法及再Tomcat中運行的方法
這篇文章主要介紹了eclipse springboot工程打war包方法及再Tomcat中運行的方法,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08Java并發(fā)編程之關(guān)鍵字volatile知識總結(jié)
今天帶大家學(xué)習(xí)java的相關(guān)知識,文章圍繞著Java關(guān)鍵字volatile展開,文中有非常詳細的知識總結(jié),需要的朋友可以參考下2021-06-06mapstruct的用法之qualifiedByName示例詳解
qualifiedByName的意思就是使用這個Mapper接口中的指定的默認方法去處理這個屬性的轉(zhuǎn)換,而不是簡單的get?set,今天通過本文給大家介紹下mapstruct的用法之qualifiedByName示例詳解,感興趣的朋友一起看看吧2022-04-04