【发布时间】:2015-05-23 15:42:29
【问题描述】:
我实际上使用 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;
}
}
【问题讨论】:
-
您的文件在哪里?你如何开始你的应用程序......你是否在你的 maven pom 中配置了
spring-boot-maven-plugin? -
文件在src/main/resrouces,我忘记写了,我编辑一下。我不会忘记我的 pom.xml 中的
srping-boot-maven-plugin。我用这个启动应用程序:SpringApplication.run(Launcher.class, args);进入配置一些@Bean 的 Launcher 类 -
发布启动应用程序的类。
-
我正在编辑帖子,现在已经完成。
标签: spring maven properties spring-boot