Qas*_*sim 37 java spring dependency-injection spring-boot
我正在开发一个SpringBoot项目,我想用它的名字来获取bean applicationContext.我已尝试过很多来自网络的解决方案,但无法成功.我的要求是我有一个控制器
ControllerA
Run Code Online (Sandbox Code Playgroud)
在控制器内我有一个方法getBean(String className).我想获取已注册bean的实例.我有hibernate实体,我想通过仅在getBean方法中传递类的名称来获取bean的实例.
如果有人知道解决方案,请帮忙.
reo*_*eos 65
您可以将ApplicationContext自动装配为字段
@Autowired
private ApplicationContext context;
Run Code Online (Sandbox Code Playgroud)
或方法
@Autowired
public void context(ApplicationContext context) { this.context = context; }
Run Code Online (Sandbox Code Playgroud)
最后用
context.getBean(SomeClass.class)
Run Code Online (Sandbox Code Playgroud)
Sun*_*amy 33
您可以使用ApplicationContextAware.
ApplicationContextAware:
接口由希望被通知ApplicationContext中,它运行英寸实现此接口有意义例如当对象需要访问一组协作bean的任何对象来实现.
有几种方法可以获得对应用程序上下文的引用.您可以实现ApplicationContextAware,如以下示例所示:
package hello;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
@Component
public class ApplicationContextProvider implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public ApplicationContext getContext() {
return applicationContext;
}
}
Run Code Online (Sandbox Code Playgroud)
更新:
当春天实例豆,它查找了ApplicationContextAware实现,如果发现他们,()方法将被调用的setApplicationContext.
通过这种方式,Spring正在设置当前的 applicationcontext.
来自Spring的代码片段source code:
private void invokeAwareInterfaces(Object bean) {
.....
.....
if (bean instanceof ApplicationContextAware) {
((ApplicationContextAware)bean).setApplicationContext(this.applicationContext);
}
}
Run Code Online (Sandbox Code Playgroud)
一旦获得对Application上下文的引用,就可以使用getBean()获取所需的bean.
实际上,您想从Spring引擎中获取对象,该引擎已经在Spring应用程序启动时(Spring引擎的初始化)维护了所需类的对象。现在,您只需要将该对象获取到参考。
在服务班级
@Autowired
private ApplicationContext context;
SomeClass sc = (SomeClass)context.getBean(SomeClass.class);
Run Code Online (Sandbox Code Playgroud)
现在,在sc的参考中,您具有该对象。希望解释得很好。如有任何疑问,请通知我。
使用SpringApplication.run(Class<?> primarySource, String... arg)对我有用。例如:
@SpringBootApplication
public class YourApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(YourApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
作为替代方法,您可以使用获取任何用,或ConfigurableApplicationContext注释的类的 bean 。@Component@Repository@Service
假设您想要获取BaseComponent类的 bean :
@Service
public class BaseComponent {
public String getMessage() {
return "hello world";
}
}
Run Code Online (Sandbox Code Playgroud)
现在你可以使用ConfigurableApplicationContext来获取 bean:
@Component
public class DemoComponent {
@Autowired
ConfigurableApplicationContext applicationContext;
public BaseComponent getBeanOfBaseComponent() {
return applicationContext.getBean(BaseComponent.class);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
150674 次 |
| 最近记录: |