I am developing a web application with Spring MVC (4.2.3) and Servlet 3.0 API, so there is no web.xml.
My WebConfig.java is as follows:
...
import javax.servlet.ServletContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {...})
public class WebConfig extends WebMvcConfigurerAdapter {
@Autowired
ServletContext servletContext;
}
I am creating this spring application by copying from a java application with Servlet < 3.0, so there is a web.xml which contains this section regarding the data source:
<resource-ref>
<res-ref-name>jdbc/DefaultDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
</resource-ref>
How do I create such a setting in my Spring MVC application where there is no web.xml?
In the meantime I have had a look at the "Java Servlet Specification Version 3.0". It says about @Resource:
The @Resource annotation is used to declare a reference to a resource such as a data source... This annotation is equivalent to declaring a resource-ref...
@Resource example:
@Resource private javax.sql.DataSource catalogDS;
public getProductsByCategory() {
// get a connection and execute the query
Connection conn = catalogDS.getConnection();
..
}
In the example code above, a servlet, filter, or listener declares a field catalogDS of type javax.sql.DataSource for which the reference to the data source is injected by the container prior to the component being made available to the application. The data source JNDI mapping is inferred from the field name “catalogDS” and type (javax.sql.DataSource). Moreover, the catalogDS resource no longer needs to be defined in the deployment descriptor.
Unfortunately I don't know how to use it and how to get it connected to Springs JDBCTemplate. Is
public class WebConfig extends WebMvcConfigurerAdapter {
the right location at all?