spring - 从类的静态字段中的属性文件中读取属性值

use*_*352 1 java spring properties spring-mvc

我有一个实用程序类,我有一个方法,需要用户名和密码来连接其他网址.我需要在属性文件中保留该用户名,以便我可以随时更改它.但是当我在静态方法(实用程序类)中使用它时,问题是它显示为null.(即它无法从属性文件中读取).

但是,当我在其他控制器中使用这些值时,他们就会到达那里.所以我的问题是如何在静态字段中读取属性值

<bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath*:/myservice_detaults.properties</value>
            <value>classpath*:/log4j.properties</value>
        </list>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

//在Utitlity类代码中

  @Value("${app.username}")
      static String userName;

public static connectToUrl(){
  //use userName
 //userName showing null
}
Run Code Online (Sandbox Code Playgroud)

bit*_*kot 11

在您的Utility课程中,您可以使用setter方法设置属性,然后您可以使用MethdInvokingFactoryBean.

class Utility{
    static String username;
    static String password;
    public static setUserNameAndPassword(String username, String password){
        Utility.username = username;
        Utility.password = password;
    }
    //other stuff
}

<bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath*:/myservice_detaults.properties</value>
            <value>classpath*:/log4j.properties</value>
        </list>
    </property>
</bean>

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="staticMethod" value="foo.bar.Utility.setUserNameAndPassword"/>
    <property name="arguments">
        <list>
            <value>${username}</value>
            <value>${password}</value>
        </list>
   </property>
</bean>
Run Code Online (Sandbox Code Playgroud)


小智 9

或者使用@Valueover the non-static setter 方法,username 例如。

@Value("${app.username}")
public void setUserName(String userName) {
    UtilityClass.userName = userName;
}
Run Code Online (Sandbox Code Playgroud)


Man*_*tra 6

试试这个:让你的类成为一个组件

@Component
public class UserXXXUtils {
    private static Integer trustXXXMask;

    @Value("${trustXXXMask}")
    public void setTrustXXXMask(Integer trustXXXMask) {
        UserXXXUtils.trustXXXMask = trustXXXMask;
    }
    //Access anywhere in the class
}
Run Code Online (Sandbox Code Playgroud)


Raj*_*esh 5

Read property value from properties file in static field of class using Java based spring configuration.
Example :
// The property file to store fields.
user.properties
     username=Elijah Wood
     age=26
     language=English
// This class holds the static values
Run Code Online (Sandbox Code Playgroud)

包 org.javahive.propertyreader.example;

public class UserDetails {

    static String username;
    static String age;
    static String language;

    public static void setUserValues(String username, String age, String language) {
        UserDetails.username = username;
        UserDetails.age = age;
        UserDetails.language = language;
    }
}

//Spring configuration class

package org.javahive.propertyreader.example;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.MethodInvokingFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

@Configuration
@ComponentScan(value = { "org.javahive.propertyreader.example" })
@PropertySource("classpath:user.properties")
public class PropertyReaderConfig {

    @Value("${user}")
    private String username;

    @Value("${age}")
    private String age;

    @Value("${language}")
    private String language;

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigIn() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public MethodInvokingFactoryBean methodInvokingFactoryBean() {
        MethodInvokingFactoryBean mifb = new MethodInvokingFactoryBean();
        mifb.setStaticMethod("org.javahive.propertyreader.example.UserDetails.setUserValues");
        mifb.setArguments(new String[] { this.username, this.age, this.language });
        return mifb;
    }

    /**
     * @return the name
     */
    public String getName() {
        return username;
    }

    /**
     * @param name
     *            the name to set
     */
    public void setName(String name) {
        this.username = name;
    }

    /**
     * @return the age
     */
    public String getAge() {
        return age;
    }

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

    /**
     * @return the language
     */
    public String getLanguage() {
        return language;
    }

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

}

//The main class.
Run Code Online (Sandbox Code Playgroud)

包 org.javahive.propertyreader.example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(PropertyReaderConfig.class);
        System.out.println("User Name : " + UserDetails.username);
        System.out.println("Age : " + UserDetails.age);
        System.out.println("Language : " + UserDetails.language);
    }
}
Run Code Online (Sandbox Code Playgroud)