在给定运行时值的情况下,我找不到一种简单的方法来注入组件/服务.
我开始阅读@ 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) 我正在尝试写一个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的定义.
说我有一个服务接口下面的结构ServiceInterface和实现它几个部分组成:ProductAService和ProductBService我也有一个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) 我有一个带@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.是否可能以及如何实现?
我有以下界面:
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)