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.

KiriSakow
  • 957
  • 1
  • 12
  • 22
John F.
  • 4,780
  • 5
  • 28
  • 40
  • 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)? – Dave Syer Feb 27 '14 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. – John F. Feb 28 '14 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. – Dave Syer Feb 28 '14 at 13:06
  • @DaveSyer Thanks for your help. So upon further inspection, I was mistaken in thinking that the properties file wasn't being read. I enabled the Spring 'banner' and noticed it appeared when the maven test was running. So it looks like it was ignoring my spring.datasource properties. So, what I tried next was create a DataSource bean instead and that worked properly. I think I may stick with the `@Bean` approach for the Datasource. Is there a 'best practice' in terms of defining items in .props file vs defining a bean? – John F. Feb 28 '14 at 14:35
  • 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). – Dave Syer Feb 28 '14 at 16:05
  • I added the actuator as a dependency. This /env feature is really nice. Thanks for that. So I ran the test case using `mvn test` and mocked an endpoint and had it print out the response for /env. And the spring.datasource properties are present. So now I don't know. I'm going to come up with some simple examples to show you. I have to be doing something wrong. – John F. Feb 28 '14 at 17:11
  • I added the tomcat-jdbc jar to the POM and everything started to work... I'm confused. Does that make sense? – John F. Mar 02 '14 at 13:58
  • Maybe. If you don't have a pooled data source implementation on the classpath but you do have h2 or hsql, then their native data sources will be used instead. If tomcat jdbc is there it is preferred, so that would be a forcing function. – Dave Syer Mar 02 '14 at 15:30
  • 1
    Just to confirm your application.properties are in the correct location ? src/main/resources and src/test/resources ?? – Tiarê Balbi Oct 09 '14 at 19:34

7 Answers7

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>
peterh
  • 11,875
  • 18
  • 85
  • 108
Antonino Barila
  • 163
  • 1
  • 8
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.

KiriSakow
  • 957
  • 1
  • 12
  • 22
nijogeorgep
  • 762
  • 12
  • 20
  • 3
    If you name the file "application.properties" and put it in src/test/resources it will be automatically picked up by spring – alextsil Apr 16 '16 at 12:15
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 {
//...
}
Levent Divilioglu
  • 11,198
  • 5
  • 59
  • 106
1

This works for me:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestApplication.class, 
    initializers = ConfigFileApplicationContextInitializer.class)
public class SomeTestClass {
    ...
}
luboskrnac
  • 23,973
  • 10
  • 81
  • 92
  • due to updates @SpringApplicationConfiguration... should be replaced by : ```@SpringBootTest(classes = {TestApplication.class, ConfigFileApplicationContextInitializer.class})``` – Sasha Bond Apr 27 '22 at 17:45
0

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

obesechicken13
  • 795
  • 1
  • 12
  • 24
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.

Vishnu Dahatonde
  • 179
  • 2
  • 13
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>