Java Spring Boot 无法识别 application.properties 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43818248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot not recognizing application.properties file
提问by Yu Chen
I'm trying to configure a DynamoDb client with Spring Boot, and placed my endpoints and configuration information in my resources/application.properties file. However, Spring Boot does not seem to pick up these properties. It does pick up the "server.default" key that I have stored in the same file, so it is definitely recognizing the the file itself.
我正在尝试使用 Spring Boot 配置 DynamoDb 客户端,并将我的端点和配置信息放在我的 resources/application.properties 文件中。但是,Spring Boot 似乎并没有选择这些属性。它确实选择了我存储在同一个文件中的“server.default”键,因此它肯定会识别文件本身。
Here is my application.properties file and the class I'm trying to load properties into (DynamoDBClientMapper):
这是我的 application.properties 文件和我试图将属性加载到(DynamoDBClientMapper)中的类:
amazon.dynamodb.endpoint=http://localhost:8000/
amazon.dynamodb.region=us-west-1
amazon.aws.accesskey=key
amazon.aws.secretkey=key2
server.port=8080
Here is the relevant class I'm trying to load properties into. I tried the @PropertySourceannotation with a new properties file, as well as EnableAutoConfiguration, but neither are registering the properties file(s).
这是我尝试将属性加载到的相关类。我尝试了带有新属性文件的@PropertySource注释以及EnableAutoConfiguration,但都没有注册属性文件。
@PropertySource("database.properties")
public class DynamoClientMapper {
@Value("${amazon.dynamodb.endpoint}")
private String amazonDynamoDBEndpoint;
@Value("${amazon.aws.accesskey}")
private String amazonAWSAccessKey;
@Value("${amazon.aws.secretkey}")
private String amazonAWSSecretKey;
@Value("${amazon.aws.region}")
private String amazonAWSRegion;
Here is my App.java:
这是我的 App.java:
@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class App {
// private static final Logger logger = Logger.getLogger(App.class.toString());
public static void main(String[] args){
SpringApplication.run(App.class, args);
}
}
Here is the stack trace:
这是堆栈跟踪:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dynamoClientMapper' defined in file [C:\Users\ychen4\Desktop\DiningApplication\target\classes\main\java\com\dining\dao\DynamoClientMapper.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [main.java.com.dining.dao.DynamoClientMapper$$EnhancerBySpringCGLIB$$f4ba10ad]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: endpoint cannot be null
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1155) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
at main.java.com.dining.App.main(App.java:18) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.5.2.RELEASE.jar:1.5.2.RELEASE]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [main.java.com.dining.dao.DynamoClientMapper$$EnhancerBySpringCGLIB$$f4ba10ad]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: endpoint cannot be null
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1147) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
... 22 common frames omitted
Caused by: java.lang.IllegalArgumentException: endpoint cannot be null
at com.amazonaws.util.RuntimeHttpUtils.toUri(RuntimeHttpUtils.java:182) ~[aws-java-sdk-core-1.11.125.jar:na]
at com.amazonaws.util.RuntimeHttpUtils.toUri(RuntimeHttpUtils.java:171) ~[aws-java-sdk-core-1.11.125.jar:na]
at com.amazonaws.AmazonWebServiceClient.toURI(AmazonWebServiceClient.java:238) ~[aws-java-sdk-core-1.11.125.jar:na]
at com.amazonaws.AmazonWebServiceClient.setEndpoint(AmazonWebServiceClient.java:228) ~[aws-java-sdk-core-1.11.125.jar:na]
at com.amazonaws.client.builder.AwsClientBuilder.setRegion(AwsClientBuilder.java:362) ~[aws-java-sdk-core-1.11.125.jar:na]
at com.amazonaws.client.builder.AwsClientBuilder.configureMutableProperties(AwsClientBuilder.java:337) ~[aws-java-sdk-core-1.11.125.jar:na]
at com.amazonaws.client.builder.AwsSyncClientBuilder.build(AwsSyncClientBuilder.java:46) ~[aws-java-sdk-core-1.11.125.jar:na]
at main.java.com.dining.dao.DynamoClientMapper.<init>(DynamoClientMapper.java:32) ~[classes/:na]
at main.java.com.dining.dao.DynamoClientMapper$$EnhancerBySpringCGLIB$$f4ba10ad.<init>(<generated>) ~[classes/:na]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_121]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_121]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_121]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_121]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
... 24 common frames omitted
I've tried making another separate database.properties file , but Spring Boot isn't recognizing that either. What am I doing wrong?
我尝试制作另一个单独的 database.properties 文件,但 Spring Boot 也无法识别。我究竟做错了什么?
采纳答案by SkyWalker
You can make a try to define resources tag in the build section in your pom.xml file. Set path for resource directory where is application.properties
您可以尝试在 pom.xml 文件的构建部分中定义资源标签。设置资源目录的路径在哪里application.properties
<build>
<resources>
<resource>
<directory>resources</directory>
<targetPath>${project.build.outputDirectory}</targetPath>
<includes>
<include>application.properties</include>
</includes>
</resource>
</resources>
</build>
Resource Link: https://stackoverflow.com/a/30595114/2293534
资源链接:https: //stackoverflow.com/a/30595114/2293534
Another approach:
另一种方法:
If you use spring 3.X version, You can add @PropertySource("application.properties")
如果您使用 spring 3.X 版本,您可以添加 @PropertySource("application.properties")
@Configuration
@PropertySource(value = "classpath:application.properties")
public class ApplicationConfig {
// more configuration ...
}
If you use spring 4 version, you add 2 properties file using new annotation called @PropertySources that allows you to declare repeated @PropertySource annotations:
如果您使用 spring 4 版本,则使用名为 @PropertySources 的新注释添加 2 个属性文件,该注释允许您声明重复的 @PropertySource 注释:
@PropertySources({
@PropertySource("default.properties"),
@PropertySource("overriding.properties")
})
Details is given here in my another answer: https://stackoverflow.com/a/43659158/2293534
在我的另一个答案中给出了详细信息:https: //stackoverflow.com/a/43659158/2293534
UPDATE#1:
更新#1:
Replace your App.java class with following class
用下面的类替换你的 App.java 类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
//@SpringBootApplication
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass);
}
private static Class<Application> applicationClass = Application.class;
}
For java.io.FileNotFoundException:
对于 java.io.FileNotFoundException:
Use the following
使用以下
@PropertySource(value = "database.properties", ignoreResourceNotFound = true)
UPDATE#2:
更新#2:
I have followed the following steps to run your application. It runs successfully.
我已按照以下步骤运行您的应用程序。它运行成功。
Go to your project folder where pom.xml is exists.
You have some errors and warning on pom.xml. I have clarified all.
Open command prompt and Run
mvn clean
Run
mvn clean install
At last
mvn spring-boot:run
转到存在 pom.xml 的项目文件夹。
您在 pom.xml 上有一些错误和警告。我都澄清了。
打开命令提示符并运行
mvn clean
跑
mvn clean install
最后
mvn spring-boot:run
Then in browser, I run "http://localhost:8080/"
然后在浏览器中,我运行“ http://localhost:8080/”
It open's the project successfully. I have also searched other pages also opened successfully.
它成功打开了项目。我还搜索了其他页面也成功打开。
First page looks like below http://localhost:8080/
第一页如下所示 http://localhost:8080/
Review all pages looks like below: http://localhost:8080/api/reviews
查看所有页面如下所示: http://localhost:8080/api/reviews
[{"id":1,"userName":"ychennay","reviewText":"This restaurant was terrific!"},{"id":2,"userName":"david","reviewText":"This restaurant was okay!"},{"id":3,"userName":"ben","reviewText":"This restaurant was mediocre!"},{"id":4,"userName":"leon","reviewText":"This restaurant was awful!"},{"id":5,"userName":"lawrence","reviewText":"This restaurant was confusing!"}]
[{"id":1,"userName":"ychennay","reviewText":"这家餐厅太棒了!"},{"id":2,"userName":"david","reviewText":"这个餐厅还行!"},{"id":3,"userName":"ben","reviewText":"这家餐厅很一般!"},{"id":4,"userName":"leon", "reviewText":"这家餐厅太糟糕了!"},{"id":5,"userName":"lawrence","reviewText":"这家餐厅令人困惑!"}]
So Replace your pom.xml
所以替换你的 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.diningapp</groupId>
<artifactId>Dining</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<Hymanson.version>2.7.5</Hymanson.version>
<spring-version>4.3.7.RELEASE</spring-version>
<dynamodb-local.port>8000</dynamodb-local.port>
<dynamodb-local.endpoint>http://localhost:${dynamodb-local.port}</dynamodb-local.endpoint>
<spring-boot-version>1.5.2.RELEASE</spring-boot-version>
<aws-sdk-java-version>1.11.124</aws-sdk-java-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- For UTF-8 support -->
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- For UTF-8 support -->
</properties>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source> <!-- Used java7 -->
<target>1.7</target> <!-- Used java7 -->
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>dynamodb-local-oregon</id>
<name>DynamoDB Local Release Repository</name>
<url>https://s3-us-west-2.amazonaws.com/dynamodb-local/release</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Hopper-SR10</version>
<type>pom</type>
<!-- <scope>import</scope> -->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>${spring-boot-version}</version> <!-- You have missed to add this version -->
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${spring-boot-version}</version> <!-- You have missed to add this version -->
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot-version}</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>${aws-sdk-java-version}</version>
</dependency>
<dependency>
<groupId>com.github.derjust</groupId>
<artifactId>spring-data-dynamodb</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>${aws-sdk-java-version}</version>
<type>pom</type>
<!-- <scope>import</scope> -->
<scope>provided</scope> <!-- changed import to provided -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>${spring-boot-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
<version>${Hymanson.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version> <!-- You have missed to add this version -->
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>${spring-boot-version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Errors and solutions:
错误及解决方法:
Issue#1:
问题#1:
[WARNING] 'dependencies.dependency.scope' for org.springframework.data:spring-data-releasetrain:pom must be one of [provided, compile, runtime, test, system] but is 'import'. @ line 70, column 18
[警告] org.springframework.data:spring-data-releasetrain:pom 的“dependencies.dependency.scope”必须是 [provided, compile, runtime, test, system] 之一,但是是“import”。@ 第 70 行,第 18 列
Solution#1:
解决方案#1:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Hopper-SR10</version>
<type>pom</type>
<!-- <scope>import</scope> -->
<scope>provided</scope> <!-- changed import to provided -->
</dependency>
Issue#2:
问题#2:
[ERROR] 'dependencies.dependency.version' for org.springframework.boot:spring-boot-devtools:jar is missing. @ line 73, column 19
[错误] 缺少 org.springframework.boot:spring-boot-devtools:jar 的“dependencies.dependency.version”。@ 第 73 行,第 19 列
Solution#2:
解决方案#2:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>${spring-boot-version}</version> <!-- You have missed to add this version -->
<optional>true</optional>
</dependency>
Issue#3:
问题#3:
[ERROR] 'dependencies.dependency.version' for org.springframework.boot:spring-boot-configuration-processor:jar is missing. @ line 78, column 19
[错误] org.springframework.boot:spring-boot-configuration-processor:jar 的“dependencies.dependency.version”丢失。@ 第 78 行,第 19 列
Solution#3:
解决方案#3:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${spring-boot-version}</version> <!-- You have missed to add this version -->
<optional>true</optional>
</dependency>
Issue#4:
问题#4:
[WARNING] 'dependencies.dependency.scope' for com.amazonaws:aws-java-sdk-bom:pom must be one of [provided, compile, runtime, test, system] but is 'import'. @ line 105, column 18
[警告] com.amazonaws:aws-java-sdk-bom:pom 的“dependencies.dependency.scope”必须是 [provided, compile, runtime, test, system] 之一,但是是“import”。@ 第 105 行,第 18 列
Solution#4:
解决方案#4:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>${aws-sdk-java-version}</version>
<type>pom</type>
<!-- <scope>import</scope> -->
<scope>provided</scope> <!-- changed import to provided -->
</dependency>
Issue#5:
问题#5:
[ERROR] 'dependencies.dependency.version' for mysql:mysql-connector-java:jar is missing. @ line 148, column 19
[错误] mysql:mysql-connector-java:jar 的“dependencies.dependency.version”丢失。@ 第 148 行,第 19 列
Solution#5:
解决方案#5:
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version> <!-- You have missed to add this version -->
</dependency>
回答by Achille_vanhoutte
have tried the following approach?
是否尝试过以下方法?
@Component
@PropertySource("database.properties")
public class DynamoClientMapper { ...}
or
或者
@Service
@PropertySource("database.properties")
public class DynamoClientMapper { ...}
回答by jshcode
Instead of @EnableAutoConfiguration, use @Configuration as below. Also you will need to fix aws region property as its differing in name between prop file and code - [amazon.dynamodb.region vs amazon.aws.region] - this will throw error once it starts picking up property file after below change..
使用@Configuration 代替@EnableAutoConfiguration,如下所示。此外,您还需要修复 aws 区域属性,因为它在 prop 文件和代码之间的名称不同 - [amazon.dynamodb.region vs amazon.aws.region] - 在以下更改后开始拾取属性文件时,这将引发错误..
@Configuration
@PropertySource("database.properties")
public class DynamoClientMapper {
@Value("${amazon.dynamodb.endpoint}")
private String amazonDynamoDBEndpoint;
@Value("${amazon.aws.accesskey}")
private String amazonAWSAccessKey;
@Value("${amazon.aws.secretkey}")
private String amazonAWSSecretKey;
@Value("${amazon.aws.region}")
private String amazonAWSRegion;
回答by Torsten
回答by slartidan
Please double-check, that your maven-pom uses this packaging:
请仔细检查,您的 maven-pom 是否使用此包装:
<packaging>war</packaging>
If it is set to pom
or similar, your IDE might not recognize the "Spring"-nature of your module (happend to me in IntelliJ 2018).
如果设置为pom
或类似,您的 IDE 可能无法识别模块的“Spring”性质(在 IntelliJ 2018 中发生在我身上)。
回答by SJX
You can use also a fixed path.
您也可以使用固定路径。
@PropertySource("file:${C:/Development/workspace/Project/resources/}/application.properties")
@PropertySource("file:${C:/Development/workspace/Project/resources/}/application.properties")
Helpful in development process where you want to use different property files.
在要使用不同属性文件的开发过程中很有帮助。