We are developing a web application which uses spring mvc, rest, jquery, ajax and json. Also we use mongodb as our db. We use maven as build tool.
I have a project structure like below:
eem (parent pom)
eem-db (sub module, and it is a Eclipse project for mongo db dao level and model resides here)
eem-net (sub module, and it is again a Eclipse project for some networking code)
eem-webapp (sub module, and it is a Eclipse web app project for web application, which has Spring MVC)
My doubt is about the design of our web application in which I'm not knowing how to use spring container on this web app.
Below are my requirements and need help:
Is it a correct way to have mongodb related dao in a separate project?
(we use dependency on eem-webapp to get db code (as .jar) on our web app).
How can i use #Autowired on the model in my controller to get mongo db model "emp" (say, com.eem.db.model.emp)? i.e What configuration do i need to provide on my eem-webapp to autowire emp (and all model classes) model to my controller?
I want to instantiate a class (say, com.eem.net.discovery.discover) from eem-net on my controller. How should i do this from spring config file. I know using new is not necessary when we are on spring container. What kinda DI i should use for this situation?
Any pointers for my learning?
The best practique is create one session factory bean (scope=singleton) that you can #Autowired in all your Controllers, you can also create one pool connections to managing your clients db requests.
For this you can create your own .jar, and use your own .jar classes to register your bean in your spring container.
Related
Let's say we have a Spring MVC project which is a banking web application. I am trying to identify the sub-systems ,modules and components of this project.
1.Subsystems - from what I have red the subsystems in MVC architecture are only 3- the Model,View and Controller is that correct?
2.Modules - are these a group of classes that do something particular as a group.For example LoginController.java,RegisterControler.java form a module let's call it Authentication
3.Components - for components I am not sure which they are in a Spring MVC project.
If someone can explain with examples in terms of a banking web application or other Spring MVC app it would be great!
Spring MVC follows the Model-View-Controller design pattern.
Model - A model contains the data of the application. A data can be a single object or a collection of objects.
Controller - A controller contains the business logic of an application. Here, the #Controller annotation is used to mark the class as the controller.
View - A view represents the provided information in a particular format. Generally, JSP+JSTL is used to create a view page. Although spring also supports other view technologies such as Apache Velocity, Thymeleaf and FreeMarker.
Spring Boot Framework has mainly four major Components.
Spring Boot Starters.
Spring Boot AutoConfigurator.
Spring Boot CLI.
Spring Boot Actuator.
Please check out : https://www.journaldev.com/7989/key-components-and-internals-of-spring-boot-framework
In project-specific terms,
Module: It can be something that can be built separately in an application. ex: Login Module, Sign-up module, Transactions module, etc., You can say a module is a group of components.
Sub-systems: As far as I know, subsystems are the service package related stuff.
Components: Annotating a class with #Component tells Spring that it is available for fulfilling injections.
Spring Component annotation is used to denote a class as a Component. It means that the Spring framework will autodetect these classes for dependency injection when annotation-based configuration and classpath scanning is used
I have a Spring Boot Jersey Application. I want to use a custom class loader with it to allow loading classes and resources from outside of standard web application classpath. (I need it to allow customers to add "plugins" to the application without disturbing original application file structure). Any suggestions how can I do that?
One of the Spring framework advantage is dependency injection. Many had used SpringBoot for providing REST Web Services.
Read up and notice there are Scheduler and CommandLineRunner for SpringBoot, could we using SpringBoot for backend type of application to replace the usual standalone java program while making use of SpringBoot advantage (Dependency Injection)
- Cron Job (Execute and stop running)
- Long Running Process
One of the main thing I am looking into is to use annotation such as Spring Configuration, Spring Data JPA and other technology in backend application.
Of course!
I used spring boot to back CLI projects, DB access projects and more.
Spring boot is very modular. It works by providing auto-configuration based on your maven/gradle imports. If you don't import starter-web/starter-jersey or any other starter that is for the web/rest api, the auto-configuration for this resources won't be triggered and you can basically enjoy all the power of spring boot to support your needs
Definitely,
Spring boot is not a separate framework.It reduces the configuration difficulties when you using spring framework. Spring boot provides a Rapid Application Development using without complex configuration including your dispatcher servlet, XML file for database connectivity and configuration files. You can use spring boot for back-end development. Simply says you can do everything what you does in spring MVC without any complex configuration. If you are using spring boot , You can configure your database details in application.properties file. I am adding one of two links for proper reading,
https://projects.spring.io/spring-boot/ ,
https://dzone.com/articles/why-springboot
In the project our team's working on, we currently have 3 separate Spring projects which utilizes the same services. To avoid redunancy and code copy-pasting, we're planning to create a "common" project wherein all the three projects would be dependent on the common project. In this instance, is it possible to inject these services (perhaps using the #Service annotation) to the Controllers of the Spring projects?
EDIT:
I tried implementing this on my own and what I basically did was I configured the pom.xml to get the Spring Context 3.1.1 dependency (which are also being used by my Spring projects) for my "common" project. With that, I was able to annotate my service with #Service. Afterwards, on my Spring project, I set the component-scan to a level wherein my two projects would meet. On my Spring controller, I #Autowired the service from the "common" project. I ran the Spring project and apparently it worked. Is this the best way to do this?
That's absolutely fine, and standard. Spring (unlike CDI) couldn't care less whether your beans come from the current project or from an imported jar.
I have been using Spring DI and IOC extensively in my earlier projects. As per a new project requirement, we have to setup our projects as follows :
Business project (Java project)
Common project ( Java project)
Presentation project (Web project)
Business and presentation communicates/uses the common layer. However, the business and presentation are to be deployed in different app servers, and the communication between business and presentation is not http based. Presentation invokes the business through iiop protocol. That means, the protocol switching happens, while calling business (Java project) from presentation (web project).
In order to setup different application layers, I did something as follows :
In Business project, I encapsulated all business logic methods, and DAO methods in a facade object, using SPRING dependency Injection feature and normal OOPS technique.
Now I want to expose facade object using EJB, and from the presentation layer, I want to invoke the ejb jndi and get the facade object by making an EJB call.
I need to know :
How can I create an EJB (a simple stateless session bean EJB 2.1) and expose it using Spring ?
How can I access an EJB using spring ? (does not matter if the same has been exposed using spring or not)
I did not find any proper/complete example (even in spring ref), for doing the same configuration in spring.
Any help in this topic is highly appreciated.
Thanks and Regards,
Jitendriya Dash
Java/J2EE developer, Mumbai, India.
You don't expose it using Spring. You expose it by deploying it in your app server as a remote EJB. That's not Spring's responsibility.
You access a remote EJB by following the instructions in the reference documentation : http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#ejb.