詳解Java如何使用注解來配置Spring容器
介紹
我們將介紹如何在Java代碼中使用注解來配置Spring容器。它包括:
- Basic Concepts: @Bean and @Configuration。
- Instantiating the Spring Container by Using 。
- AnnotationConfigApplicationContext。
- Using the @Bean Annotation。
- Using the @Configuration annotation。
- Composing Java-based Configurations。
- Bean Definition Profiles。
- PropertySource Abstraction。
- Using @PropertySource。
- Placeholder Resolution in Statements。
@Bean and @Configuration
@Bean注解用在一個方法上表示實例化、配置和初始化一個新對象,由Spring IoC容器管理。對于那些熟悉Spring的 XML配置的人來說,@Bean注解的作用與元素的作用相同。
用@Configuration來注解一個類,表明它的主要目的是作為一個bean定義的來源。此外,@Configuration類允許通過調(diào)用同一個類中的其他@Bean方法來定義Bean間的依賴關系。最簡單的@Configuration類如下:
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
@Bean
public OtherService otherService() {
return new OtherService();
}
}AnnotationConfigApplicationContext實例化容器
與實例化
ClassPathXmlApplicationContext時使用Spring XML文件作為輸入的方式相同,你可以在實例化AnnotationConfigApplicationContext時使用@Configuration類作為輸入。這使得Spring容器的使用完全不需要XML,如下例子:
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
}通過使用 register(Class...) 以編程方式構建容器
你可以通過使用無參數(shù)構造函數(shù)來實例化AnnotationConfigApplicationContext,然后使用 register() 方法來配置它。這種方法在以編程方式構建 AnnotationConfigApplicationContext 時特別有用。下面的例子展示了如何做到這一點。
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class, OtherConfig.class);
ctx.register(AdditionalConfig.class);
ctx.refresh();
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
}@ComponentScan啟用組件掃描
為了啟用組件掃描,可以在@Configuration類做如下注釋。
@Configuration
@ComponentScan(basePackages = "com.acme")
public class AppConfig {
// ...
}Bean的依賴
@Configuration
public class AppConfig {
@Bean
public TransferService transferService(AccountRepository accountRepository) {
return new TransferServiceImpl(accountRepository);
}
}生命周期回調(diào)
任何用@Bean注解定義的類都支持常規(guī)的生命周期回調(diào),并且可以使用JSR-250的@PostConstruct和@PreDestroy注解。如果一個bean實現(xiàn)了InitializingBean、DisposableBean或Lifecycle,它們各自的方法將被容器調(diào)用。
public class BeanOne {
public void init() {
// initialization logic
}
}
public class BeanTwo {
public void cleanup() {
// destruction logic
}
}
@Configuration
public class AppConfig {
@Bean(initMethod = "init")
public BeanOne beanOne() {
return new BeanOne();
}
@Bean(destroyMethod = "cleanup")
public BeanTwo beanTwo() {
return new BeanTwo();
}
}Bean指定作用域
Bean默認的作用域是singleton,更多Bean作用域可參考Bean作用域章節(jié)。
@Configuration
public class MyConfiguration {
@Bean
@Scope("prototype")
public Encryptor encryptor() {
// ...
}
}自定義bean名稱
默認情況下,配置類使用@Bean方法的名稱作為Bean的名稱??梢酝ㄟ^name屬性來自定義名稱,如下:
@Configuration
public class AppConfig {
@Bean("myThing")
public Thing thing() {
return new Thing();
}
}Bean別名
@Configuration
public class AppConfig {
@Bean({"dataSource", "subsystemA-dataSource", "subsystemB-dataSource"})
public DataSource dataSource() {
// instantiate, configure and return DataSource bean...
}
}Bean注入之間的依賴
@Configuration
public class AppConfig {
@Bean
public BeanOne beanOne() {
return new BeanOne(beanTwo());
}
@Bean
public BeanTwo beanTwo() {
return new BeanTwo();
}
}@Import
@Import注解表示要導入一個或多個@Configuration類。在導入的@Configuration類中聲明的@Bean定義應該通過使用@Autowired注入來訪問。
@Configuration
public class ConfigA {
@Bean
public A a() {
return new A();
}
}
@Configuration
@Import(ConfigA.class)
public class ConfigB {
@Bean
public B b() {
return new B();
}
}現(xiàn)在,在實例化上下文時不需要同時指定ConfigA類和ConfigB類,而只需要明確提供ConfigB:
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigB.class);
// now both beans A and B will be available...
A a = ctx.getBean(A.class);
B b = ctx.getBean(B.class);
}@ImportResource
Spring提供了一個@ImportResource注解,用于從applicationContext.xml文件中加載bean到應用上下文中。
@Configuration
@ImportResource("classpath:/com/acme/properties-config.xml")
public class AppConfig {
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource() {
return new DriverManagerDataSource(url, username, password);
}
}<!-- properties-config.xml -->
<beans>
<context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>
</beans>@PropertySource
我們將討論如何使用@PropertySource來讀取屬性文件,并用@Value和Environment來顯示值。
@PropertySource注解為向Spring的環(huán)境添加PropertySource提供了一種方便的聲明性機制。要與@Configuration類一起使用。
假設我們從config.properties文件中讀取數(shù)據(jù)庫配置,并使用Environment將這些屬性值設置為DataSourceConfig類。
@Configuration
@PropertySource("classpath:config.properties")
public class ProperySourceDemo implements InitializingBean {
@Autowired
Environment env;
@Override
public void afterPropertiesSet() throws Exception {
setDatabaseConfig();
}
private void setDatabaseConfig() {
DataSourceConfig config = new DataSourceConfig();
config.setDriver(env.getProperty("jdbc.driver"));
config.setUrl(env.getProperty("jdbc.url"));
config.setUsername(env.getProperty("jdbc.username"));
config.setPassword(env.getProperty("jdbc.password"));
System.out.println(config.toString());
}
}支持多個properties文件
@Configuration
@PropertySources({
@PropertySource("classpath:config.properties"),
@PropertySource("classpath:db.properties")
})
public class AppConfig {
//...
}ApplicationContext
ApplicationContext實現(xiàn)了BeanFactory接口,并提供了如下功能:
- 通過MessageSource接口,訪問i18n風格的消息。
- 通過ResourceLoader接口訪問資源,如URL和文件。
- 事件發(fā)布,即通過使用ApplicationEventPublisher接口,向?qū)崿F(xiàn)ApplicationListener接口的bean發(fā)布。
- 通過HierarchicalBeanFactory接口加載多個(分層的)上下文,讓每個上下文專注于一個特定的層,例如一個應用程序的Web層。
MessageSource 國際化
ApplicationContext接口擴展了一個名為MessageSource的接口,因此,它提供了國際化("i18n")功能。Spring還提供了HierarchicalMessageSource接口,它可以分層次地解析消息。
account.name=TestAccount
@Configuration
public class AppConfig {
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("config/messages");
return messageSource;
}
}@Service
public class AccountService {
@Autowired
private MessageSource messageSource;
public void someMsg() {
messageSource.getMessage("account.name", null, Locale.ENGLISH);
//todo
}
}以上就是詳解Java如何使用注解來配置Spring容器的詳細內(nèi)容,更多關于Java注解配置Spring容器的資料請關注腳本之家其它相關文章!
相關文章
SpringCloud?@RefreshScope刷新機制淺析
RefeshScope這個注解想必大家都用過,在微服務配置中心的場景下經(jīng)常出現(xiàn),他可以用來刷新Bean中的屬性配置,那大家對他的實現(xiàn)原理了解嗎?它為什么可以做到動態(tài)刷新呢2023-03-03
SpringBoot 策略模式實現(xiàn)切換上傳文件模式
策略模式是指有一定行動內(nèi)容的相對穩(wěn)定的策略名稱,這篇文章主要介紹了SpringBoot 策略模式 切換上傳文件模式,需要的朋友可以參考下2023-11-11
IDEA查看所有的斷點(Breakpoints)并關閉的方式
我們在使用IDEA開發(fā)Java應用時,基本上都需要進行打斷點的操作,這方便我們排查BUG,也方便我們查看設計的是否正確,不過有時候,我們不希望進入斷點,所以我們需要快速關閉所有斷點,故本文給大家介紹了IDEA查看所有的斷點(Breakpoints)并關閉的方式2024-10-10
SpringCloud Eureka服務的基本配置和操作方法
Eureka是Netflix開源的一個基于REST的服務治理框架,主要用于實現(xiàn)微服務架構中的服務注冊與發(fā)現(xiàn),Eureka是Netflix開源的服務發(fā)現(xiàn)框架,用于在分布式系統(tǒng)中實現(xiàn)服務的自動注冊與發(fā)現(xiàn),本文介紹SpringCloud Eureka服務的基本配置和操作方法,感興趣的朋友一起看看吧2023-12-12
IDEA報java:?java.lang.OutOfMemoryError:?Java?heap?space錯誤
這篇文章主要給大家介紹了關于IDEA報java:?java.lang.OutOfMemoryError:?Java?heap?space錯誤的解決辦法,文中將解決的辦法介紹的非常詳細,需要的朋友可以參考下2024-01-01
SpringBoot處理form-data表單接收對象數(shù)組的方法
form-data則是一種更加靈活的編碼方式,它可以處理二進制數(shù)據(jù)(如圖片、文件等)以及文本數(shù)據(jù),這篇文章主要介紹了SpringBoot處理form-data表單接收對象數(shù)組,需要的朋友可以參考下2023-11-11

