新聞中心
默認(rèn)情況下,Spring容器中的注解配置沒有被打開。因此,在我們使用基于注解的配置之前,我們需要在Spring配置文件中啟用它。因此,如果你想在你的Spring應(yīng)用程序中使用任何注解,請考慮以下配置文件。

@Required注解是方法級注解,適用于Bean的setter方法。這個注解簡單地表明setter方法必須在配置時被配置為具有依賴注入的值。
@Component
@Component
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Required
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
屬性movieFinder必須存在,若注入時為null,會拋NullPointerException異常。
@Autowired
我們可以使用@Autowired來標(biāo)記Spring將要解析和注入的依賴關(guān)系。我們可以在構(gòu)造函數(shù)、Setter或字段注入中使用這個注解。
構(gòu)造函數(shù)注入
class Car {
private Engine engine;
@Autowired
Car(Engine engine) {
this.engine = engine;
}
}
Setter 注入
class Car {
private Engine engine;
@Autowired
void setEngine(Engine engine) {
this.engine = engine;
}
}
字段注入
class Car {
@Autowired
private Engine engine;
}
@Primary
當(dāng)有多個相同類型的Bean時,使用@Primary來給一個Bean更高的優(yōu)先權(quán)。
為什么需要@Primary?在某些情況下,我們需要注冊超過一個相同類型的Bean。在這個例子中,我們有mySQLConnection()和oracleConnection()的Connection類型的bean。
@Configuration
public class Config {
@Bean
public Connection mySQLConnection() {
return new MySQLConnection();
}
@Bean
public Connection oracleConnection() {
return new OracleConnection();
}
}
如果我們運(yùn)行該應(yīng)用程序,Spring會拋出NoUniqueBeanDefinitionException。為了訪問具有相同類型的Bean,我們通常使用@Qualifier("beanName")注解,和@Autowired一起應(yīng)用。在我們的案例中,我們在配置階段配置Bean,所以@Qualifier不能在這里應(yīng)用。關(guān)于@Qualifier會在本章后面講到。
為了解決這個問題,Spring提供了@Primary注解。下面的例子展示了如何在一個Spring應(yīng)用程序中使用@Primary注解。
@Primary注解可用于任何直接或間接用@Component注解的類或用@Bean注解的工廠方法。在這個例子中,我們將使用@Primary注解和@Component注解。
package com.demo.spring.primary;
public interface MessageService {
public void sendMsg();
}
package com.demo.spring.primary;
import org.springframework.stereotype.Component;
@Component
public class FacebookMessageService implements MessageService {
@Override
public void sendMsg() {
System.out.println("FacebookMessageService implementation here");
}
}
package com.demo.spring.primary;
import org.springframework.stereotype.Component;
@Component
public class EmailMessageService implements MessageService {
@Override
public void sendMsg() {
System.out.println("EmailMessageService Implementation here");
}
}
package com.demo.spring.primary;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Primary
@Component
public class TwitterMessageService implements MessageService {
@Override
public void sendMsg() {
System.out.println("TwitterMessageService Implementation here");
}
}
請注意,在上述TwitterMessageService類中,我們添加了@Primary與@Component注解。
package com.demo.spring.primary;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.demo.spring.primary")
public class AppConfig {
}
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MessageService messageService = context.getBean(MessageService.class);
messageService.sendMsg();
context.close();
}
}
Output:
TwitterMessageService Implementation here
@Qualifier
當(dāng)我們創(chuàng)建了多個相同類型的Bean,但只想將其中一個與某個屬性關(guān)聯(lián)??梢允褂聾Qualifier注解和@Autowired注解來控制。
@Qualifier用于解決模糊的依賴關(guān)系,也就是說,它幫助@Autowired注解選擇其中一個依賴關(guān)系。如果一個接口有多種實(shí)現(xiàn),那么我們可以在運(yùn)行時使用@Qualifier來選擇所需的實(shí)現(xiàn)。
public interface MessageService {
public void sendMsg(String message);
}
public class EmailService implements MessageService{
public void sendMsg(String message) {
System.out.println(message);
}
}
public class TwitterService implements MessageService{
public void sendMsg(String message) {
System.out.println(message);
}
}
public class SMSService implements MessageService{
public void sendMsg(String message) {
System.out.println(message);
}
}
public interface MessageProcessor {
public void processMsg(String message);
}
public class MessageProcessorImpl implements MessageProcessor {
private MessageService messageService;
// setter based DI
@Autowired
@Qualifier("TwitterService")
public void setMessageService(MessageService messageService) {
this.messageService = messageService;
}
// constructor based DI
@Autowired
public MessageProcessorImpl(@Qualifier("TwitterService") MessageService messageService) {
this.messageService = messageService;
}
public void processMsg(String message) {
messageService.sendMsg(message);
}
}
@Configuration
@ComponentScan("com.demo.springframework.di")
public class AppConfiguration {
@Bean(name="emailService")
public MessageService emailService(){
return new EmailService();
}
@Bean(name="twitterService")
public MessageService twitterService(){
return new TwitterService();
}
@Bean(name="smsService")
public MessageService smsService(){
return new SMSService();
}
@Bean
public MessageProcessor messageProcessor(){
return new MessageProcessorImpl(twitterService());
}
}
public class TestApplication {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfiguration.class);
MessageProcessor userService = applicationContext.getBean(MessageProcessor.class);
userService.processMsg("twitter message sending ");
}
}
Output:
twitter message sending
@Resource
Spring支持通過@Resource(javax.annotation.Resource)方式注入,可以在字段或setter方法上加該注解,@Resource有一個name屬性,如果不指定name,默認(rèn)就是字段或setter方法名稱。
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Resource(name="myMovieFinder")
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Resource
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}
@Value
該注解可用于向Spring管理的Bean中的字段注入值,它可以應(yīng)用于字段或構(gòu)造函數(shù)/方法參數(shù)級別。
我們需要一個屬性文件來定義我們想用@Value注解注入的值。因此,我們首先需要在我們的配置類中定義一個@PropertySource--帶有屬性文件名。
value.from.file=Value got from the file
priority=high
listOfValues=A,B,C
注意:Spring Boot默認(rèn)配置了一個PropertySourcesPlaceholderConfigurer的Bean,它將從application.properties和application.yml文件中獲取屬性。
@Value("${value.from.file}")
private String valueFromFile;有時,我們需要注入一堆值。將它們定義為屬性文件中單個屬性用逗號分隔的值,或定義為系統(tǒng)屬性并注入一個數(shù)組。例如listOfValues中定義了逗號分隔的值,所以數(shù)組的值將是["A","B", "C"]。
@Value("${listOfValues}")
private String[] valuesArray;
使用SpEL表達(dá)式
使用SpEL表達(dá)式獲取配置文件中priority的值。
@Value("#{systemProperties['priority']}")
private String spelValue;如果在property文件中沒有定義屬性,就會報nullvalue異常。為了防止這種情況,我們可以在SpEL表達(dá)式中提供一個默認(rèn)值。如果屬性沒有定義,我們就為該字段設(shè)置默認(rèn)值。
@Value("#{systemProperties['unknown'] ?: 'some default'}")
private String spelSomeDefault;我們也可以用其它bean的屬性值:
@Value("#{someBean.someValue}")
private Integer someBeanValue;從屬性配置中獲取List列表:
@Value("#{'${listOfValues}'.split(',')}")
private List valuesList; 把@Value注入到Map加入propeties文件中有如下key:
valuesMap={key1: '1', key2: '2', key3: '3'}注意,Map中的值必須是單引號。
@Value("#{${valuesMap}}")
private Map valuesMap; 如果要獲取map中指定的key:
@Value("#{${valuesMap}.key1}")
private Integer valuesMapKey1;如果不確定Map中是否包含key,我們可以使用一個安全的表達(dá)式,使其不會拋出異常,并且設(shè)置值為null:
@Value("#{${valuesMap}['unknownKey']}")
private Integer unknownMapKey;為屬性設(shè)置默認(rèn)值:
@Value("#{${unknownMap : {key1: '1', key2: '2'}}}")
private Map unknownMap;
@Value("#{${valuesMap}['unknownKey'] ?: 5}")
private Integer unknownMapKeyWithDefaultValue; 注入前過濾不需要的值:
@Value("#{${valuesMap}.?[value>'1']}")
private Map valuesMapFiltered;
與構(gòu)造函數(shù)一起注入
@Component
@PropertySource("classpath:application.properties")
public class PriorityProvider {
private String priority;
@Autowired
public PriorityProvider(@Value("${priority:normal}") String priority) {
this.priority = priority;
}
// standard getter
}
注意:默認(rèn)配置文件是application.properties或application.yaml則不需要@PropertySource注解。
與Setter一起注入
@Component
@PropertySource("classpath:values.properties")
public class CollectionProvider {
private Listvalues = new ArrayList<>();
@Autowired
public void setValues(@Value("#{'${listOfValues}'.split(',')}") Listvalues) {
this.values.addAll(values);
}
// standard getter
}
@PostConstruct and @PreDestroy
我們可以自定義Bean的創(chuàng)建和銷毀附動作。我們可以通過實(shí)現(xiàn)InitializingBean和DisposableBean接口來實(shí)現(xiàn)它。我們也可以用@PostConstruct和@PreDestroy注解來實(shí)現(xiàn)。
@PostConstruct
Spring只調(diào)用一次用@PostConstruct注釋的方法,就在Bean屬性初始化之后。請記住,即使沒有任何東西需要初始化,這些方法也會運(yùn)行。帶有@PostConstruct注釋的方法不能是靜態(tài)的。
@Component
public class DbInit {
@Autowired
private UserRepository userRepository;
@PostConstruct
private void postConstruct() {
User admin = new User("admin", "admin password");
User normalUser = new User("user", "user password");
userRepository.save(admin, normalUser);
}
}
@PreDestroy
帶有@PreDestroy注解的方法只運(yùn)行一次,就在Spring將我們的Bean從容器中移除之前。帶有@PreDestroy注解的方法不能是靜態(tài)的。
@Component
public class UserRepository {
private DbConnection dbConnection;
@PreDestroy
public void preDestroy() {
dbConnection.close();
}
}
網(wǎng)站標(biāo)題:Spring框架之基于注解的容器配置
文章分享:http://fisionsoft.com.cn/article/dppdcph.html


咨詢
建站咨詢
