Reputation: 5
We are working on PAAS applications (Spring Boot) which have 2-3 different modules in which each module has some redundant code for creating and closing database and MQ connections.
We are using Hikari DataSource for maintaining pool connections, and are planning to centralize the code which contains configuration data, putting that as a parent dependency in others.
Is there any suitable java design pattern which fulfills this use case?
Upvotes: 0
Views: 872
Reputation:
If I am interpreting your question correctly, I assume that it would be a good idea to create one class which handles the connections and annotate it with an @Component
annotation. You can then refer to the class by using the @Autowired
annotation in your other classes. for example:
@Component
public class MyDataSource {
// ....
}
Spring boot can automatically wire the component into other classes as follows (notice the @Autowired
annotation above the constructor):
public class SomeServiceUsingTheDataSource {
private final MyDataSource myDataSource;
@Autowired
public SomeServiceUsingTheDataSource(MyDataSource myDataSource) {
this.myDataSource = myDataSource;
}
}
Upvotes: 0
Reputation: 390
In spring boot no need to create new class using a design pattern, spring handle itself. only need to following propertis in application.propertis
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
Upvotes: 0
Reputation: 26
Probably either Factory Design Pattern or Abstract factory design pattern; They are conceptually almost same. UML diagram of it Factory Design Pattern
The Abstract Factory Design Pattern sounds fancier, but isn't anything more complex than the Factory Design Pattern. Abstract Factory Design Pattern.
Upvotes: 1