繁体   English   中英

使用Java容器配置的Spring依赖注入

[英]Spring Dependency Injection using Java Container Configuration

我是Java和Spring的新手,来自C#和.NET世界,所以请耐心等待 - 我试图做的事情可能不合时宜......

我试图使用Java配置和注释配置Spring DI,而不是XML配置,但是我遇到了一些问题。 这适用于独立应用程序,而不是Web应用程序。 我已经完成了springource文档 ,据我所知,我的基本配置应该是正确的......但事实并非如此。 请看下面的代码:

Java配置注释类:

package birdalerter.common;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import birdalerter.process.ISightingsProcessor;
import birdalerter.process.SightingsProcessor;

@Configuration
@ComponentScan({"birdalerter.process", "birdalerter.common"})
public class AppConfig {
    @Bean
    @Scope("prototype")
    public ISightingsProcessor sightingsProcessor(){
        return new SightingsProcessor();
    }
}

配置实现ISightingsProcessor接口的组件:

package birdalerter.process;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

import org.springframework.stereotype.Component;

import birdalerter.domainobjects.IBirdSighting;
@Component
public class SightingsProcessor implements ISightingsProcessor{

    private LinkedBlockingQueue<IBirdSighting> queue;
    private List<ISightingVisitor> sightingVisitors = new ArrayList<ISightingVisitor>();

    public SightingsProcessor(){
    }

    ...
}

配置工厂组件:

package birdalerter.process;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
public class ProcessorFactory {
    private ISightingsProcessor sightingsProcessor;

    @Autowired
    @Required
    private void setSightingsProcessor(ISightingsProcessor sightingsProcessor){
        this.sightingsProcessor = sightingsProcessor;
    }

    public ISightingsProcessor getSightingsProcessor(){
        return this.sightingsProcessor;
    }
}

连接AnnotationConfigApplicationContext并测试:

@Test
public void testProcessingDI(){
    @SuppressWarnings("resource")
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(AppConfig.class);
    context.refresh();


    ISightingsProcessor processor = new ProcessorFactory().getSightingsProcessor();
    System.out.println(processor);
    Assert.assertTrue(processor != null);
}

SightingsProcessor未被设置者注入,并且断言失败,因为返回的对象为null。 希望我错过了一些非常明显的东西。

提前致谢。

编辑回应Meriton:

谢谢Meriton的回答。

为什么Spring不知道新创建的对象? Spring是否在整个应用程序生命周期中不保持依赖关系,并在创建配置为bean的新对象时适当地注入?

我不想直接使用context.getBean(ISightingsProcessor.class)如果我可以帮助它说实话,我希望在没有人工干预的情况下在setter方法中注入依赖 - 它看起来更干净。

我使用ProcessorFactory作为ISightingsProcessor接口扩展Runnable - 实现对象将作为线程启动。 应用程序可配置为具有n *个线程,每个线程在循环迭代中启动。 我认为在方法声明中使用@Autowired注释是不可能的(我可能是错的,请提供建议),因此我使用工厂提供注入的ISightingsProcessor具体类的新实例。

是的,我刚看了一下@Scope注释 - 你是对的,需要转到AppConfig @Bean声明(我在这个编辑中完成),谢谢你。

ISightingsProcessor processor = new ProcessorFactory().getSightingsProcessor();

这将调用ProcessorFactory的构造函数,然后调用构造函数创建的实例的getter。 Spring无法知道新创建的对象,因此不会注入其依赖项。 你应该向Spring询问ProcessorFactory,例如

ProcessorFactory pf = context.getBean(ProcessorFactory.class);
ISightingsProcessor processor = pf.getSightingsProcessor();

也就是说,我不知道为什么你需要类ProcessorFactory。 您也可以直接获取ISightingsProcessor:

ISightingsProcessor processor = context.getBean(ISightingsProcessor.class);

此外,“基于Java的配置”和组件扫描是声明bean的独立方式。 目前,您正在声明ISightingsProcessor两次:一次使用@ Bean-annotated工厂方法,一次使用组件扫描和类上的@Component注释。 做其中任何一件都行。 实际上,两者都可能导致一个bean定义覆盖另一个。

哦, @Scope注释用于bean定义(使用@Bean@Component注释的那些)。 它可能会被注射点忽略( @Autowired )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM