JHipster中的新属性文件不起作用

时间:2018-07-12 15:10:53

标签: spring jhipster

首先,我了解JHipster创建了要使用的ApplicationProperty。但是出于测试目的,我创建了以下内容:

Class TestProperties

package com.xxx.yyy.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "test", ignoreUnknownFields = false)
public class TestProperties {

    private String dummyValue;

    public String getDummyValue() {
        return dummyValue;
    }

    public void setDummyValue(String dummyValue) {
        this.dummyValue = dummyValue;
    }

}

Class TestService

package com.xxx.yyy.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.xxx.yyy.config.TestProperties;

@Service
public class TestService {

     private final Logger log = LoggerFactory.getLogger(TestService.class);

     private final TestProperties testProperties;

     public TestService(TestProperties testProperties) {
         this.testProperties = testProperties;
     }

     public void test() {
         log.debug("show have val" + testProperties.getDummyValue());
     }
}

在application-dev.yml中,我有

# application:

test:
    dummy-value: Test Value

但是,当我运行mvn时,出现以下错误,有人知道出了什么问题吗?

DEBUG 10760 --- [  restartedMain] c.ehcache.core.Ehcache-usersByLogin      : Close successful.
ERROR 10760 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   :

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.xxx.yyy.service.TestService required a bean of type 'com.xxx.yyy.config.TestProperties' that could not be found.


Action:

Consider defining a bean of type 'com.xxx.yyy.config.TestProperties' in your configuration.

我知道从Spring 4.3开始,不需要@Autowired的注释,并且构造函数的任何参数都是Spring Bean。我在服务中使用JHipsterProperties进行了测试,并且可以正常工作。

 public TestService(JHipsterProperties jHipsterProperties ) {
     this.jHipsterProperties = jHipsterProperties;
 }

有人知道为什么我的新属性不起作用吗?

1 个答案:

答案 0 :(得分:4)

要加载的属性,需要将其添加到应用程序主类的@EnableConfigurationProperties中。对于jhipster-sample-app,it would be added here