Spring boot app in another project from controller class - spring

I want to know if I can keep my Spring boot class in Project A and my controller and services in Project B and invoke the end-points from Project A? The reason to do this is because I want to give Project A (which has dependency of Project B) an option to extend my controller and services to override any method.

Yes, it is a common practice to have your starter in a project and the libraries in another (spring projects are a good example for it).
it is better to have them separated in 2 repositories :faster build, version management, separation of responsibility between team members etc.
Build the project with the libraries (A)
Project with the starter (B) will have a dependency to project A
In general, it is recommended to have a configuration in project A with #ComponentScan and project B should import the configuration.
If the beans are scanned from project A, you will have access to all endpoints and services.

Related

How to share spring boot multi modules project (A) as dependency to another multimodule spring boot project (B) to access all rest api from it?

How to share spring boot multi-modules project as dependency to another multi-module spring boot project to access rest api from it?
The use case is :
I want to share the rest APIs from the multi-module Spring boot project let's say "A" to another multi-module spring boot project "B" as maven dependencies.
In this case, each module has different API's that needs to be accessed from Project "B"
Each module can be shared with its dependent module to run all the implemented rest api to another project. In simple words, all the project "A" RestControllers class should be accessible from consumer project "B".
When I tried to share it as jar of project "A" to Project "B" it does not throw any error but it should recognize the API endpoints from "A" with the message "This application has no explicit mapping for /error, so you are seeing this as a fallback. There was an unexpected error (type=Not Found, status=404)."
I also tried to add #ComponentScan(basePackages = "com.demo") in configuration but still it did not work.
I found Related question but in my case its multi-module project
Please feel free to ask for any additional information if my question is not clear.
Thank you in advance.

Is bad practice to have spring boot starter project for starter project?

Spring boot starter project provides extensive set of functionalities auto configured. But for our application we want to have only a subset of functionality. May be only one feature out of the spring boot starter project. So is it advised to have custom starter project on top of spring boot provided starter project to mask some of the features or write new starter project directly from lower level libraries?
Spring boot starter project provides extensive set of functionalities
auto configured
There are two separate concerns you are talking about.
I think the auto configured part is something which is Spring boot's opinionated way of configuring. As an example if in classpath it finds a in-memory database library ( like H2) it automatically creates a datasource (pointing to embedded in-memory database) which is available for autowiring without you writing the configuration in a Java config class. Of course you can create you own datasource of choice by including appropriate driver jar for that database. Similarly lots of other configurations are done by default by classpath scanning and availability of certain jars and classes.
The second part - is more of a dependency issue in general. Say you want to use web mvc part in Spring boot. This will require a consistent set of dependencies and transitive dependencies as well. Rather than finding and declaring the dependency in your build tool of choice ( maven, gradle etc) it has created the concept of starter projects where you simply mention the parent and all the right dependencies would be pulled on. This gives a great way to correctly lock your dependencies. However if you want to include any different version than what is provided by boot starter ( provided there is no compatibility issues with those different versions) you can still add explicitly in your build tool. For e.g., maven will by default include the highest version of a particular dependency among all available via transitive dependencies when it resolves dependency for an artifact.

Spring MVC + Web services + Hibernate project structure

in .net it is possible to create webservices, asp.net MVC, ORM, DTO, BAL as different projects so that we can reuse them nicely. For this college project I have been force to use j2ee and I am wondering how to do this layering. Is it possible to create hibernate as a different project? or how should I handle this?
I don't know .net at all, but you can start by creating multimodule maven project in eclipse.
1 module will contain you Entities and Dao and procude a jar,
1 module will contain #Controller and object related to your web service and
produce a war or ear.

Spring: Injecting services from a different project

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.

How to setup a Spring multimodule project?

I'm new to Spring and try to setup a project which is split into 3 submodules. As build tool I'm using maven. My problem is, that I don't know where to add Springs "magic".
My 3 submodules are "ORM" (holds all the hibernate staff to access the database) "BusinessLogic" (which should hold the complete logic) and "WebApp" (adds as the only "client" to the app / logic).
I want to use SpringMVC for the WebApp which seems to be no problem. As "BusinessLogic" should hold the complete logic I thought of adding the Spring related stuff (Bean definition / DI) in that module. But then I don't know how to setup Spring when accessing the module form the webapp.
The hole project is being ported from a JavaEE / JBoss app where "ORM" and "BusinessLogic" (implemented as EJBs) where put into one .ear archive and the webapp into a seperate one (.war). JNDI was used to access the beans from the webapp, but I completely want to decouple the application from JBoss and deploy it on a Tomcat webserver.
At the moment I've created all three modules as separate Maven projects ("ORM" and "BusinessLogic" as .jar, "WebApp" as .war packaging), linked by a "parent" project.
Thanks for any hints on project setup :).
Greetings
Ben
you could configure spring context in your web.xml and you can perform import of Spring sub-modules context. You can add import's configuration of sub-modules in your webApp application context.

Resources