相关疑难解决方法(0)

如何使用运行时"限定符"变量动态注入服务?

在给定运行时值的情况下,我找不到一种简单的方法来注入组件/服务.

我开始阅读@ Spring的文档:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-autowired-annotation-qualifiers 但我找不到如何变量传递给@Qualifier注释的值.

假设我有一个具有这种界面的模型实体:

public interface Case {

    String getCountryCode();
    void setCountryCode(String countryCode);

}
Run Code Online (Sandbox Code Playgroud)

在我的客户端代码中,我会做类似的事情:

@Inject
DoService does;

(...)

Case myCase = new CaseImpl(); // ...or whatever
myCase.setCountryCode("uk");

does.whateverWith(myCase);
Run Code Online (Sandbox Code Playgroud)

......我的服务是:

@Service
public class DoService {

    @Inject
    // FIXME what kind of #$@& symbol can I use here?
    // Seems like SpEL is sadly invalid here :(
    @Qualifier("${caze.countryCode}")
    private CaseService caseService;

    public void whateverWith(Case caze) {
        caseService.modify(caze);
    }

}
Run Code Online (Sandbox Code Playgroud)

我希望caseService是UKCaseService(参见下面的相关代码).

public interface CaseService {

    void modify(Case caze); …
Run Code Online (Sandbox Code Playgroud)

service spring dependency-injection autowired

18
推荐指数
1
解决办法
2万
查看次数

使用spring的条件bean

我正在尝试写一个ValidatorFactory会根据其类型给我一个验证器

public Validator getNewValidator(ValidatorType type){

    switch:
         case a : new Validator1();
         break;
          case b : new Validator2();
        break;

}
Run Code Online (Sandbox Code Playgroud)

我想用spring xml bean定义来编写

我可以使用方法注入,但它只允许我创建一个对象,而方法可以

不接受任何争论.

我不想使用FactoryBean..我只是想看看我们是否可以使用spring xml来做到这一点

bean的定义.

java spring dependency-injection

13
推荐指数
2
解决办法
2万
查看次数

Spring 3中的Custom Autowire候选bean

说我有一个服务接口下面的结构ServiceInterface和实现它几个部分组成:ProductAServiceProductBService我也有一个RequestContext是有说,我们说目前正在处理产品A产品B或一个合格资产豆.然后,如何将自动装配或其他注释自动注入正确的实现(ProductAService或ProductBService)到需要它的某个服务(ServiceThatNeedsServiceInterface如下).

public interface ServiceInterface {
  void someMethod();
}

@Component(name="ProductAService")
public class ProductAService implements ServiceInterface {
  @Override public void someMethod() { 
    System.out.println("Hello, A Service"); 
  }
}

@Component(name="ProductBService")
public class ProductBService implements ServiceInterface {
  @Override public void someMethod() { 
    System.out.println("Hello, B Service"); 
  }
}

@Component
public class ServiceThatNeedsServiceInterface {

  // What to do here???
  @Autowired
  ServiceInterface service;

  public void useService() {
    service.someMethod();
  }
}

@Component
@Scope( value = WebApplicationContext.SCOPE_REQUEST )
public class …
Run Code Online (Sandbox Code Playgroud)

java spring annotations dependency-injection

10
推荐指数
1
解决办法
1万
查看次数

JavaConfig中的Spring Bean别名

我有一个带@Service注释的类,它提供了我可以在所有项目中使用的核心功能:

@Service
public class MyService {}
Run Code Online (Sandbox Code Playgroud)

和另一个扩展它以实现项目特定的东西:

@Service
public class ExtendedMyService extends MyService {}
Run Code Online (Sandbox Code Playgroud)

现在我想配置一个bean别名,以便@Qualifier("MyServiceAlias")在使用属性自动装配它时使用:

# MyService qualifier (default: myService)
myService.qualifier=extendedMyService
Run Code Online (Sandbox Code Playgroud)

在XML中它看起来像:

<alias name="${myService.qualifier}" alias="MyServiceAlias" />
Run Code Online (Sandbox Code Playgroud)

这里也讨论了它,但我只需要使用XML,JavaConfig.是否可能以及如何实现?

java spring dependency-injection spring-bean

7
推荐指数
1
解决办法
1万
查看次数

类需要一个bean,但是找到了2个:

我有以下界面:

public interface MailSender {
    void sender(String to, String subject,String body);
}
Run Code Online (Sandbox Code Playgroud)

有2个实现:

public class SmtpkMailSender implements MailSender   {

    static Log log=LogFactory.getLog(MailSender.class); 

    public void sender(String to, String subject,String body){
        log.info("SMTP To: "+to);
        log.info("SMTP Subjecy: "+subject);
        log.info("SMTP body: "+body);
    }

}
Run Code Online (Sandbox Code Playgroud)

第二个是:

@Primary
public class MockMailSender implements MailSender   {

    static Log log=LogFactory.getLog(MailSender.class); 

    public void sender(String to, String subject,String body){
        log.info("To: "+to);
        log.info("Subject: "+subject);
        log.info("body: "+body);
    }

}
Run Code Online (Sandbox Code Playgroud)

我将依赖项注入到控制器类中,如下所示:

@RestController
public class MailController {

    @Autowired
    private MailSender smtpkMailSender;

    @RequestMapping("/send")
    public String send(){ …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc

4
推荐指数
2
解决办法
1万
查看次数