2015-09-25 110 views
0

如何写一个完全基于注解配置春天注解基地配置

@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什么?

+0

您已经使用基于Java的配置,因为你有一个'@ Configuration'类... –

+0

所以你不想使用注释或XML配置? –

+0

@ M.Deinum我已删除@配置 – eatSleepCode

回答

2

首先对清理你的代码放在注释一个接口不会工作。接下来@Configuration@Component,那么这个类配置或者一个组件不会尝试这两者。通常是一个糟糕的主意

您正在使用字段级注释,因此您的getter和setter不添加任何内容,只添加未使用的代码。我也建议使用构造函数注入而不是setter或field注入。 (关于为什么野外注射是邪恶的阅读this post)。

所有这些应用会导致下面的代码。

MessageServiceHelper用构造函数而不是setter或field注入。

@Component 
public class MessageServiceHelper { 
    private final MessageService messageService; 

    @Autowired 
    public MessageServiceHelper(@Qualifier("emailService") MessageService messageService) { 
     this.messageService=messageService; 
    } 

    public boolean sendMessage(String message){ 
     return this.messageService.sendMessage(message); 
    } 
} 

MessageService及其实现。

public interface MessageService { 
    boolean sendMessage(String message); 
} 

EmailServiceImpl

@Component 
@Qualifier("emailService") 
public class EmailServiceImpl implements MessageService { 

    @Override 
    public boolean sendMessage(String message) { 
     System.out.println("EmailServiceImpl.sendMessage " + message); 
     return true; 
    } 
} 

SmsServiceImpl

@Configuration

@Configuration 
@ComponentScan(basePackages = "demo.di") 
public class ApplicationConfiguration {} 

而且该应用程序的启动器。

public class Application 

    public static void main(String[] args) { 
     ApplicationContext ctx = new AnnotationConfigApplicationContext(ApplicationConfiguration.class); 
     MessageHelper helper = ctx.getBean(MessageHelper.class); 
     helper.sendMessage("Hello World!"); 
    } 
} 

如果你不想使用@Configuration(最终你会当你想使用类似交易的事情,AOP等,它们)的使用,这需要String...的构造,并通过demo.di,而不是类的。这将从该包开始扫描所有@Component s。

public class Application 

    public static void main(String[] args) { 
     ApplicationContext ctx = new AnnotationConfigApplicationContext("demo.di"); 
     MessageHelper helper = ctx.getBean(MessageHelper.class); 
     helper.sendMessage("Hello World!"); 
    } 
} 
-1

有4种方式,你可以配置Spring上下文:

  1. XML配置
  2. 注解为基础的配置
  3. 基于Java的配置
  4. Groovy的配置文件

在每你必须告诉spring哪里可以找到spring bean的定义。

与基于Java的配置的情况下,你可以尝试这样的事情来引导Spring上下文:

AnnotationConfigApplicationContext context = 
    new AnnotationConfigApplicationContext (Application.class); 
MessageServiceHelper helper= context.getBean(MessageServiceHelper.class); 

参考Spring Docs更多信息

+0

这是旧的,很旧,不再维护。那是Spring的java配置项目,现在(大部分)已经融入了Spring的核心。 –

+0

@ M.Deinum真。编辑我的答案 – pomkine