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?
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.
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.
Related
I have a bean post processor (RepoRegistry) that picks up spring-data repositories and puts them i a map to make them available by type (repoRegistry.getRepositoryFor(MyEntity.class)). It seems as if the repo beans are created lazily. As a workaorud I now have to autowire all repositories manually somwhere so that they get created and processed by the post processor.
Is there another way to declare that some (or at least all) spring data repositories are declared non-lazy? I tried to add #Lazy to the repository interfaces without success.
You're probably seeing this on a Spring Data version that is older than the Codd release train (e.g. Spring Data JPA 1.4.x). The Codd release train (and thus Spring Data JPA 1.5) switched this model to eager init by default. Prior to that, repository beans were only create if there had been an injection target.
We generally recommend to use the Repositories type we already provide with Spring Data Commons to obtain all repositories contained in a BeanFactory:
Repositories repositories = new Repositories(applicationContext);
repositories.getRepositoryFor(Person.class);
Add this method to RepoRegistry:
#Autowired
public void setReps( List<Repository> repos ) {...}
Note: I haven't used Spring Data, yet. You need to use the common interface type of all repos as type argument of the List.
Spring will then collect all beans which implement Repository and pass them into the setter as a list.
Our application uses #Bean to define create beans and load them into the Spring context.
We now need to externalize these, so as to enable the application to be configured without touching the java source code.
We wish to replace the #Bean's with Groovy classes.
Is there a way to annotate a Groovy bean so that it will be picked up by Spring?
Note that we cannot simply reference each Groovy bean in the Spring XML, as we need to add and modify beans without touching the Spring code.
Thanks very much.
Use Spring config inheritance.
Move all shared code in a common "base" project that each individual / specific project depends on. Use Maven for this.
Create a common / base Spring config and put that into the "base" project. This config doesn't contain a definition for ProcessDefinition
In the specific project, create one bean which inherits from ProcessDefinition. Create a Spring config which imports the base config and define the single specific bean in it.
"BuildUp" means not to create an object, but inject dependencies to an existing object according to annotations such as "Required". Expecting it will find proper beans for those dependencies automatically.
I'm using JBoss 7.1.1 with CDI.
I've got a Stateless bean named ServiceAccount in JNDI. This is the real service implementation.
I've got another Statelss bean named ServiceAccountMock which is a Mock service.
The both herited from the same interface and are packaged in a service.ear.
What I want to do is to declare the mock service as alternative in bean.xml, redeploy my services ear, and then all the client see the mock version (without changing anything on client side).
When I deploy my service.ear, JBoss says :
java.lang.IllegalArgumentException: JBAS011046: A component named 'ServiceAccount' is already defined in this module
This is true, both services are declared the same way (#Stateless(name="ServiceAccount")).
If I change the name of the mock version, I have to change on client side which EJB is used (and I don't want to do that).
Does anyone know how to do that ?
I don't think you will be able to deploy 2 beans with the same name in the same application.
If the clients of the bean are only local, you should use CDI type injection selection.
Remove the name of the beans or put different name if you realy need a name (The mock will have a different name that the real implementation).
Keep the #Alternative annotation on the mock.
At the injection point, use the interface as the type of variable (and probably using the #Inject annotation instead of the #EJB one may help).
The EJB specification and CDI aren't yet completly aligned. EJB has some element like the name that need to be unique over the application and is not taken into account in the CDI alternative functionaltity.
So I don't think you will be able to mix EJB name injection selection and CDI alternative injection selection.
First you need to annotate ServiceAccountMock with #Alternative, to tell the container not to use it if not instructed to.
#Stateless(name="ServiceAccount")
#Alternative
public class ServiceAccountMock{
....
}
Then in beans.xml you need to tell the A/S to pick the mock implementation:
...
<alternatives>
<class>xx.yy.ServiceAccountMock</class>
</alternatives>
...
I want to clear some moments about integrating spring and struts. I have only one action class per application extended from MappingDispatchAction. So, actually my app when doing something uses not Action objects, but methods from my action. All I want from spring is to initialize this action and all for now. Just simply set DAO object. I looked through documentation, but I don't understand following:
We use action path from struts-config.xml as a name of bean in action-servlet.xml. Okay, but am I supposed to write beans in action-servlet.xml for every path name and set this poor DAO ref or what ?
The Struts 1 config file will use the DelegatingActionProxy class as the type attribute for all action configurations.
The Spring config file will contain the bean definitions of each action implementation. I don't know what DAO you're talking about, but actions that require DAO or service injection need to have them listed, yes--that's what Spring configuration is.
You may also be able to use annotations if you're not interested in using XML configuration, or use bean inheritance if many beans share the same DAO/service/etc. property values.