spring3.1使用JDBC数据源datasource的错误问题
JAR包:
junit-4.10.jar
org.springframework.beans-3.1.1.RELEASE.jar
org.springframework.context-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar
org.springframework.asm-3.1.1.RELEASE.jar
org.springframework.expression-3.1.1.RELEASE.jar
org.springframework.jdbc-3.1.1.RELEASE.jar
commons-logging.jar
mysql-connector-java-5.1.18-bin.jar
1、User类
Java code
package com.model;
public class User {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2、UserDAO接口
Java code
package com.dao;
import com.model.User;
public interface UserDAO {
public void save(User u);
}
3、UserDAOImpl实现类
Java code
package com.dao.impl;
import java.sql.Connection;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.springframework.stereotype.Component;
import com.dao.UserDAO;
import com.model.User;
@Component("userDAO")
public class UserDAOImpl implements UserDAO {
private DataSource dataSource ;
public DataSource getDataSource() {
return dataSource;
}
@Resource(name="dataSource")
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public void save(User u) {
try {
Connection conn = dataSource.getConnection();
conn.createStatement().executeUpdate("insert into user values(null,'lisi')");
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("A user saved!");
}
}
4、UserService类
Java code
package com.service;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.dao.UserDAO;
import com.dao.impl.UserDAOImpl;
import com.model.User;
@Component("userService")
public class UserService {
private UserDAO userDAO = new UserDAOImpl();
public UserDAO getUserDAO() {
return userDAO;
}
@Resource(name="userDAO")
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
public void add(User u) {
userDAO.save(u);
}
}
5、UserServiceTest测试类
Java code
package com.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.model.User;
import com.service.UserService;
public class UserServiceTest {
@Test
public void testAdd() {
User u = new User();
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
UserService userService = (UserService)context.getBean("userService");
userService.add(u);
}
}
[解决办法]
刚才把你的代码放在我的工程里面跑的一下我的sping版本是3.0.5的
一点问题都没有
运行完以后的结果:
2012-6-7 15:36:14 org.springframework.context.support.AbstractApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@14d3343: startup date [Thu Jun 07 15:36:14 CST 2012]; root of context hierarchy2012-6-7 15:36:14 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [beans.xml]2012-6-7 15:36:17 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties信息: Loading properties file from class path resource [jdbc.properties]2012-6-7 15:36:17 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@126d3df: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,userDAO,userService,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource]; root of factory hierarchy2012-6-7 15:36:17 org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName信息: Loaded JDBC driver: com.mysql.jdbc.DriverA user saved!
[解决办法]
应该是jar的不兼容性