Spring AOP in separate project - spring-boot

I've been playing around with aspects in my spring boot project using spring-boot-starter-aop. It's all working nicely and I have good test coverage with some of them leveraging whats offered in spring boot e.g. #SpringBootTest.
Since the aspects and related code would be useful across multiple different projects I wanted to extract this code into a separate "utility" project. Then import this utility project as a dependency.
As I understand stand it (from here), a spring boot project should generally not be used as a dependency. Since I'm using spring-boot-starter-aop and also leveraging spring beans etc in my tests, this is a problem...
I'm just wondering on the best way to proceed. Any advice appreciated.
Thanks

Related

Is it necessary to use a spring initializr for creating spring applications?

I have started learning about spring and in every tutorial they start from spring initializr, i was wondering is it necessary to use it or we can create a project without using spring initializr ?
No, it is no necessary. You can do everything by your own hands. It just helps you to start quicker so that you focus more on the Spring concepts instead of spending much time on "infrastructure" like configuring dependencies. You run it and it just works. Then you can extend it step by step. This can be especially helpful if you just start learning Spring. Later on you should of course spend some time on other aspects that initializr provides you.
Adding to whatever is already mentioned.
No , it is not necessary to create a project with Spring Initializr
The site provides a curated list of dependencies that you can add to
your application based on the selected Spring Boot version. You can
also choose the language, build system and JVM version for the
project.
https://spring.io/blog/2019/02/20/what-s-new-with-spring-initializr.
https://github.com/spring-io/initializr/
Using Spring Initializr the right dependencies are preconfigured for the Spring Boot version used. These preconfigured projects reduces the setup time and one can start implementing the code rather than investing time on which dependencies to go with.

How to integrate the spring boot project generated by swagger with OSGi and deploy it in Apache karaf?

How to integrate the spring boot project generated by swagger with OSGi and deploy it in Apache karaf?
How should I write my pom.xml and how to modify the startup class. If there is something not detailed, I will add it. Thank you!
You don't. It's not strictly impossible but it would be a lot of work and struggle and you will not get any benefits out of it.
If you want SpringBoot - stick to that. You will sacrifice modularity, strong encapsulation, enforced clean architecture and bunch of other architectural thing that will matter a lot in the long run. But you will gain something that is easy to work with in the beginning and tons of code to copy/paste.
If you are playing the long game - I'd recommend to forget about SpringBoot and learn how to build modular OSGi applications. Recent version allow you to use popular technologies like JAX-RS and CDI. You can probably use one of the Swagger's JAX-RS generators and then convert the outcome to proper modular code. It may even be that there is a generator that generates OSGi JAX-RS code already.

Does spring boot come in to picture only while creating the project?

