简体   繁体   中英

injecting spring beans into non-singleton classes

is it possible to inject spring beans into a polling filter class (FClass) controlled by a scheduler job? i don't quite understand how singleton applies here.

i understand spring beans are singleton so in order to inject the spring beans into class FClass. i need to define FClass as a bean and add the DI as property etc..

so how do i know if FClass should be a singleton? i assume only classes that are singletons can be created and beans and have DI done to them.

my problem is :

i need to be able to inject my facade bean xfacade into FClass. x_facacde handles the dao object. it has Y_dao and a Z_hibernate session beans injected as DI.

when i tried to create a spring bean of StatusPollingFilter (FClass) and injected the facade bean - i got a null and the setter is never called for the injection in debug mode.

the problem: i'm thought it might be something to do with the thread / scheduler nature of StatusPollingFilter, and since spring beans are singletons it might not work due to that.

i'm thinking of creating a factory for the StatusPollingFilter (FClass). but need to know if this is correct thing and i'm on right track before i do too much work and realize even that doesn't work as the problem might be somewhere else. ideally i just want to update a table in the easiest possible way. but i have to use hibernate as the DAO exists but hibernate is configured using

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 with /hibernate/TopoObject.hbm.xml

files.

so no matter how i try this i always get null pointer exception on session or injected facade bean. reading some of the QA's here seems like because StatusPollingFilter is instantiated using the scheduler and not spring it cant be aware of the DI beans. so would the above factory pattern help here.

I may have an additional problem but i'll cross that bridge when i come to it. but just to mention briefly, in case anyone is aware of issues that i might hit ... not sure what / how the scheduler would invoke the factory for an instance as its all controlled by 3rd party api - which invokes a StatusPollingFilter but i'm assuming if i pass in the factory as the class and parameter it would find its way through... but initial part is the main question. please ignore the latter waffle. thanks in advance.

Actually :

i assume only classes that are singletons can be created

is where you are wrong. A bean is just a class that you let spring instantiate. By default , they are created as singleton but you can specify the scope on your bean using the attribute scope (quite surprisingly). The value you can specify are those specified in the documentation here

So one thing you have to be careful with is the injection of beans scoped as prototype or request into singletons.

having read more - i have come across the ans. because the StatusPollingFilter object is under control of scheduler (i knew that scheduler had something to do with it) then it is unaware of the spring beans which is why i keep getting null when i try injecting the bean.

i created a class:

 ApplicationContextProvider implements ApplicationContextAware

added static access

private static ApplicationContext   appContext;

did a setter for it :

public void setApplicationContext(ApplicationContext context)
{
    appContext = context;
}

and added

public static Object getBean(String beanName) throws BeansException
{
    return appContext.getBean(beanName);
}

used in code as :

EvoTAMDAOFacade evoDao = (EvoTAMDAOFacade) ApplicationContextProvider.getBean("evoDaoFacade");

i now have access to the facade bean and all injected beans into facade. i still have an issue with hibernate session but thats prob due to some other issue.

pt here is i don't have access to the bean as its not in control of the spring container so i needed to somehow get it , probably could have done it via the factory method but why mess around when there a simpler way. thanks for help by anyone who may have posted or tried to understand my problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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