如何使类bean可选用于其他模块



我正在制作一个EventService jar库,供其他两个模块使用。我构建到jar库的源代码有下面的类。

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
}
}

我有两个模块使用这个库:ReceiveEventMessage1和ReceiveEventMessage2。

我只希望ReceiveEventMessage1使用库中的DatabasePublisher,而不希望在ReceiveEventMessage2中使用它。所以我只在ReceiveEventMessage1中为DatabasePublisher进行了配置,它可以正常运行。但是,如果我不为ReceiveEventMessage2模块进行此配置,当我运行ReceiveEventMessage 2应用程序时,它将抛出以下问题:

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.

我不想为ReceiveEventMessage2进行DatabasePublisher配置。因此,我需要重构EventService库中的DatabasePublisher类,以使用可选的DatabasePublisher。

有人能给我这个案子的解决方案吗?我在DatabasePublisher类中使用了@Lazy和@ConditionalOnProperty,但效果不佳。我的想法是创建一个DatabaseConfig空bean,但我不知道如何做到这一点。谢谢

当您创建一个库供其他应用程序使用时,它应该设计得尽可能通用,以便它可以处理您需要支持的所有情况,在您的情况下,无论是否使用DatabasePublisher。例如,您可以在上面提供不同的静态工厂方法来为不同的情况创建一个实例,比如:

public class EventService {

public static EventService newInstanceWithDatabasePublisher(DatabasePublisher databasePublisher,String eventName, int totalMessag){
}

public static EventService newInstanceWithoutDatabasePublisher(String eventName, int totalMessag){
}
}

然后让客户端定义如何使用@Bean方法创建DatabasePublisher

因此,对于ReceiveEventMessage1:

@Configuration
public class AppConfig {

@Bean
public EventService eventService(){
return EventService.newInstanceWithDatabasePublisher(...,...,..); 
}
}

ReceiveEventMessage2:

@Configuration
public class AppConfig {

@Bean
public EventService eventService(){
return EventService.newInstanceWithoutDatabasePublisher(...,...,..); 
}
}

如果DatabasePublisherEventService的可选依赖项,则可以使用setter注入而不是构造函数注入。

最新更新