Spring Boot Application not reading application.pr

2019-04-17 23:56发布

UPDATE:

I realized a couple of things now. My application.properties file is being loaded properly because I verified via the /env path (thanks Dave) that my DB properties are being loaded. The problem appears to be that when I run it using the Spring Boot maven plug-in,it fails to initialize my dataSource.

mvn spring-boot:run

This then causes my application to blow-up with errors because other beans can't get initialized. The odd thing is it runs fine from Eclipse.

I have a class called DataService that extends JdbcTemplate. In my DataService constructor, I inject the Datasource.

@Component
public class DataService extends JdbcTemplate  {

    @Autowired
    public DataService(DataSource dataSource){
        super(dataSource);
    }
    ...more custom methods
}

I use this DataService class in other beans to perform DB operations. My DataSource is defined in my application.properties file

spring.datasource.url: jdbc:h2:tcp://localhost/~/testdb2
spring.datasource.driverClassName: org.h2.Driver

This is my Application.java class

@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableWebMvcSecurity
@EnableAsync
@EnableScheduling
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }    
}

I first realized this when I was attempting to run jUnit tests from Maven using

mavent test

I thought it just had to do with how it was executing the junit test cases however it is also occurring when I simple try to run the application using maven.

My JUnit4 test class is defined as follows:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes={Application.class})
@WebAppConfiguration
public class QuestionRepositoryIntegrationTests {
     ...methods
}

I used the example from the Spring Boot how-to docs (http://projects.spring.io/spring-boot/docs/docs/howto.html)

When I run this JUnit class from Eclipse, it works just fine. When it executes from maven, it starts to act up as I described above.

5条回答
姐就是有狂的资本
2楼-- · 2019-04-18 00:18

Just add the following statement;

@TestPropertySource("classpath:application.properties")

To your test class. I am assuming you have your application.properties file under src/test/resources

Here is my working example;

@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource("classpath:application.properties")
public class TestTwitterFeedRoute extends CamelTestSupport {
//...
}
查看更多
别忘想泡老子
3楼-- · 2019-04-18 00:30

This works for me:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestApplication.class, 
    initializers = ConfigFileApplicationContextInitializer.class)
public class SomeTestClass {
    ...
}
查看更多
甜甜的少女心
4楼-- · 2019-04-18 00:34

Make sure your @ConfigurationProperties annotation is set to the same prefix as whatever you're using in your configuration file (application.config)

查看更多
走好不送
5楼-- · 2019-04-18 00:40

Try to define the <resources> tag in the build section in your pom, setting path for resource directory where is application.properties:

<build>
        <resources>
            <resource>
                <directory>resources</directory>
                <targetPath>${project.build.outputDirectory}</targetPath>
                <includes>
                    <include>application.properties</include>
                </includes>
            </resource>
        </resources>
</build>
查看更多
走好不送
6楼-- · 2019-04-18 00:45

You can configure your main datasource as the following, I'm using mysql here. But you can use your own datasource. you can configure the following in your application.properties inside src/main/resources

spring.datasource.url = jdbc:mysql://localhost:3306/dsm
spring.datasource.username = root
spring.datasource.password = admin123
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

to run test inside your application you can either use the same datasource or create application-test.properties inside src/test/resources and its possible to configure test data source over there.

查看更多
登录 后发表回答