29

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 simply 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 (https://docs.spring.io/spring-boot/docs/current/reference/html/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.

9
  • 1
    What's the symptom of it not working exactly? How many application.properties do you have on the classpath (maybe it's reading another one)? Also, I assume you would have set spring.datasource.hibernate.ddl-auto=none (otherwise using tcp for an H" database could lead to grief)? Commented Feb 27, 2014 at 22:05
  • @DaveSyer I'm not using hibernate. I realize my class name is misleading (Not using the Spring JPA repo classes). I'm using JdbcTemplate. I only have one application.properties file in my src/main/resources path. When my test cases run in eclipse, it reads the .properties file successfully and I can query my "questions" table. However, when Maven runs my test case, it says it can't find the "questions" table and I assume that's because it's not reading my datasource properties and it is defaulting to automatically creating an embedded DB since I have the H2 jar in my path. Commented Feb 28, 2014 at 12:03
  • 1
    Can you try a snapshot? There was a bug with application.properties resolution that we fixed since RC3 (it doesn't seem relevant, but it might be). Otherwise, if you can share your complete project (or a simple project with the same problem) and a nice README to tell me how to create the database, I can take a look. Commented Feb 28, 2014 at 13:06
  • 1
    I would stick with the defaults unless you need something that the default DataSource can't do (it works for me in production). So we still need to try and understand why it wasn't working for you. The banner has nothing to do with properties files. The best thing you can do might be to add the actuator as a dependency (if you haven't already) and look at the /env endpoint (where you will see all the environment properties and their sources). Commented Feb 28, 2014 at 16:05
  • 1
    Just to confirm your application.properties are in the correct location ? src/main/resources and src/test/resources ?? Commented Oct 9, 2014 at 19:34

7 Answers 7

7

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>
Sign up to request clarification or add additional context in comments.

3 Comments

Yup, this worked for me. I have no idea why it was not picking them up automatically.
changed <directory>resources</directory> to <directory>src/main/resources</directory>
It didn't work on my project :') would you like to checkout it out github.com/black-lotus/study-spring-netty @AlexBeardsley
5

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 it's possible to configure test data source over there.

1 Comment

If you name the file "application.properties" and put it in src/test/resources it will be automatically picked up by spring
2

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 {
//...
}

Comments

1

This works for me:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestApplication.class, 
    initializers = ConfigFileApplicationContextInitializer.class)
public class SomeTestClass {
    ...
}

1 Comment

due to updates @SpringApplicationConfiguration... should be replaced by : @SpringBootTest(classes = {TestApplication.class, ConfigFileApplicationContextInitializer.class})
0

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

Comments

0

If you're using eclipse, its always a good idea to check the projects build resources. (Rclick project->properties->build path) I was not getting my application.properties file picked up and turned out I simply missed adding it to build resources.

Comments

0

I'm trying to load all properties files form outside jar, below entry in pom works perfect, you can also exclude or include files depends upon requirements.

  <build>
        <resources>
            <resource>
                 <directory>config</directory>
                 <includes>FILENAME</includes>
                 <excludes>FILENAME</excludes>
            </resource>
        </resources>
    </build> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.