Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何注入ApplicationContext本身_Java_Spring_Applicationcontext - Fatal编程技术网

Java 如何注入ApplicationContext本身

Java 如何注入ApplicationContext本身,java,spring,applicationcontext,Java,Spring,Applicationcontext,我想将ApplicationContext本身注入bean 差不多 public void setApplicationContext(ApplicationContect context) { this.context = context; } 这在春季可能吗?使用ApplicationContextAware界面很容易 public class A implements ApplicationContextAware { private ApplicationContext con

我想将
ApplicationContext
本身注入bean

差不多

public void setApplicationContext(ApplicationContect context) {
  this.context = context;
}

这在春季可能吗?

使用
ApplicationContextAware
界面很容易

public class A implements ApplicationContextAware {
  private ApplicationContext context;

  public void setApplicationContext(ApplicationContext context) {
      this.context = context;
  }
}
然后在实际的applicationContext中,您只需要引用bean

<bean id="a" class="com.company.A" />

是的,只需实现-接口。

前面的评论可以,但我通常更喜欢:

@Autowired private ApplicationContext applicationContext;

我在上面看到一些关于@Autowired不能正常工作的评论。以下内容可能会有所帮助

这是行不通的:

@Route(value = "content", layout = MainView.class)
public class MyLayout extends VerticalLayout implements RouterLayout {

  @Autowired private ApplicationContext context;

   public MyLayout() {
    comps.add(context.getBean(MyComponentA.class)); // context always null :(
}
您必须这样做:

 @Autowired
  public MyLayout(ApplicationContext context) {
    comps.add(context.getBean(MyComponentA.class)); //context is set :)
}

或者这个:


@PostConstruct
    private void init() {
    comps.add(context.getBean(MyComponentA.class)); // context is set :)
}
还要注意,上传是另一个必须在@PostConstruct范围内设置的组件。这对我来说是一场噩梦。希望这有帮助

我差点忘了提到@Scope注释对于您的Bean可能是必要的,如下所示。在Bean中使用Upload时就是这种情况,因为UI在创建Bean之前没有实例化/附加,并且会导致抛出Null引用异常。在使用@Route时不会这样做,但在使用@Component时会这样做-因此后者不是一个选项,如果@Route不可行,那么我建议使用@Configuration类创建具有原型范围的bean

@Configuration
public class MyConfig {
  @Bean
  @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS)
  public MyComponentA getMyBean() {
    return new MyComponentA();
  }
}

部分解决方案-从任何(非Spring)类获取Springbean:

@组件
公共类SpringContext{
私有静态应用上下文应用上下文;
@自动连线
私有void setApplicationContext(ApplicationContext ctx){
applicationContext=ctx;
}
公共静态T getBean(类组件类){
返回applicationContext.getBean(componentClass);
}
}

+1顺便问一句,您是否知道通过直接@Autowired注入applicationContext或实现ApplicationContextAware接口的利弊?谢谢。@Bariscan:我认为没有正反两方面的因素。但我更喜欢这个,因为@Autowired是我用来注入所有属性的,所以有什么不同的方法,因为它是一个应用程序上下文?你能提供一个更详细的@Autowired(我非常喜欢)应用程序上下文的例子吗?对我来说总是空的。需要额外的接口吗?@Autowired是否在同一个bean中注入其他属性?同样的代码在Spring 3.1.1中也适用于我,而我同意
@Autowired
很好;当涉及循环引用时,使用
@Autowired
和ApplicationContextAware之间的差异可能会令人惊讶。事实证明,仅仅删除
@Autowired
就解决了我在中描述的问题
@Component
public class SpringContext {
    private static ApplicationContext applicationContext;

    @Autowired
    private void setApplicationContext(ApplicationContext ctx) {
        applicationContext = ctx;
    }

    public static <T> T getBean(Class<T> componentClass) {
        return applicationContext.getBean(componentClass);
    }
}