I am new to Spring and I have below query.
I have a Spring Project which just perform the Database Operations. It has Entity Class and Repository say Employee and EmployeeRepository.
I have another project which act as RestWebService (in Spring). I want to inject EmployeeRepository in the controller. Both the project are Web project and deployed on same application server.
Please suggest how to do this.
If the projects are deployed as separate applications on an application server, they cannot access each other's beans. Each application is maintaining its own Application Context.
The better way would be having just one application deployed, which has the other project as dependency (using maven for example).
Of course easier would be having just one project.
Related
Just started exploring spring boot and trying to understand.....
In a normal project scenario, we create multiple projects / modules depending on functionality. Ultimately, package all different jars , wars in .ear and deploy it.
With spring boot, new project are deployed on a separate of instances of tomcat.
But if all these projects are related, it will need reconciliation of proprty files and any other resources differently utilized, before you create .ear.
While I understand the advantages of spring boot while development, is there any thought on how to make this process better when using spring boot?
Modern cloud architectures are moving away from using property files. Your Ops or DevOps teams should appreciate if your app would accept configurations via system properties. Spring Boot is perfectly suited for that.
Older fashioned orchestrators would replace placeholders in your properties files during deployment phase.
I have been working with J2E web applications for some years now and have started to use EJB over the last few weeks.
I created a project based on this example
Basically I create two maven projects, similar to what is described here https://blog.illucit.com/2015/04/ejb-remoting-with-wildfly/, one that builds a web application(war) and one that builds an EJB.
In web applications pom I add a dependency to my EJB and I can use a context look up to call the correct EJB and cast it to the correct class type. Even though my bean is annotated with remote I am guessing this is a local invocation.
On a second attempt I deployed an EJB module and separably deployed the web application. I was able to do a context look up to pull in the session bean. But I could only cast it to an Object class. I think because I had no reference to the EJB in my web application, as it was part of a separably deployed module, I couldn't do the cast.
I would imagine that since my web applications are seperate, this is a very simple example of a distributed system?
If so, How do I avoid this casting issue, if the class definition is in another application?
Edit-
Because I havent got the awnser I was looking for I have tried to narrow the scope of my question some more.
On this example
https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+server+instance
You can see the client is able to call the Greeter class, even though it is sitting on another server. The client has no import of the Greeter class.
So how is it able to cast the EJB lookup to a class it doesn't have any reference to?
If you want to divide the EJB and the WAR project in different servers you need a third project (normal Java project) with all the objects (POJO, VO, DTO or Entities) that you will use for communicate between WAR and EJB and also you need to put all EJB's Remote interfaces. In this way you can do the "cast". This new project must be a dependency of both projects EJB and WAR.
I want to be able to create multiple spring projects in eclipse that will work together that will be created as one EAR. For example, a Spring JMS project, a Spring-WS project, etc. Since these technically can be treated as a sepearte project, will this create multiple spring containers? I would like to have one spring container that will be shared, especially for common configurations such as the data source bean.
I'm hoping to get some advice on how to structure/setup a project.
Currently we have a webapp that uses spring, and JPA/hibernate. We want to create an admin webapp that's going to live separately then the "user" webapp for backend stuff we need to support. We want to keep it separate from the existing project so they can be deployed independently since they have different testing and reliability requirements, as well as we want to be able to iterate more quickly on the admin stuff. These are deployed via war files built by maven.
We do want them to share the domain model though, and we would prefer to keep the existing configuration from the webapp as well. So basically we want to be able to separate out the webservices from each other, while keeping everything else the same (app-context, web config, persistence, etc...).
Is there a nice way to separate out the webservices, or will we basically have to duplicate the entire project for each war file, or keep them lock step?
Package your domain model (entity and DAO classes) in a separate jar. Use maven for dependency management. Distribute this domain model jar in a company local repository (eg: nexus). Then on your user web-app & admin web-app just declare this as a dependency.
The only thing you need to be aware when using this mechanism is when you update the domain model and distribute new version into nexus, all downstream packages dependency version has to be updated and recompiled
I am currently learning Spring. So far I have created a basic application consisting of Hibernate/JPA entities, DAOs and classes that perform business logic. This I am calling the service layer.
If I now wish to use SpringMVC to add a web front end to this application, how should I separate the two?
i.e. do I need to create a separate 'Dynamic Web' project in Eclipse for the web layer? If so, how do I then integrate the two? I presume I could simply copy the service layer source into the web project, but this doesn't seem like the best approach.
You don't need a separate project, it really depends on whether you'll be reusing your services elsewhere.
If you won't be reusing your services, add your web layer to the same project, have your controllers call your service layer, and build a WAR from it.
If you will be reusing your services, create a new project for you web layer, build a JAR for your services, and import that JAR into your web layer. Something like Maven will help here.
Are you using Maven? If so, you should create a webapp project and add your "core project" as a dependency.