无法使用Spring访问Maven属性 [英] Can't access Maven properties with Spring
问题描述
你好,我是Maven的新手,所以如果我做错了事,我想提前道歉.
我有一个带有Spring的Maven项目,在其中我还可以访问MySQL数据库.
经常无法访问MySQL数据库,因此我使用本地数据库进行测试,通过更改活动配置文件在两者之间进行交换. /p>
我通过将其放入我的POM.xml中来设置配置文件:
<profiles>
<profile>
<id>Local</id>
<dependencies>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.3</version>
<classifier>jdk5</classifier>
</dependency>
</dependencies>
<properties>
<jdbc.url>jdbc:hsqldb:file:databaseName</jdbc.url>
<jdbc.username>a</jdbc.username>
<jdbc.password></jdbc.password>
<jdbc.driver>org.hsqldb.jdbcDriver</jdbc.driver>
</properties>
</profile>
<profile>
<id>MySQL</id>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
</dependencies>
<properties>
<jdbc.url>jdbc:mysql://mysql.website.ac.uk:3306</jdbc.url>
<jdbc.username>user</jdbc.username>
<jdbc.password>1234</jdbc.password>
<jdbc.driver>com.mysql.jdbc.Driver</jdbc.driver>
</properties>
</profile>
</profiles>
当我尝试通过例如$ {jdbc.url}访问属性变量时,它们没有转换为配置文件中定义的实际值,并且出现错误.我怀疑这可能与Spring Boot有关.
您在配置文件中指定了属性,所以有一种解决方法.诸如@Value("${name}")
之类的注释引用的是外部化配置,例如来自application.yml.您应该使用此文件来加载所需的属性. @property_name@
从您的Maven个人资料中加载属性.
jdbc:
url: @jdbc.url@
username: @jdbc.username@
and so on...
然后可以在您的代码中访问这些属性.
Hello I am new to Maven in general so I'd like to apologise in advance if I get things wrong.
I have a Maven project with spring in which I also access a MySQL database.
often the MySQL database is not accessible so I use a local database for testing, I swap between the two by changing the active profile.
I set up the profiles by putting this in my POM.xml:
<profiles>
<profile>
<id>Local</id>
<dependencies>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.3</version>
<classifier>jdk5</classifier>
</dependency>
</dependencies>
<properties>
<jdbc.url>jdbc:hsqldb:file:databaseName</jdbc.url>
<jdbc.username>a</jdbc.username>
<jdbc.password></jdbc.password>
<jdbc.driver>org.hsqldb.jdbcDriver</jdbc.driver>
</properties>
</profile>
<profile>
<id>MySQL</id>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
</dependencies>
<properties>
<jdbc.url>jdbc:mysql://mysql.website.ac.uk:3306</jdbc.url>
<jdbc.username>user</jdbc.username>
<jdbc.password>1234</jdbc.password>
<jdbc.driver>com.mysql.jdbc.Driver</jdbc.driver>
</properties>
</profile>
</profiles>
When I try to access the properties variables through for example ${jdbc.url} they are not converted to the actual values as defined in the profile and I get errors. I suspect it might have something to do with Spring Boot.
You specified properties in profile, so there is a workaround. Annotations such as @Value("${name}")
are referencing to externalized configuration, e.g. from application.yml. You should use this file to load properties you want. @property_name@
loads property from your maven profile.
jdbc:
url: @jdbc.url@
username: @jdbc.username@
and so on...
Then these properties will be accessible in your code.
这篇关于无法使用Spring访问Maven属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!