Application.properties 使用 Spring Boot 上传文件
Application.properties file unload with SpringBoot
我实际上使用 spring 作为后端服务器。这一切的配置我也是用SpringBoot
我现在还需要将此服务器连接到本地数据库。所以我使用 application.properties 的文件。此文件位于 src/main/resources
#Datasource
spring.datasource.url: jdbc:postgresql://localhost:5432/base_temp
spring.datasource.username: postgres
spring.datasource.password: password
spring.datasource.driverClassName: org.postgresql.Driver
#Jpa
jpa.database: POSTGRESQL
jpa.show-sql: true
jpa.hibernate.ddl-auto: create
jpa.hibernate.dialect: PostgreSQLDialect
jpa.hibernate.namingStrategy: org.hibernate.cfg.ImprovedNamingStrategy
无论我在 spring.datasource.password 中输入什么,我在控制台中都没有任何错误,所以我猜文件没有加载。
为了解决这个问题,我尝试将其包含在我的 pom.xml
中
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>application.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
我也尝试过@PropertySource 或自己创建数据源的@Bean :
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://localhost:5432/base_temp");
dataSource.setUsername("postgres");
dataSource.setPassword("password");
return dataSource;
}
但在每种情况下,该后端都能够从数据库中获取数据。我真的不知道如何使用 SpringBoot 并且知道,我并非无法看到我的配置有什么问题。
还有我如何启动这个:
/**
* This is the Spring-Boot application launcher
*
*/
@Configuration
@EnableAutoConfiguration
@EnableJpaRepositories
@ComponentScan(basePackages = { "XXX.be" })
@EntityScan(basePackages = { "XXX.be.model" })
public class Launcher extends SpringBootServletInitializer {
public static void main(String[] args) throws Exception {
SpringApplication.run(Launcher.class, args);
}
@Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder application) {
return application.sources(Launcher.class);
}
@Bean
public ServletRegistrationBean jerseyServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(
new ServletContainer(), "/pige/*");
registration.addInitParameter(
ServletProperties.JAXRS_APPLICATION_CLASS,
ApplicationConfiguration.class.getName());
return registration;
}
@Bean
public EmbeddedServletContainerFactory containerFactory() {
final JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory = new JettyEmbeddedServletContainerFactory() {
@Override
protected JettyEmbeddedServletContainer getJettyEmbeddedServletContainer(
Server server) {
return new JettyEmbeddedServletContainer(server);
}
};
jettyEmbeddedServletContainerFactory
.addServerCustomizers(new JettyConfigurer());
return jettyEmbeddedServletContainerFactory;
}
@Bean
public DozerBeanMapper dozerBean() {
DozerBeanMapper dozerBean = new DozerBeanMapper();
return dozerBean;
}
}
最后,这是 pom.xml
中 2 个工件之间的简单兼容性错误
我实际上使用 spring 作为后端服务器。这一切的配置我也是用SpringBoot
我现在还需要将此服务器连接到本地数据库。所以我使用 application.properties 的文件。此文件位于 src/main/resources
#Datasource
spring.datasource.url: jdbc:postgresql://localhost:5432/base_temp
spring.datasource.username: postgres
spring.datasource.password: password
spring.datasource.driverClassName: org.postgresql.Driver
#Jpa
jpa.database: POSTGRESQL
jpa.show-sql: true
jpa.hibernate.ddl-auto: create
jpa.hibernate.dialect: PostgreSQLDialect
jpa.hibernate.namingStrategy: org.hibernate.cfg.ImprovedNamingStrategy
无论我在 spring.datasource.password 中输入什么,我在控制台中都没有任何错误,所以我猜文件没有加载。
为了解决这个问题,我尝试将其包含在我的 pom.xml
中 <resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>application.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
我也尝试过@PropertySource 或自己创建数据源的@Bean :
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://localhost:5432/base_temp");
dataSource.setUsername("postgres");
dataSource.setPassword("password");
return dataSource;
}
但在每种情况下,该后端都能够从数据库中获取数据。我真的不知道如何使用 SpringBoot 并且知道,我并非无法看到我的配置有什么问题。
还有我如何启动这个:
/**
* This is the Spring-Boot application launcher
*
*/
@Configuration
@EnableAutoConfiguration
@EnableJpaRepositories
@ComponentScan(basePackages = { "XXX.be" })
@EntityScan(basePackages = { "XXX.be.model" })
public class Launcher extends SpringBootServletInitializer {
public static void main(String[] args) throws Exception {
SpringApplication.run(Launcher.class, args);
}
@Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder application) {
return application.sources(Launcher.class);
}
@Bean
public ServletRegistrationBean jerseyServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(
new ServletContainer(), "/pige/*");
registration.addInitParameter(
ServletProperties.JAXRS_APPLICATION_CLASS,
ApplicationConfiguration.class.getName());
return registration;
}
@Bean
public EmbeddedServletContainerFactory containerFactory() {
final JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory = new JettyEmbeddedServletContainerFactory() {
@Override
protected JettyEmbeddedServletContainer getJettyEmbeddedServletContainer(
Server server) {
return new JettyEmbeddedServletContainer(server);
}
};
jettyEmbeddedServletContainerFactory
.addServerCustomizers(new JettyConfigurer());
return jettyEmbeddedServletContainerFactory;
}
@Bean
public DozerBeanMapper dozerBean() {
DozerBeanMapper dozerBean = new DozerBeanMapper();
return dozerBean;
}
}
最后,这是 pom.xml
中 2 个工件之间的简单兼容性错误