标签: application.properties

在 spring boot 中从 application.properties 获取属性

我想使用 @Value 注释从 spring boot web 项目中的 application.properties 获取值。我以为我可以只使用 @Value 注释,因为我认为 application.properties 中的变量只是即时加载的。但是当我只写 @Value 注释时,它不起作用。

我在 application.poperties 中有一个属性

google.recaptcha.site-key=???
Run Code Online (Sandbox Code Playgroud)

我想加载这个值,所以我编码如下。

@Controller
@RequestMapping("/member")
public class MemberController extends BaseController{

    @Value("#{google.recaptcha.site-key}")
    public String recaptchaSiteKey; 

}
Run Code Online (Sandbox Code Playgroud)

但是当我编译代码时 eclipse 返回错误。

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'google' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:226) ~[spring-expression-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:94) ~[spring-expression-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:81) ~[spring-expression-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:51) ~[spring-expression-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:87) ~[spring-expression-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.expression.spel.ast.OpMinus.getValueInternal(OpMinus.java:98) ~[spring-expression-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:121) ~[spring-expression-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:262) ~[spring-expression-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at …
Run Code Online (Sandbox Code Playgroud)

spring-boot application.properties

4
推荐指数
1
解决办法
5514
查看次数

关于 spring.http.multipart.max-file-size 与 spring.servlet.multipart.max-file-size 的混淆

我花了几天时间让 Spring Boot Upload 文件工作,但是,和 Spring 一样,你不知道魔法是如何工作的,即使在使用这个框架多年之后 - 你必须用谷歌搜索大量时间来解开哪里出了问题并解决诸如穿越迷宫之类的事情,这是可维护性的噩梦。

使用 Spring Boot 2.2.0.M3 进行文件上传 2 对设置之间有什么区别?哪个是对的 ?

spring.http.multipart.max-file-size=-1
spring.http.multipart.max-request-size=-1
Run Code Online (Sandbox Code Playgroud)

上面的“http”是否与 Spring REST 控制器方法一起使用,就像这样... @GetMapping("/files/{filename:.+}") @ResponseBody public ModelAndView yourMethod(.....) 或者这是不需要的完全是一个完整的红鲱鱼,它是下面的设置,它为 REST http 或 Servlet 请求的大于默认值 1MB 的文件完成所有工作。

spring.servlet.multipart.max-file-size=-1
spring.servlet.multipart.max-request-size=-1
Run Code Online (Sandbox Code Playgroud)

上传异常

超过最大上传大小;嵌套异常是 java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: 字段文件超出其最大允许大小 1048576 字节。

file-upload spring-boot application.properties

4
推荐指数
1
解决办法
4059
查看次数

如何拆分 application.properties 中的属性值

我想拆分 application.properties 定义的属性值并将其用作另一个属性的值。

以下是我在 application.properties 文件中的属性

test.product.release.version=2003.1
test.product.release.year=${test.product.release.version}.split('.')[0]
Run Code Online (Sandbox Code Playgroud)

我希望财产价值test.product.release.year2003

我尝试使用 split 来分割,${test.product.release.version}.split('.')[0]但是当我在控制器中获取该属性时,我仍然获得如下值2003.1.split('.')[0]

我怎样才能将其作为2003唯一?

spring spring-boot application.properties

4
推荐指数
1
解决办法
3079
查看次数

application.properties中的spring boot .env变量

我已经创建了 .env 文件,我现在在其中保存变量,我希望它们在我的 application.properties 中定义,但这不起作用。我需要添加什么来获取变量。

.env 文件

MYSQLDB_USER=root
MYSQLDB_ROOT_PASSWORD=root
Run Code Online (Sandbox Code Playgroud)

应用程序属性

spring.profiles.active = dev
Run Code Online (Sandbox Code Playgroud)

应用程序-dev.properties

# mysql database properties
spring.datasource.url = jdbc:mysql://localhost:3306/testdb?useUnicode=true&serverTimezone=UTC&server&createDatabaseIfNotExist=true
spring.datasource.username = ${MYSQLDB_USER}
spring.datasource.password = ${MYSQLDB_ROOT_PASSWORD}
        
# hibernate properties
spring.jpa.hibernate.ddl-auto = create
spring.jpa.show-sql = true
Run Code Online (Sandbox Code Playgroud)

environment-variables pom.xml maven spring-boot application.properties

4
推荐指数
1
解决办法
9085
查看次数

3
推荐指数
1
解决办法
753
查看次数

Spring Boot 多个端口?

如何在多个端口上运行 Spring Boot Web 应用程序?例如 8080 和 80
我怎样才能做到这一点?
应用程序属性

server.port=8080, 80
Run Code Online (Sandbox Code Playgroud)

port spring spring-boot application.properties

3
推荐指数
1
解决办法
5497
查看次数

如何将 spring.freemarker.template-loader-path 指向依赖项 jar 中的模板?

我有2个项目。一个项目 (A) 包含我所有的核心功能,例如我的实体/服务/daos 等。它还包含许多我希望在我的另一个项目 (B) 中利用和重用的 .ftl 模板。我已经成功地让 (B) 使用了 (A) 中的类,但我在重用 freemarker 模板方面没有运气。

项目 (B) 是一个 Spring Boot 应用程序 (v2.1.3),所以我认为我使用 application.property: 是正确的:spring.freemarker.template-loader-path而不是定义一个新的@Bean.

由于是 spring-boot 应用程序,默认情况下,如果没有此属性,项目将在项目自己的src/main/resources/templates位置查找,但我的目标是让项目 (A) 没有自己的模板,并让我的控制器返回在其中找到的模板项目(B)。

在我的 Maven 依赖项中,层次结构是这样的:

projectA-0.0.1.jar
    templates
        folder1
            exampleA.ftl
        folder2
            exampleB.ftl
Run Code Online (Sandbox Code Playgroud)

目前,我的控制器设置为返回return new ModelAndView("folder1/exampleA")上下文前缀为 src/main/resources/templates/ 的内容

有谁知道我需要给spring.freemarker.template-loader-path属性的值的正确格式,以便指向我的依赖 jar 中的模板而不是本地 src/main/resources/templates?

dependencies freemarker external spring-boot application.properties

3
推荐指数
1
解决办法
4450
查看次数

Spring boot项目如何保存DB用户名和密码

当我尝试为我的 Java 项目创建安全的生产环境时,我应该在哪里保存数据库用户名、密码和 url,以保持安全,目前我正在使用应用程序属性来保存数据库凭据,请建议一些良好且安全的方法来保存数据库凭据。

应用程序属性

# This should be used only for credentials and other local-only config.
spring.datasource.url = jdbc:postgresql://localhost/database
spring.datasource.username = root
spring.datasource.password = root@123
Run Code Online (Sandbox Code Playgroud)

我应该在哪里保存安全和生产 Spring boot 项目

java security spring spring-boot application.properties

3
推荐指数
1
解决办法
1万
查看次数

未找到数据库“C:/data/sample”,并且 IFEXISTS=true,因此我们无法自动创建它 - Spring Boot 中的错误

我创建了一个 spring boot 应用程序来连接 h2 数据库。这样做时,它会引发错误,显示未找到数据库。请帮助我解决我可以实施和解决问题的解决方案。

我在 pom.xml 文件中添加了 com.h2database 依赖项,然后它也给出了错误。

下面是我的 pom.xml 文件和 application.properties 文件

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>
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
    <groupId>com.example</groupId>
    <artifactId>santanderdbproj</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>firstproject</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring …
Run Code Online (Sandbox Code Playgroud)

h2 pom.xml spring-boot microservices application.properties

3
推荐指数
3
解决办法
1万
查看次数

Spring Boot中application.properties文件中以下两条语句有什么区别

Spring Boot中application.properties文件中的以下两条语句有什么区别?

server.port = 8080 和 server.port = ${PORT:8080}

请帮忙解释一下

java spring spring-boot application.properties

3
推荐指数
1
解决办法
78
查看次数