網(wǎng)站微信建設(shè)運(yùn)營經(jīng)驗分享沈陽疫情最新消息
Spring注解使用中常見的概念性問題
- @Configuration有什么用?
- @Configuration和XML有什么區(qū)別?哪種好?
- @Autowired 、 @Inject、@Resource 之間有什么區(qū)別?
- @Value、@PropertySource 和 @Configuration?
- Spring如何處理帶@Configuration @Import的類?
- @Profile有什么用?
@Configuration有什么用?
- @Configuration表明一個類中聲明一個和多個@Bean標(biāo)記的方法,并且這些方法被Spring容器管理用于生成Bean定義以及在運(yùn)行時這些Bean的服務(wù)請求。
- 其實相當(dāng)于原來的聲明了多個bean的xml配置文件,而且被@Configuration也相當(dāng)于一個組件。
- 加入@Configuration 注解,表明這就是一個配置類。有一個myBean()的方法并用@Bean 進(jìn)行注釋,返回一個MyBean()的實例,表明這個方法是需要被Spring進(jìn)行管理的bean。@Bean 如果不指定名稱的話,默認(rèn)使用myBean名稱,也就是小寫的名稱。
@Configuration
public class AppConfig {@Beanpublic MyBean myBean(){return new MyBean();}
}
- 可以通過使用 AnnotationConfigApplicationContext 來引導(dǎo)啟動這個@Configuration 注解的類(在web項目中,也可以使用AnnotationContextWebApplicationContext或者其他變體來啟動)
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class);
ctx.refresh();
MyBean myBean = ctx.getBean(MyBean.class);
// use myBean ...
- 另外可以通過使用XML方式開啟基于注解的啟動,在/resources 目錄下新建 application-context.xml 代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
><!-- 相當(dāng)于基于注解的啟動類 AnnotationConfigApplicationContext--><context:annotation-config /><bean class="com.linyf.demo.config.MyConfiguration"/>
</beans>
@Configuration和XML有什么區(qū)別?哪種好?
@Autowired 、 @Inject、@Resource 之間有什么區(qū)別?
- @Inject: 這是jsr330 的規(guī)范,通過AutowiredAnnotationBeanPostProcessor 類實現(xiàn)的依賴注入。位于javax.inject包內(nèi),是Java自帶的注解。
- @Autowired: Spring提供的注解,通過AutowiredAnnotationBeanPostProcessor 類實現(xiàn)注入。位于org.springframework.beans.factory.annotation 包內(nèi)。
- @Resource: @Resource 是jsr250規(guī)范的實現(xiàn),通過CommonAnnotationBeanPostProcessor 類實現(xiàn)注入。@Resource 一般會指定一個name屬性
區(qū)別:
@Autowired和@Inject基本是一樣的,因為兩者都是使用AutowiredAnnotationBeanPostProcessor來處理依賴注入。但是@Resource是個例外,它使用的是CommonAnnotationBeanPostProcessor來處理依賴注入。當(dāng)然,兩者都是BeanPostProcessor。
@Value、@PropertySource 和 @Configuration?
- @Configuration 可以和@Value 和@PropertySource 一起使用讀取外部配置文件,
Spring如何處理帶@Configuration @Import的類?
詳情可以查看這篇文章:Spring/SpringBoot系列之SpringBoot 源碼常用注解【九】
@Profile有什么用?
@Profile: 表示當(dāng)一個或多個@Value 指定的配置文件處于可用狀態(tài)時,組件符合注冊條件,可以進(jìn)行注冊。
三種設(shè)置方式:
-
可以通過ConfigurableEnvironment.setActiveProfiles()以編程的方式激活
-
可以通過AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME (spring.profiles.active )屬性設(shè)置為JVM屬性
-
作為環(huán)境變量,或作為web.xml 應(yīng)用程序的Servlet 上下文參數(shù)。也可以通過@ActiveProfiles 注解在集成測試中以聲明方式激活配置文件。
作用域: -
作為類級別的注釋在任意類或者直接與@Component 進(jìn)行關(guān)聯(lián),包括@Configuration 類
-
作為原注解,可以自定義注解
-
作為方法的注解作用在任何方法
注意:
如果一個配置類使用了Profile 標(biāo)簽或者@Profile
作用在任何類中都必須進(jìn)行啟用才會生效,如果@Profile({“p1”,“!p2”}) 標(biāo)識兩個屬性,那么p1 是啟用狀態(tài) 而p2
是非啟用狀態(tài)的。
詳見: https://blog.csdn.net/fei1234456/article/details/106905054/