A colleague told me recently he didn’t use Spring for his latest REST project because he couldn’t get the beans defined in a XML configuration file loaded. He was familiar with Spring but had never boot strapped a brand new project. I didn’t realise this could be a problem because I have used Spring MVC for a very long time. He was right. It was not obvious. For example, in the Spring Boot tutorial Building a RESTful Web Service, everything is @Autowired
. In a real application, you might need to define some beans in a XML configuration file. For example, database connection information for the persistence layer.
Using the example from my previous post on Spring Boot. You can use the annotation @ImportResource to load XML configuration files.
@SpringBootApplication @ComponentScan @ImportResource("classpath:spring-config.xml") public class Application extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
Spring will auto scan classes annotated with @Service, @Repository, @Controller and @Component. Because Spring AOP is proxy-based, your DAO classes should implement interfaces. For example,
public interface OrderDao { Order getOrder(int id) throw OrderNotFoundException; }
@Repository public class OrderDaoImpl implements OrderDao { private JdbcTemplate jdbcTemplate; @Autowired public void setMyDataSource(DataSource myDataSource) { this.jdbcTemplate = new JdbcTemplate(myDataSource); } }
For some reason, Spring’s own JdbcDaoSupport class is not autowired enabled. If you choose to extend JdbcDaoSupport, you will need to use XML configuration to set the datasource manually. I prefer to have JdbcTemplate as a member and @Autowired the setter instead.
The datasource is defined in the XML file spring-config.xml. The file is located in src/main/resources in a maven project. (Please use a connection pool in a real application. I’m using BasicDataSource here for simplicity sake).
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/> <property name="url" value="${db.url}"/> <property name="username" value="${db.username}"/> <property name="password" value="${db.password}"/> </bean>
The properties are defined in application.properties, also in src/main/resources.
db.url=jdbc:sqlserver://localhost:1433;DatabaseName=Orders db.username=Luser db.password=Pwd
Note: I’m using Spring Boot 1.3.5