Duplicate config code problem for all spring microservices [closed] - spring

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Is there a way to create a spring configuration class and use it to all my microservices?
Right now I have to copy a duplicate config class to all microservices. And if there are changes , I have to make them in all modules.
I use the apache zookeeper for centralize my configurations in single place. For setting authentication details on connection to zookeeper server I have to create a new bean for CuratorFramework class in spring #Configuration in any microservices that keep their configs on the zookeeper.

Yes, you can:
Create a module with a configuration (maven, gradle managed, whatever). Add all common beans to this module for reuse. It will be a kind of "infra".
Optionally add a spring.factories file to automatically "load" the configuration
In Microservices (each of them should be in a different module) add a dependency on that common module.
If you've added spring.factories - the configuration will be loaded automatically, if you didn't you'll have to import this configuration explicitly in each module. But in any case you won't need to duplicate code.
There are many tutorials / threads in SO about spring factories, so in case you're not familiar with this feature - you can read here for example

If a system has lots of common stuff and stuff that changes often it's a monolith. Maybe your split to microservices is not the best approach.
Regarding shared common stuff you get 3 choices:
Copy/paste (like you do for now)
Break out library (each microservice has a dependency to a common module)
Isolate stuff in his own microservice
Remember that DRY must be strict inside one microservice but can be relaxed between microservices.

Related

Which is the best way to do end to end testing of Spring Boot Rest APIs? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have checked in google and stackoverflow for end to end testing of Spring Boot Rest APIs.
I would like to test controller -> Service -> Repository at once.
I found two approaches are efficient:
1. TestRestTemplate:
Single call to the controller method with in-memory database configuration for repository.
2. MockMvc:
Create 2 Test classes.
one for Controller->Service
and one for Service->Repository.
Is there any way to club both 2 classes into one class.
which is the best way to do "end to end testing" of Spring Boot Rest APIs from above 2 approaches ???
I would say the better way to do the END2END testing is to use TestRestTemplate. It would actually start-up the server at a random port (it you configured) and call the api itself for testing.In this way, it mimic the actual behavior of the running server and use the default configures and beans.
MockMvc, in my testing experience, is mainly for testing the web layer, aka controllers. I would use mocked Service beans to replace the original ones. So I can test the behaviors of the controller layer itself without having to worry about the bugs in the service layer. More importantly, I don't have to set up any Database before testing.
So I would say, for E2E test, you can go for the first approach.

