0

I use Play Framework + Spring Data JPA from this example:

https://github.com/typesafehub/play-spring-data-jpa

I have models, repositories and now I want to initialize my DB on start up. I inject repositories to my Global.java file using @Autowired, but they equals to null on run time. It's because Spring don't try to inject dependencies to Global.java, because Global.java don't have even package name, so I can't tell Spring to scan his package!

With controllers Spring DI works well, but how to use it in Global.java? How to initialize database using repositories?

Dark Hydra
  • 265
  • 6
  • 21

2 Answers2

2

You can change package of Global.java in application.conf by setting property application.global.

Other way can be to fetch dependency explicitly calling

context.getBean(YourBeanClass)

in Global.java instead of autowiring it, as you have context available in Global.

Govind Singh
  • 15,282
  • 14
  • 72
  • 106
Shikha Gupta
  • 103
  • 1
  • 7
  • Explicit calling ```context.getBean()``` is working for me. Autowiring still is not working, but probably it's because Global object is not managed by Spring and Play Framework creates it. Not sure, if possible to use ```@Autowired``` in Global object and maybe it's bad idea. – Dark Hydra May 06 '15 at 11:51
  • Yeah, I gave a try as well. @Autowired does't work in global. Probably because Spring doesn't recognize it as a component. – Shikha Gupta May 07 '15 at 07:26
0

You may explicitly create a bean by defining it in your context xml <bean id="global" class="Global"></bean>

Also, having classes in default packages is not a good idea.

TJ-
  • 14,085
  • 12
  • 59
  • 90
  • I don't have any classes in default package. There is only Global class. Also I tried combine xml configurations from here https://github.com/guillaumebort/play20-spring-demo and annotation based configuration from here https://github.com/typesafehub/play-spring-data-jpa but failed. I can choose only one. So I don't have context.xml file. Is there any examples, how to combine them? – Dark Hydra May 05 '15 at 18:22
  • Okay, I combined that configurations using answer from this question http://stackoverflow.com/questions/25049778/how-to-configure-spring-data-jpa-using-xml But when I define bean, nothing changes, fields still equals to ```null```. I also tried to add setter for ```@Autowired``` field and add parameter to bean, but it didn't work. – Dark Hydra May 05 '15 at 19:42