I am making a EventService jar library for other two modules use. The source code that I build to a jar library have class below.
public class DatabaseConfig {
private String username;
private String password;
private int maximumPoolSize;
public DatabaseConfig(String username, String password, int maximumPoolSize) {
this.username = username;
this.password = password;
this.maximumPoolSize = maximumPoolSize;
}
public String getUsername() {
return username;
}
public void setUsername (String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword (String password) {
this.password = password;
}
public int getMaximumPoolSize() {
return maximumPoolSize;
}
public void setMaximumPoolSize (int maximumPoolSize) {
this.maximumPoolSize = maximumPoolSize;
}
}
public class DatabasePublisher {
private final DatabaseConfig databaseConfig;
private final String name;
private final String email;
public DatabasePublisher(DatabaseConfig databaseConfig, String name, String email) {
this.databaseConfig = databaseConfig;
this.name = name;
this.email = email;
}
public String getDatabaseConfig() {
return databaseConfig;
}
public void setDatabaseConfig (DatabaseConfig databaseConfig) {
this.databaseConfig = databaseConfig;
}
public String getName() {
return name;
}
public void setName (String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail (String email) {
this.email = email;
}
}
public class EventService {
private static final Logger log = LoggerFactory.getLogger(EventService.class);
private final DatabasePublisher databasePublisher;
private final String eventName;
private final int totalMessage;
public EventService(DatabasePublisher databasePublisher, String eventName, int totalMessage) {
this.databasePublisher = databasePublisher;
this.eventName = eventName;
this.totalMessage = totalMessage;
}
public void handleEvent() {
//TO DO
}
}
I have two modules use this library: ReceiveEventMessage1 and ReceiveEventMessage2.
I only want ReceiveEventMessage1 use the DatabasePublisher in the library and do not want to use it in ReceiveEventMessage2. So I only make a configuration for DatabasePublisher in ReceiveEventMessage1 and it run normally. But if I do not make this configuration for ReceiveEventMessage2 module, it will throw the issue below when I run the ReceiveEventMessage2 app:
Parameter 5 of constructor in com.eventApp.EventService required a bean of type 'com.eventApp.DatabasePublisher' that could not be found.
Action:
Consider defining a bean of type 'com.eventApp.DatabasePublisher' in your configuration.
I do not want to make a DatabasePublisher configuration for ReceiveEventMessage2. So I need to refactor DatabasePublisher class in the EventService library to use DatabasePublisher optional.
Anyone can give me solution for this case? I used @Lazy and @ConditionalOnProperty for DatabasePublisher class but it not effective. My idea is create a DatabaseConfig empty bean but I do not know how to do it. Thanks.
CodePudding user response:
As you are creating a library for other applications to use , it should be designed as generic as possible such that it can work with all cases that you need to support which is with and without DatabasePublisher in your case. For example, you can provide different static factory methods on it to create an instance for different cases , something like:
public class EventService {
public static EventService newInstanceWithDatabasePublisher(DatabasePublisher databasePublisher,String eventName, int totalMessag){
}
public static EventService newInstanceWithoutDatabasePublisher(String eventName, int totalMessag){
}
}
Then let the clients to define how to create DatabasePublisher using @Bean method.
So for ReceiveEventMessage1 :
@Configuration
public class AppConfig {
@Bean
public EventService eventService(){
return EventService.newInstanceWithDatabasePublisher(...,...,..);
}
}
And ReceiveEventMessage2 :
@Configuration
public class AppConfig {
@Bean
public EventService eventService(){
return EventService.newInstanceWithoutDatabasePublisher(...,...,..);
}
}
CodePudding user response:
If DatabasePublisher is an optional dependency of EventService, you can use setter injection instead of constructor injection.