How spring boots works ,How it loads all the configuration [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
So I am having my rest Api calls using spring boot.
I want to know how spring boot loads all the configuration step by step.
like what would be the process while loading configuration,what and how the order are getting followed. Spring security,db configuration and bean initialization in term of all How these are getting loaded.
And how the spring boot find the priority (order) ,which should be loaded first and which should be next.
Beans are loaded based on the autoconfiguration mechanism in Spring Boot, there is a spring.factories file in the META-INF folder of the jar file containing the the fully qualified name of the Configuration classes to load.
When Spring Boot finds a file like that, it will load the configuration as a bean, and the configuration usually loads other beans.
Other way configuration could be loaded, by just simply #Importing them, some work like that, example #EnableMetrics.
Jar files containing this autoconfiguration mechanism usually called starters.
There are some special beans as well that will be picked up by Spring, example FilterDefinitionBean that will be turned into a filter, or CommandLineRunner, that will be executed after startup.
Usually you do not need to worry too much about the order of the starters, since Spring Boot automatically detects the correct order to initialize these based on the dependencies, beans with #DependsOn annotations.
Spring Boot starter autoconfiguraions happen after your beans are already defined, so they can give you fallback beans, but only if you have not defined them.
But sometimes, especially when you have #ConditionalOnBean you have to explicitly define the order, this can be done by #AutoConfigureBefore and #AutoConfigureAfter annotations to define where this autoconfiguration has to be initialized.
Another annotation #AutoConfigureOrder is the Spring Boot equivalent of the #Order annotation could also be used to change the order (since #Order does not work for autoconfigurations).
If you check your favourite starter's source code, you will see these order changing annotations are often added to the class.

Spring boot annotations [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm a beginner with spring boot so i need to ask some questions:
1 - what is difference between #async and #service("async")
2 - what is #transactional do ?
3 - what is the difference between maven project and spring-starter-project ?
thanks for all ,,
Question 1
The annotations don't have any relation.
#Async marks a method as a candidate for asynchronous execution. Can also be used at the type level, in which case all of the type's methods are considered as asynchronous.
#Service annotation serves as a specialization of #Component, allowing for implementation classes to be autodetected through classpath scanning.
If you use #Service("async") you are creating a Bean with name "async", nothing to do you asynchronous processing.
Question 2
#Transactional is used on methods that should span operations (like DB operations) in a single transaction. Let's say for example that you have a method that is going to save 3 records to different tables on DB and you have it annotated with #Transactional. Then all the 3 operations are going to be commited only if the method completes successfully. If the last operation fails, then nothing is commited to DB.
Question 3
A maven project can be any type of Java project. A Spring project is a project that uses components from Spring framework.
If you want to create an automatic Maven project with Spring dependencies take a look at Spring Initializer. It will automatically generate the files for you.

Is Spring Container is just like a jvm? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Is Spring Container is just like a jvm? or it is different?
Why is Spring IOC mainly used? If it is for creating the objects without using new operator? what is wrong in using new operator?
If we are creating singleton objects and return the same object whenever application wants, we are loading all the objects on server start up? will that not make the application heavy?
If it is so, then why we need spring core?
How is filter,bean post processor, aop is different?
if aop is used for implementing cross cutting concern, why do we need beanProcessor interface?
Is Spring Container is just like a jvm?
No, Spring is a Java framework. It provides classes you can use to run a Java application on a JVM.
Why is Spring IOC mainly used?
Learn what Inversion of Control is and you will understand why it is used so heavily.
If it is for creating the objects without using new operator? what is
wrong in using new operator?
The new keyword forces compile time dependencies. Inversion of Control and Dependency Injection remove those dependencies, mostly through reflection.
If we are creating singleton objects and return the same object
whenever application wants, we are loading all the objects on server
start up? will that not make the application heavy?
You will usually want all those objects at startup so it's a non-issue. You can delay the initialization of those objects (beans) with lazy loading.
If it is so, then why we need spring core?
If what is so?
How is filter,bean post processor, aop is different?
The BeanFactory creates and initializes beans. A BeanPostProcessor is meant to wrap a bean in a proxy or modify that bean's properties. The javadoc has more details.
Aspect oriented programming is a style of programming. In order to implement it with plain old Java, you need to use JDK or CGLIB proxies. Those are applied using BeanPostProcessor instances by wrapping the processed bean. Calls going to the target bean will be intercepted by the proxy which will (possibly) perform logic before delegating to the target bean. Java's AOP capabilities are almost completely limited to method calls.

Migrating EJB2.1 Application [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have a legacy financial application which is written using EJB2.1(Making use of Entity Beans, Stateless Session Beans and Couple of MDBs).
I want to migrate the application to latest Java EE or Spring framework. The application consists of around 400 entities and Entity beans are mainly used for Creating and Updating.
For the viewing part a separate DAO layer is there and I don't want to touch that part.Also I want to keep the business logic written in service beans as it is very complex to re write.
i.e., I simply want to replace the ORM part of the system. The application is making use of JTA transactions.
Sorry to ask a very high level question, but which technology I can use to replace the ORM.
Spring/Hibernate
Java EE
The primary considerations for the application would be scalability, performance also ease of deployment.
I just want opinions on who have used these technologies, I don't want to start a war between 'evangelists'.
If you find the input is not suffcient please ask me I can provide more details.
the argument here is really between EJB-3.x versus Spring/Hibernate the first caveat being that one does not necessarily mutually exclude the other. (annotations, container, testing, etc.)
there's lots of support in migrating to EJB 2.1 to EJB 3.x and lots of toolsets to assist. one of the principal challenges that i've seen with EJB is integration testing outside the container. (for example. in a continuous integration environment) There are JTA solutions, JNDI solutions and others to support but on the whole, i've found that there is more 'out-of-container' testing support on the Spring migration path than Java EE. that said, there are foundation frameworks such as Arquillian from JBoss designed to support this.
so i would suggest you look at EJB 2.1 to EJB 3 migration paths and then look at the Arquillian framework for integration testing support

Resources