I am writing a web service using Spring and JAX-RS, and I'm kinda confused with the following
Here's my service exmaple
@Path("/users")
public class UserService {
@GET
@Path("{id}")
@Produces("application/xml")
public User getById(@PathParam("id") int id) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserDAO userDAO = (UserDAO) context.getBean("userDao");
return userDAO.getById(id);
}
}
And here's my beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Initialization for data source -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/hospital-system"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="userDao" class="dao.UserDAO">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
I wonder if this is the right technique to load Application context each time the resource get's called, and if no, how should I change it?