如何寫一個完全基於註解配置春天註解基地配置
@Component
public class MessageServiceHelper {
@Autowired
@Qualifier("emailService")
private MessageService messageService;
public MessageService getMessageService() {
return messageService;
}
public void setMessageService(MessageService messageService) {
this.messageService = messageService;
}
public boolean sendMessage(String message){
return this.messageService.sendMessage(message);
}
@Component
@Qualifier("emailService")
public class EmailServiceImpl implements MessageService {
@Override
public boolean sendMessage(String message) {
System.out.println("EmailServiceImpl.sendMessage " + message);
return true;
}
}
@Component
public interface MessageService {
boolean sendMessage(String message);
}
@Component
public class SmsServiceImpl implements MessageService {
@Override
public boolean sendMessage(String message) {
System.out.println("SmsServiceImpl.sendMessage " + message);
return true;
}
}
public class Application {
@Autowired
MessageServiceHelper messageServiceHelper;
@Autowired
public Application application;
public static void main(String[] args) {
//How can I get messageservicehelper here and call the method.
}
}
所以基本上的事情是我不想在我的任何配置XML文件,我怎麼能做到這一點?
更新
我不想使用javabased配置是它可以配置Spring沒有配置在XML什麼?
您已經使用基於Java的配置,因爲你有一個'@ Configuration'類... –
所以你不想使用註釋或XML配置? –
@ M.Deinum我已刪除@配置 – eatSleepCode