我有一个 Spring Boot 应用程序。当我单击 SpringToolSuit4 中的“运行”按钮时,它可以正常工作。
但是当我以“maven install”运行时,我收到了错误消息:
<error message="Failed to load ApplicationContext" type="java.lang.IllegalStateException">java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'envVersionNum' in value "${envVersionNum}"
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'envVersionNum' in value "${envVersionNum}"
</error>
我的应用程序属性的内容:
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.jpa.show-sql=true
spring.thymeleaf.cache=false
version=${envVersionNum}
如果我把它改成
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.jpa.show-sql=true
spring.thymeleaf.cache=false
version=1.1.1
它工作正常。但我想从用户那里获取 envVersionNum,它存储在 .bash_profile (export envVersionNum= 1.1.1) 中。
pom.xml 的内容:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jrp</groupId>
<artifactId>project-management</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>project-management</name>
<description>project management application</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
homeController的代码:
package com.jrp.pma.controllers;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jrp.pma.dao.EmployeeRepository;
import com.jrp.pma.dao.ProjectRepository;
import com.jrp.pma.dto.ChartData;
import com.jrp.pma.dto.EmployeeProject;
import com.jrp.pma.entities.Project;
@Controller
public class HomeController {
@Value("${version}")
private String ver;
@Autowired
ProjectRepository proRepo;
@Autowired
EmployeeRepository empRepo;
@GetMapping("/")
public String displayHome(Model model) throws JsonProcessingException {
model.addAttribute("versionNumber", ver);
Map<String, Object> map = new HashMap<>();
// we are querying the database for projects
List<Project> projects = proRepo.findAll();
model.addAttribute("projectsList", projects);
List<ChartData> projectData = proRepo.getProjectStatus();
// Lets convert projectData object into a json structure for use in javascript
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = objectMapper.writeValueAsString(projectData);
//[["NOTSTARTED", 1], ["INPROGRESS", 2], ["COMPLETED", 1]]
model.addAttribute("projectStatusCnt", jsonString);
// we are querying the database for employees
List<EmployeeProject> employeesProjectCnt = empRepo.employeeProjects();
model.addAttribute("employeesListProjectsCnt", employeesProjectCnt);
return "main/home";
}
}
任何建议都欣然接受。
最佳答案
如果您在导出命令中设置任何环境变量,那么它只能通过命令行才能看到。
如果您使用的是 Eclipse 或任何其他 IDE,则它不会从导出命令中获取值,因为导出的范围在终端内。
尝试在命令行中导出变量后运行您的 jar,它将起作用。
关于java - Spring Boot 应用程序 : Could not resolve placeholder in application. 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59231276/