@valueを使用して、プロパティファイルから値を読み取ることができませんか的環境にAutowiredを使用して、

アマンVyasさん:

私は私の春のブートプロジェクトに2つのプロパティファイルを持っています。そして、私は1つのクラスの両方からプロパティを読み取ることができています。しかし、私は@valueを使用して、別のクラスからかAutowired環境で読み取るしようとしている同じ値が、それは私にはnullを与えています。

    prop.name=test /*   property file value */

    @Component
    public class TestUtil { // This is the class giving me null value

        @Value("${prop.name}")
        String st;

        public String getTestString()
        {
            System.out.println(st+ " ***");
            return st;

        }
    }

//Using @Autowired Enviroment
public class TestUtil {

    @Autowired
    private Environment env;


    public String getTestString()
    {
        System.out.println(env.getProperty("prop.name")+ " ***");
        return env.getProperty("prop.name");

    }
}

/* Class below giving me value from properties file*/

        public class JsonWriter extends JsonResponseWriter {

        @Value("${prop.name}")
        private String contentsMenus;

      /* Some method*/
       System.err.println("from JsonWriter  "+contentsMenus);

ここで私は、オートワイヤリングしています

@Service
public class ResponseUtil {
    @Autowired
     private TestUtil util ;

上記のクラスでは、私はautowiredを使用しています

アマンVyasさん:

M. Deinum後のコメントは、コードを分析し、私は別のクラスを呼び出した場所からの最初のクラスは、それが働いたクラスをオートワイヤリングした後autowired.Soていないと判断しました。たくさんM.Deinumと私の質問に答え、すべてのユーザー@感謝。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=200820&siteId=1