spring注释基础配置

时间:2015-09-25 12:08:44

标签: java spring

如何编写基于完全注释的配置

@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中配置任何内容?

2 个答案:

答案 0 :(得分:2)

首先清理代码,在界面上添加注释是不行的。下一个@Configuration@Component,因此这个类配置或组件不会尝试同时执行这两个操作。一般来说是个坏主意。

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

所有这些都会导致以下代码。

使用构造函数而不是setter或field injection的MessageServiceHelper

@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

@Component
public class SmsServiceImpl implements MessageService {
    @Override
    public boolean sendMessage(String message) {
        System.out.println("SmsServiceImpl.sendMessage " + message);
        return true;
    }
}

@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 :(得分:-1)

您可以通过4种方式配置弹簧上下文:

  1. XML配置
  2. 基于注释的配置
  3. 基于Java的配置
  4. Groovy配置文件
  5. 在每个配置中,您必须告诉spring在哪里找到spring bean定义。

    对于基于Java的配置,你可以尝试这样的东西来引导spring上下文:

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

    有关详细信息,请参阅as Andrew pointed out

相关问题