英文:
SPRINGBOOT - Auto resolve which dependency to inject Globally
问题
以下是翻译好的内容:
通常我喜欢根据接口进行注入(我在JS/TS中使用这种方式),并且“注册一个令牌”指向默认实现。因此,如果有一天我需要创建一个新的实现,我不需要更改整个代码,我只需要将那个令牌更改为另一个实现。
我曾尝试在Spring中实现同样的功能。然而,我仍在学习有关其IOC和DI的内容,我找不到类似这样的东西。
因此,我的问题是,在我的控制器中当我注入该接口时,我是否需要使用“@Qualifier”,或者在我的实现中指定哪一个是**“@Primary”,以便@Autowired可以注入正确的实现。使用@Qualifier()**的策略很好,因为它允许我按名称指定,但如果将来需要更改实现,我仍然需要在整个项目中进行更改。
但我想知道是否有一种方式可以全局定义一个配置,将默认注入点指向单个实现?
提前致谢。如果我的解释不清楚,请告诉我。
控制器方法
@Autowired
private IOrdinaryService iOrdinaryService;
@GetMapping("/")
void getMethodHangler() {
iCreateProjectService.execute(newEmployee);
}
接口
package com.example.demo.modules.projects.services;
import com.example.demo.modules.projects.entities.Projects;
public interface IOrdinaryService{
void execute();
}
实现 1
@Service
public class OldWayOrdinaryService implements IOrdinaryService {
@Override
public void execute() {
System.out.println("foo");
}
}
实现 2
@Service
public class NewWayOrdinaryService implements IOrdinaryService {
@Override
public void execute() {
System.out.println("bar");
}
}
英文:
I usually like to do(I do this in JS/TS) the injection based on a Interface,and "register a token" pointing to a default Implementation.So If I someday need to create a new Implementation I don't need to change the whole code, I only need to change that Token to another implementation.
I was trying to do the same in Spring. However I'm still learning about its IOC and DI, and I could not find something like this.
So my question is, in my controller when I'm injecting the Interface, I need to use a "@Qualifier", or specify which one is a "@Primary" in my implementation so the @Autowired can Inject the correct implementation. The strategy with @Qualifier() is good because it allows me to specify it by name, but I still would need to change in the Whole project if someday the implementation needs to be changed.
But I was wondering if there's some way to globally define a configuration pointing to a default injection point to a single implementation?
Thanks in advance. Sorry if my explanation is not clear,please let me know
Controller Method
@Autowired
private IOrdinaryService iOrdinaryService;
@GetMapping("/")
void getMethodHangler() {
iCreateProjectService.execute(newEmployee);
}
Interface
package com.example.demo.modules.projects.services;
import com.example.demo.modules.projects.entities.Projects;
public interface IOrdinaryService{
void execute();
}
Implementation 1
@Service
public class OldWayOrdinaryService implements IOrdinaryService {
@Override
public void execute() {
System.out.println("foo");
}
}
Implementation 2
@Service
public class NewWayOrdinaryService implements IOrdinaryService {
@Override
public void execute() {
System.out.println("bar");
}
}
答案1
得分: 6
你可以通过Spring
框架的配置类实现这一点:
@Configuration
public class InjectionConfiguration {
@Bean
public IOrdinaryService ordinaryService() {
return new OldWayOrdinaryService();
}
}
这样,你就不需要在实现类上使用@Service
注解,因此将其移除,在你的控制器类中使用@Autowired
注入,即可获得所需的实例。
当你想要更改IOrdinaryService
的实现时,只需修改上述的配置方法。
英文:
You can achieve this with configuration classes of Spring
framework:
@Configuration
public class InjectionConfiguration {
@Bean
public IOrdinaryService ordinaryService() {
return new OldWayOrdinaryService();
}
}
This way, you don't need to use @Service
annotation on the implementations, so remove it, and you will get the instance you need in your controller class with @Autowired.
When you want to change the implementation of a IOrdinaryService
, you just change the above configuration method.
答案2
得分: 0
你可以将实现类的名称作为系统属性外部化,然后在配置方法中使用它来进行注入或自动连线。
英文:
You can externalize implementation class name as a system property that you can use in configuration method to inject or autowire.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论