3

我意识到这应该是非常基本的,但是在 Helloworld 之后我还没有找到第二步示例

所以我所拥有的是:

spring 配置 xml 称为 spring-beans.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config />
    <context:component-scan base-package="org" />

</beans>

一个弹簧上下文初始化的类:

public static void main(String[] args) {
    // initialize Spring
    ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring-beans.xml");

    App app = (App) context.getBean("app");
    app.run();
}

AppImpl 类的相关细节:

@Component("app")
public final class AppImpl implements App{    

    // focus of question: This autowiring works
    @Autowired
    private DAO1 dao1;


    public void run() {
        //focus of question: This works as daoclass is instantiated properly                   
        obj1s = dao1.myFind();            

        badJobs = runJobs(obj1s);
    }

    private List<Obj1> runJobs(final List<Obj1> obj1s) {        
        List<Obj1> jobsGoneBad = new ArrayList<Obj1>();
        for (Obj1 next : obj1s) {

            // focus of question: usage of new keyword, thus not spring container managed?
            Job job = new JobImpl(next);
            job.run();

        }
        return jobsGoneBad;
    }    
}

JobImpl的相关细节:

public class JobImpl implements Job {

    private Obj1 obj1;

    // focus of question: can't autowire
    @Autowired
    private DAO2 dao2;

    @Override
    public void run() {
        //focus of question: opDAO == null - not initialized by @Autowired
        Obj2 obj2 = dao2.myFind();        
    }

}

DAO1的相关细节:

@Repository("DAO1") //Focus of question: DAO1 is a repository stereotype
public class DAO1 {

    myfind() { ...}
}

DAO2的相关细节:

@Repository("DAO2") //Focus of question: DAO2 is a repository stereotype
public class DAO2 {        

    myfind() { ...}
}

对,所以我通过 springcontext 调用初始化应用程序,然后通过使用 @Autowired 成功实例化 DAO1 实例。

然后我创建了一个 Job 的非托管实例,并希望通过使用 @Autowired 在该类中注入“singeltonish”依赖项

两个 Dao 类都是 spring 刻板印象,scanner 发现它们很好。

所以我的问题基本上是,我应该如何实例化作业实例以便我可以在其中使用 @Autowired 概念?

如果我需要一个全局可访问的应用程序上下文,我该如何最好地引入它?

4

4 回答 4

5

您只能在 Spring 托管 bean 中使用 Spring Bean 功能,例如注入!

但是你可以使用@ConfigurableAnnotation,但这需要你使用REAL AspectJ。如果一个类由@Configurable(并且您使用 AspectJ)注释,那么您可以使用 Springs Injection Annotations,即使这个类是由一个普通的new.

@看

于 2012-10-26T10:42:06.037 回答
2

@Autowired 不起作用,因为您的 JobImpl 对象不是由 spring 管理的,因此它永远没有机会注入它。Spring 托管 bean 是在组件扫描期间或在 XML 定义中创建实例的那些。在您的情况下, JobImpl 被实例化为普通的 Java 对象。

一种解决方案是在 spring 上下文中用手动查找替换自动连接。

private DAO2 dao2 = SpringApplicationContext.getApplicationContext ().getBean (DAO2.class);

@Autowired 在@AppImpl 中工作,因为它被注释为@Component。这会在其类路径扫描期间将其标记为 spring,它将创建此类的实例并执行任何自动装配/注入。

于 2012-10-26T10:41:51.210 回答
2

Spring bean 默认是单例的。但是,您需要的是多个实例,最重要的是,多个实例创建了运行时。

一种可能性是为此使用方法注入。您将创建一个容器感知作业工厂,该工厂将从容器中请求新实例。

(我认为你需要在那些运行时实例中注入 DAO 引用有点可疑......我可能会尝试重新考虑逻辑。为什么你不能只在构造函数参数中提供 DAO 引用,例如,或者完全从其他地方使用它。你可以在 dao 中有一个方法来接受 Jobs 实例,或者在 JobImpl 中有一个 runWith(DAO2 dao) 东西,它可以满足在其他地方注入的类,或者一个 JobProcessor 服务,它将注入 daos 并从 Jobs 实例中询问相关信息......)

于 2012-10-26T10:40:46.017 回答
0

为您的课程添加@Component注释JobImpl。在 xml 中为此类添加组件扫描,您的自动装配将适用dao2(提供 getter-setter 方法)。

于 2012-10-26T10:25:52.967 回答