我已经使用基于xml的配置做了以下事情,当我开始迁移到java配置时,我遇到了这个问题。它涉及到MS Access的Jdbc连接。我在属性文件中有jdbc配置并从工厂文件中读取它。
请帮助我,
**Web.xml**
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.jdbc.config</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>DispatcherConfiguration</servlet-name>
<!-- dispatcher will map -servlet and look for dispatcher-servlet.xml file
inside WEB-INF -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<!-- No explicit configuration file reference here: everything is configured in the root container for simplicity -->
<param-name>contextConfigLocation</param-name>
<param-value>com.jdbc.config</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherConfiguration</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
**My Dispatcher Config java file**
package org.jdbc.config;
import javax.sql.DataSource;
@Configuration
@EnableWebMvc
@ComponentScan( basePackages= {
"org.jdbc.controller",
"org.jdbc.config",
"org.jdbc.service",
} )
public class DispatcherConfiguration {
@Bean
public static DataSource getDataSource(){
DriverManagerDataSource source = new DriverManagerDataSource();
source.setDriverClassName(getConnectionFactory().getDriver());
source.setUrl(getConnectionFactory().getDbURL());
return source;
}
@Bean
public static ConnectionFactory getConnectionFactory(){
return new ConnectionFactory();
}
}
**Controller Class:**
@Controller
@RequestMapping(value = "/users")
public class MainController {
@Autowired
private IUserDAO userDAO;
//insert
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public Person create(@RequestBody Person person) {
/* LOGGER.info("Creating new user {}", user); */
userDAO.insertUser(person.getFirstName());
return person;
}
@RequestMapping(value = "/{userId}", method = RequestMethod.GET)
@ResponseBody
@Produces("application/json")
public Person read(@PathVariable(value = "userId") int userId) {
// LOGGER.info("Reading user with id {}", userId);
Person person = userDAO.getUser(userId);
Validate.isTrue(person != null, "Unable to find user with id: " + userId);
return person;
}
//create table
@RequestMapping(value="{/tName}",method = RequestMethod.GET)
@ResponseBody
public void createTable (@PathVariable(value = "tName") String tName) {
System.out.println("Coming inot the picture@@@@@@@@@@@@@@@@@");
userDAO.createTable();
}
}