3

I have the following problem in a Java application that use Spring framework.

So I have the following situation, into the root-context.xml configuration file I have this bean configuration:

<!-- Definition for datiPianiInterventiDaoImpl bean -->
   <bean id="datiPianiInterventiDaoImpl" class="it.myCompany.myclient.batch.dao.DatiPianiInterventiDaoImpl">
      <property name="dataSource"  ref="dataSource" />    
   </bean>  

Ok so it works fine and this bean is correctly created and works fine.

The problem is that now in this bean I have to inject also an intance of the org.springframework.core.env.Environment Spring class.

So I try to do in this way:

public class DatiPianiInterventiDaoImpl implements DatiPianiInterventiDao {

    @Autowired
    private Environment env;

    ...................................................
    ...................................................
    ...................................................
}

But it seems can't work because, when I perform my application the value of the Environment env is null.

The @Autowired is activated because I use this annotation in other classes o my project.

So what could be the problem? I am thinking that maybe it could depend by the fact that I define my bean having id="datiPianiInteventiDaoImpl" into my root-context.xml (and here I am defining also the dependency to inject into this bean).

So maybe I can't mix the XML dependency injection with the use of @Autowired?

What is wrong? What am I missing? How can I correcctly inject the Environment instance into this class?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

2 Answers2

6

Possible causes of Environment being null:

  • You are missing @Component / @Service annotation on top of Environemnet class.
  • You created somewhere instance of class DatiPianiInterventiDaoImpl using new operator.
  • Does your entry: corresponds to the proper package base?
  • I assume annotation-config is present since @Autowired works elsewhere.
  • Try to annotate your DatiPianiInterventiDaoImpl with @Service
mlewandowski
  • 802
  • 7
  • 14
  • It is straange, annotting the DatiPianiInterventiDaoImpl with Service (instead the XML configuration) the autowiring of the Environment env variable seems works fine but using the XML configuration and mixing with the Autowire annotation not – AndreaNobili Feb 23 '16 at 13:31
  • Glad I could help. Vote up :) – mlewandowski Feb 23 '16 at 13:37
  • No it isn't. Because is you have a component-scan in another context that will now pickup the bean and do the injection. The `` or `` has to be in the same context. If you define this dao in the root (`ContextLoaderListener`) and have scanning in your `DispatcherServlet` (or vice versa) annotations won't work. – M. Deinum Feb 23 '16 at 19:48
  • Simple and comprehensive at the same time. My issue was, I had used "new" operator somewhere to instantiate my class. – Zwakele Mgabhi May 22 '19 at 08:19
3

There is no problem to mix the XML dependency injection with the use of @Autowired. As long as your bean is scanned by spring bean factory this is a valid syntax. There was a problem with autowiring Enviroment to Dao classe, see what dave wrote here, you can find a solution in this link ( the other answer)

Community
  • 1
  • 1
Hezi Schrager
  • 391
  • 6
  • 18