Spring Boot Not Recognizing Application.Properties File

Spring Boot not recognizing application.properties file

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

<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

Another approach:

If you use spring 3.X version, You can add @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:

@PropertySources({
@PropertySource("default.properties"),
@PropertySource("overriding.properties")
})

Details is given here in my another answer: https://stackoverflow.com/a/43659158/2293534

UPDATE#1:

Replace your App.java class with following class

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:

Use the following

@PropertySource(value = "database.properties", ignoreResourceNotFound = true)

UPDATE#2:

I have followed the following steps to run your application. It runs successfully.

  1. Go to your project folder where pom.xml is exists.

  2. You have some errors and warning on pom.xml. I have clarified all.

  3. Open command prompt and Run mvn clean

  4. Run mvn clean install

  5. At last mvn spring-boot:run

Then in browser, I run "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/.

Java Sample Image 2

Review all pages looks like below: 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!"}]

So Replace your 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>
<jackson.version>2.7.5</jackson.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.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.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:

[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

Solution#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:

[ERROR] 'dependencies.dependency.version' for org.springframework.boot:spring-boot-devtools:jar is missing. @ line
73, column 19

Solution#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:

[ERROR] 'dependencies.dependency.version' for org.springframework.boot:spring-boot-configuration-processor:jar is
missing. @ line 78, column 19

Solution#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:

[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

Solution#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:

[ERROR] 'dependencies.dependency.version' for mysql:mysql-connector-java:jar is missing. @ line 148, column 19

Solution#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>

Spring Boot app: Not picking up application.properties?

This was obscure - and the other answers were very helpful in getting me pointed in the right direction.

After trying the suggested solutions, I poked around deeper and found this in Project Properties --> Java Build Path --> Source(tab) --> Source folders on build path: [Exclusion section]

**/application.properties

Removing the exclusion fixed the issue and the values were picked up from the application.properties file during startup.

It may be worth noting that running this from the command line (in the directory with the .project file) bypassed the exclusion problem and worked fine.

mvn spring-boot:run


Related Topics



Leave a reply



Submit