在没有弹簧实例化的类中支持自动装配(3) [英] Support for autowiring in a class not instantiated by spring (3)

查看:158
本文介绍了在没有弹簧实例化的类中支持自动装配(3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I realize this should be really basic but I haven't found a second step example after Helloworld

所以我拥有的是:

spring config xml名为spring-beans.xml:

spring config xml called 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类的相关详情:

Relevant details of AppImpl class:

@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的相关详细信息:

Relevant details of 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的相关细节:

Relevant details of DAO1:

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

    myfind() { ...}
}

相关详细信息DAO2:

Relevant details of DAO2:

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

    myfind() { ...}
}

是的,所以我通过springcontext调用初始化App,然后通过使用@Autowired成功实例化DAO1实例。

Right, so I initialize the App through a springcontext call and then succesfully instantiate a DAO1 instance through the use of @Autowired.

然后我创建一个非托管实例工作,并希望通过使用@Autowired

Then I create an unmanaged instance of Job and want to inject "singeltonish" dependencies in that class too by using @Autowired

在该类中注入singeltonish依赖关系.Dao类都是春天的刻板印象,扫描仪发现它们很好。

Both Dao classes are spring stereotypes and scanner finds them fine.

所以我的问题基本上是,如何实例化作业实例以便我可以使用@Autowir里面的ed概念?

So my question is basically, how should I instantiate the job instance so that I can use @Autowired concept inside it?

如果我需要一个全局可访问的applicationcontext,我该如何最好地介绍它?

If I need a globally accessible applicationcontext, how do I best introduce that?

推荐答案

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

Spring beans are singletons by default. However, what you need there are multiple instances, and on top of that, multiple instances created runtime.

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

One possibility is to use method injection for this. You'd create a container aware job factory that would request new instances from the container.

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

(I think it is a bit fishy that you'd need a DAO reference injected in those runtime instances... I would maybe try rethinking the logic. Why couldn't you just provide the DAO reference in a constructor argument, for example, or use it from somewhere else altogether. You could have a method in the dao that would accept a Jobs instance, or a runWith(DAO2 dao) stuff in the JobImpl that would be satisfied with a class injected elsewhere, or a JobProcessor service that would have the daos injected and would ask the relevant info from Jobs instance...)

这篇关于在没有弹簧实例化的类中支持自动装配(3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