JAVA代码 写一个工具类,获取另一个moudle下的application-druid.yml的数据库连接信息
时间: 2024-03-11 08:48:39 浏览: 79
可以编写一个静态方法的工具类,用于获取指定模块下的`application-druid.yml`文件中的数据库连接信息。以下是一个示例代码:
```java
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;
import java.io.IOException;
import java.util.Map;
public class DatabaseConfigUtil {
public static DatabaseConfig getDatabaseConfig(String moduleName) throws IOException {
YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
ClassPathResource resource = new ClassPathResource(moduleName + "/application-druid.yml");
PropertySource<?> propertySource = loader.load("database", resource);
Map<String, Object> properties = (Map<String, Object>) propertySource.getSource();
Map<String, Object> dataSourceProperties = (Map<String, Object>) properties.get("spring.datasource");
String url = (String) dataSourceProperties.get("url");
String username = (String) dataSourceProperties.get("username");
String password = (String) dataSourceProperties.get("password");
return new DatabaseConfig(url, username, password);
}
}
```
在上述代码中,我们编写了一个静态方法`getDatabaseConfig`,该方法接收一个`moduleName`参数,表示目标模块的名称。我们首先使用`YamlPropertySourceLoader`加载目标模块下的`application-druid.yml`文件,然后从加载后的属性源中获取`spring.datasource`的属性值,最后将属性值封装到一个`DatabaseConfig`对象中返回。
假设`DatabaseConfig`类的构造方法如下:
```java
public class DatabaseConfig {
private final String url;
private final String username;
private final String password;
public DatabaseConfig(String url, String username, String password) {
this.url = url;
this.username = username;
this.password = password;
}
public String getUrl() {
return url;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
```
在使用该工具类时,只需要调用`getDatabaseConfig`方法并传入目标模块的名称即可,例如:
```java
try {
DatabaseConfig databaseConfig = DatabaseConfigUtil.getDatabaseConfig("moduleA");
String url = databaseConfig.getUrl();
String username = databaseConfig.getUsername();
String password = databaseConfig.getPassword();
// 使用获取到的数据库连接信息进行操作
} catch (IOException e) {
e.printStackTrace();
}
```
需要注意的是,在使用该工具类时,需要确保目标模块的`application-druid.yml`文件能够在classpath下被找到。
阅读全文
相关推荐
![java](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![aar](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)