I've been learning spring boot for a while and as per my understanding, we use spring boot only to create the project along with dependencies and embedded servers. It doesn't have much of a role while working on the project or after the project is created.
I mean, if i were to work on an enterprise application for a company, chances are that, there is already a project created using springboot and I don't have to really use spring boot anywhere while working on that project.
Is my understanding correct?
I've tired googling, but did not find a clear answer.
No. You may still need to use the following spring-boot features after creating a project :
Externalise the application properties
Test the application using testing utilities and annotations provided by spring-boot
Use its auto-configuration feature to quickly configure if you need to enhance your application with other library/framework afterwards.
Use its developer tool to make you have a better Dev experience (e.g automatic restart the app when code changes , hot-reload static content etc.)
Use Actuator to monitor your application health and gather metrics.
I mean, if i were to work on an enterprise application for a company, chances are that, there is already a project created using springboot and I don't have to really use spring boot anywhere while working on that project.
I think you can say pretty much the same about any framework out there. I can't agree with this statement in general.
Every framework (including spring boot) provides a set of features that you may opt to use or not to use in your project.
So yes, for each existing microservice you'll have a #SpringBootApplication class.
Also probably set of configurations / practices how to work with configurations.
You'll already have also spring beans on existing project.
However software constantly evolves (otherwise it doesn't make sense to hire programmers, you know).
So when you (as a new employee) need to create a new micro service then, congratulations, you're using Spring boot features.
Other day-to-day tasks include (just a couple of example out of the head):
Create Integration Tests (#SpringBootTest)
Define new configuration Properties
Use Actuator
Use Metrics + define new metrics
Reconfigure Logging
Writing Liquidbase/Flyway migrations
Integrate new set of beans (Configurations)
Use Autoconfigurations
and so on and so forth.
If you want to really understand what you're doing while implementing these tasks you should know how do relevant features provided by spring boot work.

Spring Boot: starter modules - do i need to keep them?

I am starting yet another project based on spring boot.
For start it's pretty obvious and easy to go with spring-boot-starter dependencies. But for example I have projects which already live for 2 years and it becomes more challenging and time consuming to upgrade versions of those modules. Mostly because lot's of things auto-confugured under the hood, and there is only hidden knowledge to control them.
What would be the best practice to plan the new project for long term support and be able without any problems upgrade spring boot versions?
Are starter modules really just for start? After some time they should be fully replaced with pure modules from Spring framework?
The intention of Spring Starter modules is not necessarily intended to be aimed at being replaced by manually defining your dependencies. In fact, it's more the opposite, as the set of dependencies defined in starters are tested to work as expected as it provides an opinionated set.
The pattern laid out by Spring Boot is two fold, autoconfigure modules and starter modules:
Autoconfigure modules that generally do not, unless necessary, require dependencies downstream. Autoconfigure modules will compile against one or many modules, and then enable configurations within your application context based on rules generally governed by what libraries and components are available at context initialization. This is useful as it provides reasonable baseline configurations that can be used in a variety of scenarios, without enforcing what particular libraries/vendors downstream consumers must use. However, because they are intended to be used in a variety of scenarios, it is incumbent upon you as the consumer to ensure you have a valid context.
Starter modules are complimentary to autoconfigure modules. They provide an opinionated set of dependencies downstream, along with autoconfigure dependencies, to allow consumers to get started. Generally, but not necessarily, starter modules themselves are fairly empty and often do not contain any classes, as their main purpose is to just provide a POM that can be used for transitive dependencies.
Although your query is not about developing custom auto-configuration, the Spring Boot documentation regarding developing your own auto-configuration does a good job explaining the pattern.
Traditionally when we develop a Spring application, we need to include all the required dependencies (spring or other framework jar) by ourself. Starter is intended to minimize such configuration . It does not have any codes or logic inside . You can think that it is just a configuration setting which pre-defines some dependencies. Including a starter will automatically include all of the dependencies defined by this starter to your project thanks to the build system (e.g Maven) 's "transitive dependencies" features.
So you can see that starter provides one of the SpringBoot philosophy, which enable developer to create a Spring app that can "just run" quickly without much configuration , it definitely will not be replaced by the Spring.

adding spring-data-rest ontop of spring-data-jpa

i created a maven project, and added all dependencies i need.
i have some repositories using the spring-data-jpa, and i added some integration tests.
now i need to add ontop of it spring-data-rest, if i understand it is based on springmvc.
but all examples i found, i need to add spring boot to start the app.
i noticed also all new spring projects use spring boot.
this means that i have to learn and use it for my projects?
how can i use spring-data-jpa+spring-data-jpa with an existing servlet3 project
The reason all examples are written using Boot is that Boot is indeed the way you should start a new Spring project these days. It free's from a lot of the tedious work of setting up the infrastructure, finding dependencies in the right version etc.
To use Spring Data REST without Boot, simply add the necessary dependencies to your project. The easiest way to do this is to use the Spring Data Release Train BOM (which will help you pulling in the correct matching versions) along side the version-less dependency declarations for Spring Data REST WebMVC and - in your case - Spring Data JPA.
Then go ahead and either register RepositoryRestMvcConvfiguration as Spring bean (either through XML configuration or JavaConfig).
All of this is also documented in the reference documentation.

Resources