Spring,从数据库自动装配@Value

发布于 2024-08-28 14:29:50 字数 237 浏览 5 评论 0原文

我正在使用属性文件来存储一些配置属性,这些配置属性可以通过以下方式访问:

@Value("#{configuration.path_file}")
private String pathFile;

Is it possible (with Spring 3) to use the same @Value comment, but要从数据库加载属性而不是一个文件?

I am using a properties File to store some configuration properties, that are accessed this way:

@Value("#{configuration.path_file}")
private String pathFile;

Is it possible (with Spring 3) to use the same @Value annotation, but loading the properties from a database instead of a file ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

英雄似剑 2024-09-04 14:29:50

假设您的数据库中有一个表存储了键/值对:

定义一个新的 bean“applicationProperties” - 伪代码如下...

public class ApplicationProperties {
    @AutoWired
    private DataSource datasource;

    public getPropertyValue(String key) {
        // transact on your datasource here to fetch value for key
        // SNIPPED
    }
}

在应用程序中需要的地方注入此 bean。如果您已经有一个 dao/service 层,那么您只需使用它即可。

Assuming you have a table in your database stored key/value pairs:

Define a new bean "applicationProperties" - psuedo-code follows...

public class ApplicationProperties {
    @AutoWired
    private DataSource datasource;

    public getPropertyValue(String key) {
        // transact on your datasource here to fetch value for key
        // SNIPPED
    }
}

Inject this bean where required in your application. If you already have a dao/service layer then you would just make use of that.

許願樹丅啲祈禱 2024-09-04 14:29:50

是的,您可以保留 @Value 注释,并在 EnvironmentPostProcessor。

从 Spring Boot 1.3 开始,我们可以在刷新应用程序上下文之前使用 EnvironmentPostProcessor 自定义应用程序的环境。

例如,创建一个实现EnvironmentPostProcessor的类:

public class ReadDbPropertiesPostProcessor implements EnvironmentPostProcessor {

    private static final String PROPERTY_SOURCE_NAME = "databaseProperties";

    private String[] CONFIGS = {
            "app.version"
            // list your properties here
    };

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        Map<String, Object> propertySource = new HashMap<>();

        try {
            // the following db connections properties must be defined in application.properties
            DataSource ds = DataSourceBuilder
                    .create()
                    .username(environment.getProperty("spring.datasource.username"))
                    .password(environment.getProperty("spring.datasource.password"))
                    .url(environment.getProperty("spring.datasource.url"))
                    .driverClassName("com.mysql.jdbc.Driver")
                    .build();

            try (Connection connection = ds.getConnection();
                 // suppose you have a config table storing the properties name/value pair
                 PreparedStatement preparedStatement = connection.prepareStatement("SELECT value FROM config WHERE name = ?")) {
                for (int i = 0; i < CONFIGS.length; i++) {
                    String configName = CONFIGS[i];
                    preparedStatement.setString(1, configName);

                    ResultSet rs = preparedStatement.executeQuery();
                    while (rs.next()) {
                        propertySource.put(configName, rs.getString("value"));
                    }

                    // rs.close();
                    preparedStatement.clearParameters();
                }
            }

            environment.getPropertySources().addFirst(new MapPropertySource(PROPERTY_SOURCE_NAME, propertySource));
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
}

最后,不要忘记将您的spring.factories放入META-INF中。一个例子:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=
  com.baeldung.environmentpostprocessor.autoconfig.PriceCalculationAutoConfig

Yes, you can keep your @Value annotation, and use the database source with the help of EnvironmentPostProcessor.

As of Spring Boot 1.3, we're able to use the EnvironmentPostProcessor to customize the application's Environment before application context is refreshed.

For example, create a class which implements EnvironmentPostProcessor:

public class ReadDbPropertiesPostProcessor implements EnvironmentPostProcessor {

    private static final String PROPERTY_SOURCE_NAME = "databaseProperties";

    private String[] CONFIGS = {
            "app.version"
            // list your properties here
    };

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        Map<String, Object> propertySource = new HashMap<>();

        try {
            // the following db connections properties must be defined in application.properties
            DataSource ds = DataSourceBuilder
                    .create()
                    .username(environment.getProperty("spring.datasource.username"))
                    .password(environment.getProperty("spring.datasource.password"))
                    .url(environment.getProperty("spring.datasource.url"))
                    .driverClassName("com.mysql.jdbc.Driver")
                    .build();

            try (Connection connection = ds.getConnection();
                 // suppose you have a config table storing the properties name/value pair
                 PreparedStatement preparedStatement = connection.prepareStatement("SELECT value FROM config WHERE name = ?")) {
                for (int i = 0; i < CONFIGS.length; i++) {
                    String configName = CONFIGS[i];
                    preparedStatement.setString(1, configName);

                    ResultSet rs = preparedStatement.executeQuery();
                    while (rs.next()) {
                        propertySource.put(configName, rs.getString("value"));
                    }

                    // rs.close();
                    preparedStatement.clearParameters();
                }
            }

            environment.getPropertySources().addFirst(new MapPropertySource(PROPERTY_SOURCE_NAME, propertySource));
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
}

Finally, don't forget to put your spring.factories in META-INF. An example:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=
  com.baeldung.environmentpostprocessor.autoconfig.PriceCalculationAutoConfig
吃素的狼 2024-09-04 14:29:50

虽然没有使用过 spring 3,但我假设你可以,如果你创建一个从数据库读取属性并使用 getter 公开它们的 bean。

Although not having used spring 3, I'd assume you can, if you make a bean that reads the properties from the database and exposes them with getters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文