我有一个 Spring Boot 2 应用程序,它有两个数据源。两个数据源都可以工作,但是我无法更改最大池大小等属性,我的更改没有生效。
我在 application.properties 文件中配置两个数据源;
spring.datasource.url=jdbc:sqlserver://server;databaseName=ProductionMetrics
spring.datasource.username=ProductionMeusernametricsUser
spring.datasource.password=password
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.hikari.maximum-pool-size=20
# Products
trwbi.datasource.url=jdbc:sqlserver://server;databaseName=TRWBI
trwbi.datasource.username=username
trwbi.datasource.password=password
trwbi.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
trwbi.datasource.hikari.maximum-pool-size=20
Run Code Online (Sandbox Code Playgroud)
然后,我像这样设置两个数据源;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "defaultEntityManagerFactory",
transactionManagerRef = "defaultTransactionManager",
basePackages = {
"com.domain.visualisation.shared.repository"
}
)
public class DefaultDbConfig {
@Primary
@Bean(name = "defaultDataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource defaultDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "defaultEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean
entityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("defaultDataSource") DataSource dataSource
) {
return builder
.dataSource(dataSource)
.packages("com.domain.visualisation.shared.entities")
.persistenceUnit("default")
.build();
}
@Primary
@Bean(name = "defaultTransactionManager")
public …Run Code Online (Sandbox Code Playgroud) 我有一个春季启动应用程序
我@Configuration class加载了xml Configuration using @ImportResource("path/to/xml"),其中包含以下行
<property name="bla" value="${log.directory}/file.ext" />
Run Code Online (Sandbox Code Playgroud)
在src/main/resources我有application.properties以下内容的文件:
log.directory=C:/path/I/Need
Run Code Online (Sandbox Code Playgroud)
但是当我运行时它无法加载属性,如下所示:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'log.directory' in string value "${log.directory}/file.ext"
目前我有一个Spring xml配置(Spring 4),它加载了一个属性文件.
context.properties
my.app.service = myService
my.app.other = ${my.app.service}/sample
Run Code Online (Sandbox Code Playgroud)
Spring xml配置
<bean id="contextProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="ignoreResourceNotFound" value="true" />
<property name="fileEncoding" value="UTF-8" />
<property name="locations">
<list>
<value>classpath:context.properties</value>
</list>
</property>
</bean>
<bean id="placeholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="properties" ref="contextProperties" />
<property name="nullValue" value="@null" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
</bean>
Run Code Online (Sandbox Code Playgroud)
使用属性的Bean
@Component
public class MyComponent {
@Value("${my.app.other}")
private String others;
}
Run Code Online (Sandbox Code Playgroud)
这是完美的,others价值是MyService/sample,例外.但是当我尝试用JavaConfig替换这个配置时@Value,我的组件中的工作方式不同.值不myService/sample但是${my.app.service}/sample.
@Configuration
@PropertySource(name="contextProperties", ignoreResourceNotFound=true, value={"classpath:context.properties"})
public class PropertiesConfiguration { …Run Code Online (Sandbox Code Playgroud) 我正在使用 Spring Boot 并具有以下组件类:
@Component
@ConfigurationProperties(prefix="file")
public class FileManager {
private Path localDirectory;
public void setLocalDirectory(File localDirectory) {
this.localDirectory = localDirectory.toPath();
}
...
}
Run Code Online (Sandbox Code Playgroud)
以及以下 yaml 属性文件:
file:
localDirectory: /var/data/test
Run Code Online (Sandbox Code Playgroud)
我想通过替换为 java.nio.file.Path 来删除 java.io.File(setLocalDirectory)的引用。但是,执行此操作时收到绑定错误。有没有办法将属性绑定到路径(例如,通过使用注释)?
我正在使用 spring boot 和 spring jdbc 模板。我想在属性或 yml 文件中外部化 SQL 查询。我不想将 SQL 查询存储在 java 存储库类中。
处理这种情况的最佳方法是什么?
这就是我的存储库类现在的样子。
@Repository
public class UserRepositoryImpl extends BaseRepository implements UserRepository {
@Override
public List<User> findAll(){
String sqlQuery = "SELECT * FROM users";
return jdbcTemplate.query(sqlQuery, userMapper);
}
@Override
public User findById(Long userId){
String sqlQuery = "SELECT * FROM users WHERE id = :userId";
Map<String, String> namedParameters = new HashMap<String, String>();
namedParameters.put("userId", String.valueOf(userId));
return jdbcTemplate.queryForObject(sqlQuery, namedParameters, userMapper);
}
Run Code Online (Sandbox Code Playgroud) 在我当前的项目中,我们使用字符串的属性文件。然后使用 MessageFormat 对这些字符串进行“格式化”。不幸的是,MessagFormat 处理了单引号,这在使用大量撇号的语言(例如法语)中有点障碍。
例如,假设我们有这个条目
login.userUnknown=User {0} does not exist
Run Code Online (Sandbox Code Playgroud)
当这被翻译成法语时,我们得到:
login.userUnknown=L'utilisateur {0} n'existe pas
Run Code Online (Sandbox Code Playgroud)
这个,MessageFormat 不喜欢...
而我,不喜欢以下内容,即必须使用双引号:
login.userUnknown=L''utilisateur {0} n''existe pas
Run Code Online (Sandbox Code Playgroud)
我不喜欢它的原因是它到处都会导致拼写检查错误。
问题:我正在寻找以下说明的替代方法,即不需要双引号但仍使用位置占位符({0}、{1}...)的替代方法。还有什么我可以使用的吗?
MessageFormat.format(Messages.getString("login.userUnkown"), username);
Run Code Online (Sandbox Code Playgroud) 我将一个Spring Boot应用程序作为.jar文件运行,该文件部分从application.yml中获取其属性,这些属性位于jar中,而另一部分属性是从另一个驻留在jar外部的application.yml提供的.外部的一些属性会从内部覆盖属性.为了测试属性是否被正确覆盖,我希望看到当前活动的属性.开箱即用可以实现吗?或者是通过属性输出逻辑扩展我的应用程序的唯一解决方案?
我已经使用嵌入式Tomcat + Thymeleaf模板引擎使用Spring Initializr生成了Spring Boot Web应用程序。
我将此属性放在了application.properties中
default.to.address=nunito.calzada@gmail.com
Run Code Online (Sandbox Code Playgroud)
我正在使用Spring Tool Suite版本:3.8.4.RELEASE作为开发环境,但在编辑器中收到此警告 'default.to.address' is an unknown property.
我应该将此属性放在另一个属性文件中吗?
我写了一个小应用程序.我已将数据库特定信息放在属性文件中.
db.url=jdbc:mysql://localhost:3306/librarydb
db.user=root
db.passwd=pas$w0rd
Run Code Online (Sandbox Code Playgroud)
当我构建应用程序以获取可执行jar文件时,属性文件包含在其中.将这些信息放在属性文件中的全部目的是允许用户更改这些信息,但这使得它变得不可能.
如何包含不包含jar文件的属性文件?有点像你在.NET应用程序中使用AppSettings文件.
我是Java的新手,所以如果你能把它分解为一系列步骤我会很感激.
谢谢.
编辑:
这就是我从程序中的属性文件中获取值的方法.
public class DatabaseHandler {
public static Connection connectToDb() {
Connection con = null;
Properties props = new Properties();
InputStream in = null;
try {
in = DatabaseHandler.class.getResourceAsStream("database.properties");
props.load(in);
in.close();
String url = props.getProperty("db.url");
String user = props.getProperty("db.user");
String password = props.getProperty("db.passwd");
con = DriverManager.getConnection(url, user, password);
} catch (Exception ex) {
Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
}
return con;
}
}
Run Code Online (Sandbox Code Playgroud) 我想覆盖我在Quarkus应用程序的配置文件中配置的属性。
我怎样才能做到这一点?
properties-file ×10
java ×6
spring ×5
spring-boot ×5
database ×1
hikaricp ×1
jdbc ×1
localization ×1
quarkus ×1
spring-mvc ×1
sql ×1
xml ×1