我正在尝试使用junit对spring-boot应用程序进行单元测试.我已将application-test.properties放在src/test/resources下.我有一个ApplicationConfiguration类,它读取application.properties.
我的测试类看起来像这样
@RunWith(SpringRunner.class)
@SpringBootTest(classes=ApplicationConfiguration.class)
@TestPropertySource(locations = "classpath:application-test.properties")
@ActiveProfiles("test")
public class TestBuilders {
@Autowired
private ApplicationConfiguration properties;
Run Code Online (Sandbox Code Playgroud)
当我尝试读取属性时,它始终为null.
我的ApplicationConfiguration类看起来像这样
@Configuration
@ConfigurationProperties
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource(value="file:config.properties", ignoreResourceNotFound =
true)})
public class ApplicationConfiguration{
private xxxxx;
private yyyyy;
Run Code Online (Sandbox Code Playgroud)
我尝试了所有可能的方法,我在谷歌上发现..没有运气.请帮忙!提前致谢.
我正在使用spring boot构建应用程序,需要通过服务而不是sid连接到Oracle DB.
这些是数据源设置:
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@//hostname.companyname.corp:1500/servicename.companyname.com
spring.datasource.username=username
spring.datasource.password=password
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.OracleDialect
spring.jpa.properties.hibernate.default_schema=SCHEMA_NAME
Run Code Online (Sandbox Code Playgroud)
我已经探讨了关于tns连接的另外两个响应: 通过TNS连接Oracle无法正常工作 以及 如何将JDBC连接到tns oracle
但是我遇到了一个不同的错误:
org.springframework.jdbc.support.MetaDataAccessException:无法获取用于提取元数据的Connection; 嵌套异常是org.springframework.jdbc.CannotGetJdbcConnectionException:无法获取JDBC连接; 嵌套异常是org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:339)〜[spring-jdbc-4.3.13.RELEASE.jar:4.3.13.RELEASE]中的java.sql.SQLException. springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:366)〜[弹簧JDBC-4.3.13.RELEASE.jar:4.3.13.RELEASE]在org.springframework.boot.autoconfigure.orm.jpa.DatabaseLookup .getDatabase(DatabaseLookup.java:72)〜[spring-boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE]在org.springframework.boot.autoconfigure.orm.jpa.JpaProperties.determineDatabase(JpaProperties. java:139)[org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.jpaVendorAdapter(JpaBaseConfiguration.java:105)[spring] -boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE] at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration $$ EnhancerBySpringCGLIB $$ 47d988e9.CGLIB $ jpaVendorAdapter $ 4()[spring-boot -autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE] at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration $$ EnhancerBySpringCGLIB $$ 47d988e9 $$ FastClassBySpringCGLIB $$ 27b4f4ca.invoke()[spring-boot-autoconfigure-1.5.9 .RELEASE.jar:1.5.9.RELEASE]在org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE] at at org.springframework.context.annotation.ConfigurationClassEnhancer $ BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358)[弹簧上下文4.3.13.RELEASE.jar:4.3.13.RELEASE]在org.springframework.boot.autoconfigure.orm. jpa.HibernateJpaAutoConfiguration $$ EnhancerBySpringCGLIB $$ 47d988e9.jpaVendorAdapter()[spring-boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)〜[na: 1.8.0_152] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)〜[na:1.8.0_152] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)〜[na:1.8.0_152] at jav org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)中的a.lang.reflect.Method.invoke(未知来源)〜[na:1.8.0_152] [spring-beans-4.3.13 .RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE ] org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173)org.springframework.beans.factory中的[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]. support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067)[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java: 513)在组织中的[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE].springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] org.springframework.beans.factory.support.AbstractBeanFactory $ 1 …
在我的 Spring Boot 应用程序中,我有以下控制器
@RestController(value = "ProjectController")
@CrossOrigin(origins = {"${app.api.settings.cross-origin.urls}"})
public class ProjectController {
// some request mapping methods
}
Run Code Online (Sandbox Code Playgroud)
该属性已经是一个键,在application.properties文件app.api.settings.cross-origin.urls中具有逗号分隔的有效 url,例如
app.api.settings.cross-origin.urls=http://localhost:3000, http://localhost:7070
这种方法一直有效,直到我只有单个值,例如
app.api.settings.cross-origin.urls=http://localhost:3000但不适用于逗号分隔值。origins里面的字段的@CrossOrigin类型String[]仍然不会String[]自动转换。我的意思是框架应该提供某种方法来实现这一点。不是解决办法。我可以使用属性文件中的逗号分隔的 url 来实现,使用@Value到List<String>或String[]作为类内的字段,@Configuration如下所示
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Value("${app.api.settings.cross-origin.urls}")
private String[] consumerUiOrigins;
@Override
public void addCorsMappings(CorsRegistry registry) {
registry
.addMapping("/api/**")
.allowedOrigins(consumerUiOrigins);
}
}
Run Code Online (Sandbox Code Playgroud)
但这将是具有应用程序广泛适用性的全局配置。我想坚持使用更细粒度的@CrossOrigin基于注释的 CORS 配置。
所以我在下面清楚地提出了我的问题。
是否可以使用属性plcaholer表达式()将属性文件中的逗号分隔值注入具有相同类型的spring注释字段,即?????? …
spring spring-boot spring-web property-placeholder application.properties
我正在尝试从 ldap-TEST.properties 文件中读取 ldap 属性并尝试将其绑定到一个 java 配置类。因为我已经指定了 @PropertSource 并为 propertysourcesplaceholderconfigurer 定义了一个静态 Bean。我仍然得到无法解析占位符 'spring.profiles.active' 在价值“classpath:/ldap-${spring.profiles.active}.properties”下面是项目文件请帮助我
@Configuration
@PropertySource("classpath:/ldap-${spring.profiles.active}.properties")
public class LdapConfig {
@Autowired
Environment env;
@Bean
public LdapContextSource contextSource() {
LdapContextSource contextSource = new LdapContextSource();
contextSource.setUrl(env.getRequiredProperty("ldap.url"));
contextSource.setBase(env.getRequiredProperty("ldap.base"));
contextSource.setUserDn(env.getRequiredProperty("ldap.userDn"));
contextSource.setPassword(env.getRequiredProperty("ldap.password"));
contextSource.afterPropertiesSet();
return contextSource;
}
@Bean
public LdapTemplate ldapTemplate() {
return new LdapTemplate(contextSource());
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
Run Code Online (Sandbox Code Playgroud)
}
//ldap-TEST.properties file
ldap.base=dc=example,dc=com
ldap.password=password
ldap.port=839
ldap.userDn=cn=read-only-admin,dc=example,dc=com
ldap.url=ldap://ldap.forumsys.com:389
Run Code Online (Sandbox Code Playgroud)
我的主要应用
@SpringBootApplication
public class Application {
public static void main(String[] args) { …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 Spring Boot 应用程序连接到 PostGresSql 数据库。但每次我都会error in setting up SSL connection出错。
我的application.properties文件如下:-
server.port=8443
spring.datasource.url=jdbc:postgresql://localhost:5432/testdb
spring.datasource.username=postgres
spring.datasource.password=1234
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.dbcp2.test-while-idle=true
server.ssl.key-alias=selfsigned_localhost_sslserver
server.ssl.key-password=changeit
server.ssl.key-store=ssl-server.jks
server.ssl.key-store-provider=SUN
server.ssl.key-store-type=JKS
Run Code Online (Sandbox Code Playgroud)
ssl-server.jks 文件位于 src/main/resources 文件夹中。我正在添加下面的控制台消息:-
2018-09-17 09:08:16.161 INFO 6808 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-09-17 09:08:16.289 INFO 6808 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2018-09-17 09:08:17.348 ERROR 6808 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
org.postgresql.util.PSQLException: An error occurred while setting up the SSL …Run Code Online (Sandbox Code Playgroud) 我们如何将 YAML 文件转换为Application.properties?
pay.payment:
sandbox:
Url: https://securegw-stage//processTransaction
callbackUrl: http://localhost:8080/pgresponse
details:
CHANNEL_ID: '${pay.payment.sandbox.channelid}'
INDUSTRY_TYPE_ID: '${pay.payment.sandbox.industrytypeid}'
CALLBACK_URL: '${pay.payment.sandbox.callbackUrl}'
Run Code Online (Sandbox Code Playgroud)
是否有任何工具可以在线将 YAML 转换为 app.properties?
我有这个多模块项目:
Parent Pom
|
|----Main module (@SpringBootApplcation and application.properties)
|
|----Module I
|
|----Module II
|
|----Module III
Run Code Online (Sandbox Code Playgroud)
主模块依赖于其他模块,并且包含messages.properties和messages_fr.propertiesunderresources/messages/以及application.properties我定义MessageResourcebean 的位置spring.messages.basename=messages/messages。现在我想将messages.properties文件放入每个模块中。例如,对于模块 I,src/main/resources/messages/messages.properties模块 II 的内容相同。我的问题是,如何在MessageResource定义的 bean中添加模块的消息application.properties?我尝试了几个这样的声明:
spring.messages.basename=messages/messages,classpath:/com/company/moduleI/resources/messages/messages或者spring.messages.basename=messages/messages,classpath:moduleI/resources/messages/messages但没有一个有效。有办法实现这一点吗?
spring internationalization spring-boot application.properties
我正在使用git deploy在Heroku上托管Spring Boot应用程序。我有一个公共的git存储库,并将我的application.properties文件添加到.gitignore中,因为它包含敏感细节,例如api密钥和数据库凭据。
处理这种情况的最佳方法是什么,我是否可以将单个application.properties文件直接直接上传到Heroku?我不想使用Heroku配置变量,因为我宁愿保持平台独立性。
heroku configuration-files spring-boot 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.year为2003
我尝试使用 split 来分割,${test.product.release.version}.split('.')[0]但是当我在控制器中获取该属性时,我仍然获得如下值2003.1.split('.')[0]
我怎样才能将其作为2003唯一?
我已经创建了 .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
spring-boot ×10
spring ×5
java ×2
database ×1
datasource ×1
heroku ×1
maven ×1
pom.xml ×1
spring-web ×1
ssl ×1