Java Spring MVC-在控制器上设置初始化属性

Java Spring MVC-在控制器上设置初始化属性,java,spring,jakarta-ee,spring-mvc,properties,Java,Spring,Jakarta Ee,Spring Mvc,Properties,我有一个SpringMVC,它使用一个我无法访问代码的外部库。此外部库使用标准system.getProperty调用读取某些属性。在使用服务之前,我必须设置这些值 由于我的应用程序是SpringMVC应用程序,我不确定如何初始化这些属性。这是我到目前为止所做的,但由于某些原因,我认为这些值总是空的 我将属性放入属性文件/conf/config.properties my.user=myuser my.password=mypassowrd my.connection=(DESCRIPTION=

我有一个SpringMVC,它使用一个我无法访问代码的外部库。此外部库使用标准system.getProperty调用读取某些属性。在使用服务之前,我必须设置这些值

由于我的应用程序是SpringMVC应用程序,我不确定如何初始化这些属性。这是我到目前为止所做的,但由于某些原因,我认为这些值总是空的

我将属性放入属性文件
/conf/config.properties

my.user=myuser
my.password=mypassowrd
my.connection=(DESCRIPTION=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=xxxx.xxxx.xxxx)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=xxx.xxx.xxx)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=myService)))
然后,我在我的
applicationContext.xml

<context:annotation-config/>
<context:property-placeholder location="classpath*:conf/config.properties"/>    
问题是调用
AfterPropertieSet()
方法时,值总是为null

  • 上述方法是否是初始化代码的正确方法,尤其是针对控制器?如果第二次呼叫控制器会发生什么
  • 值是否因初始化而为空?i、 春天还没有把它们放好
  • 是否可以将初始化代码添加到远离控制器的位置

您确定bean/controller的定义与
属性占位符的定义在同一个spring上下文配置文件中吗

看看Boris对这个问题的回答:

如果要将代码从控制器中移出,可以添加一个组件,用于侦听spring完成初始化后的代码,然后hen调用代码:

@Component
public class ApplicationStartedListener implements ApplicationListener<ContextRefreshedEvent> {

    private static @Value("${my.user}") String username;
    private static @Value("${my.password}") String password;
    private static @Value("${my.connection}") String connectionString;

    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.setProperty("username",username);
        System.setProperty("password",password);
        System.setProperty("connectionString",connectionString);
    } 
}
@组件
公共类ApplicationStartedListener实现ApplicationListener{
私有静态@Value(“${my.user}”)字符串用户名;
私有静态@Value(“${my.password}”)字符串密码;
私有静态@Value(“${my.connection}”)字符串connectionString;
ApplicationEvent(ContextRefreshedEvent事件)上的公共无效{
System.setProperty(“用户名”,用户名);
System.setProperty(“密码”,password);
setProperty(“connectionString”,connectionString);
} 
}

修复应该相当简单,只需从字段中删除
静态
修饰符,然后删除负责在带有
@AuotWired
@Value
注释的字段中注入的
AutowiredNotationPostProcessor
,将能够正确地插入值,并且您的
AfterPropertieSet
将被干净地调用

我只有一个位于WEB-INF文件夹中的applicationContext.xml文件。System.setProperty调用都在控制器的无参数构造函数中。也许这就是问题的原因。@ziggy我根据你的问题假设所有@Value变量都为空。你是说它们的值是从配置文件中正确填写的,但是
System.setProperty()
没有设置值吗?不,你是正确的,因为@Value值从未设置过,所以它们在进入System.setProperty调用之前是空的。这似乎没有解决问题。使用静态变量是错误的,所以我将它们更改为实例变量,但属性仍然为null。
@Component
public class ApplicationStartedListener implements ApplicationListener<ContextRefreshedEvent> {

    private static @Value("${my.user}") String username;
    private static @Value("${my.password}") String password;
    private static @Value("${my.connection}") String connectionString;

    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.setProperty("username",username);
        System.setProperty("password",password);
        System.setProperty("connectionString",connectionString);
    } 
}