【问题标题】:Passing Spring Boot Property Bean to Classes in other Projects将 Spring Boot 属性 Bean 传递给其他项目中的类
【发布时间】:2018-05-15 17:16:08
【问题描述】:

我正在配置一个 Spring Boot 应用程序。所以我创建了一个这样的属性 bean 类:

import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@EnableConfigurationProperties
@PropertySource("classpath:/application.properties")
public class APPStaticProperties {

    @NotNull
    @Value("${dblog.system.name}")
    String db_logsystem_name;

    /**
     * @return the db_logsystem_name
     */
    public String getDb_logsystem_name() {
        return db_logsystem_name;
    }

    /**
     * @param db_logsystem_name the db_logsystem_name to set
     */
    public void setDb_logsystem_name(String db_logsystem_name) {
        this.db_logsystem_name = db_logsystem_name;
    }

}

我正在控制器类中创建一个对象:

@Autowired
APPStaticProperties appStaticProperties;

但我想知道如何传递 this 对象以在其他项目中使用?因为我的代码流从控制器到 JSP,然后到另一个项目中的类。我需要该类中可用的对象。老实说,在许多其他项目中也是如此!我无法弄清楚该怎么做。也没有太多的例子可供参考。

【问题讨论】:

    标签: java spring spring-boot properties


    【解决方案1】:

    您通常不会将 @Configuration bean 注入到其他 Spring 托管 bean 中,因为它会设置其他 Spring 托管 bean 使用的配置。

    例如,因为您已经声明了一个@PropertySource,所以要在其他 Spring 托管中访问您的属性意味着您可以使用 @Value 将属性注入到其他地方的代码中。您不需要注入 APPStaticProperties bean 来执行此操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-18
      • 1970-01-01
      • 2020-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多