2

I'm developing a Spring Boot application and I used application.properties to configure db connections ,server port etc..

# ===============================
# = SERVER CONFIGURATION
# ===============================
server.port=8173

# ===============================
# = DATABASE CONFIGURATION
# ===============================
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springBootApps
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create

# ===============================
# = SPRING CONFIG 
# ===============================
server.error.whitelabel.enabled = false
spring.view.prefix =/WEB-INF/jsp/
spring.view.suffix = .jsp

Can I use an application.properties file instead of spring-configuration.xml or do I need to use both configurations inside my project?

Can I write all my Spring configuration in the application.properties file? (in previous spring versions I did this using springConfiguration file)

As a example how can I implement the following XML configuration in application.properties

<bean id="daoImpl" class="com.mycompany.loginapp.dao.UserDaoImpl"/>

<bean id="data" class="org.springframework.jdbc.core.JdbcTemplate" >
  <property name="dataSource" ref="dataSource" />
</bean>
Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
sasanka
  • 476
  • 5
  • 8

2 Answers2

3

Spring's application.properties are meant for externalizing your properties such as JNDI names, file system paths, etc. This property file is not meant to replace earlier XML based the bean definition and bean wiring.

For bean definitions, you can use either XML based bean definitions or Spring annotations (like @Autowired, @ComponentScan, etc) to get rid of XMLs.

Mohit
  • 1,740
  • 1
  • 15
  • 19
  • Since Spring 3.0, there is a 3rd option for bean definitions, "Java-based configuration" using @Configuration. See http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch03.html for details. – stefan.m Jan 08 '16 at 14:22
1

You are correct, everything can be done within the application.properties.

Full list here docs.spring.io

In some cases if you are overriding Spring Boot's auto configuration capabilities the properties may not work.

code
  • 4,073
  • 3
  • 27
  • 47