我创建了一个spring项目(Spring 4.0.2)。我提出了所需的文件:
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="employeeDAO" class="com.journaldev.spring.jdbc.dao.EmployeeDAOImpl">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="employeeDAOJDBCTemplate" class="com.journaldev.spring.jdbc.dao.EmployeeDAOJDBCTemplateImpl">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://localhost:1433;database=springdb"/>
<property name="username" value="lm" />
<property name="password" value="pp" />
</bean>
</beans>
Employee.java
@Entity
@Table(name="Employee")
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private int id;
@Column(name = "name")
private String name;
@Column(name = "role")
private String role;
// with getters, setters, default constructor, constructor using fields
}
EmployeeDAO.java
public interface EmployeeDAO
{
public List<Employee> getAll();
}
EmployeeDAOImpl.java
public class EmployeeDAOImpl implements EmployeeDAO {
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public List<Employee> getAll() {
String query = "select id, name, role from Employee";
List<Employee> empList = new ArrayList<Employee>();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
con = dataSource.getConnection();
ps = con.prepareStatement(query);
rs = ps.executeQuery();
while(rs.next()){
Employee emp = new Employee();
emp.setId(rs.getInt("id"));
emp.setName(rs.getString("name"));
emp.setRole(rs.getString("role"));
empList.add(emp);
}
}catch(SQLException e){
e.printStackTrace();
}finally{
try {
rs.close();
ps.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return empList;
}
}
EmployeeDAOJDBCTemplateImpl.java
public class EmployeeDAOJDBCTemplateImpl implements EmployeeDAO,Serializable {
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public List<Employee> getAll() {
String query = "select id, name, role from Employee";
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
List<Employee> empList = new ArrayList<Employee>();
List<Map<String,Object>> empRows = jdbcTemplate.queryForList(query);
for(Map<String,Object> empRow : empRows){
Employee emp = new Employee();
emp.setId(Integer.parseInt(String.valueOf(empRow.get("id"))));
emp.setName(String.valueOf(empRow.get("name")));
emp.setRole(String.valueOf(empRow.get("role")));
empList.add(emp);
}
return empList;
}
}
要调用的bean以提取员工列表 DataModelD.java :
@ManagedBean
@ViewScoped
public class DataModelD implements Serializable
{
private List<Employee> list;
private Employee employee;
private EmployeeDAO eDao;
@PostConstruct
public void init()
{
System.out.println("-----------------------------BEGIN List");
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
eDao = ctx.getBean("employeeDAOJDBCTemplate", EmployeeDAO.class);
employee = new Employee();
list = eDao.getAll();
System.out.println("---------------------------------------List size= " + list.size());
}
}
运行后(显示员工列表),将显示以下异常:
08:14:46,222 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "HiSpring.war"
08:15:29,271 INFO [stdout] (http-localhost-127.0.0.1-8080-1) -----------------------------BEGIN List
08:15:29,529 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] (http-localhost-127.0.0.1-8080-1) Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@ca34ed: startup date [Wed Aug 17 08:15:29 GMT-08:00 2016]; root of context hierarchy
08:15:29,591 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] (http-localhost-127.0.0.1-8080-1) Loading XML bean definitions from class path resource [spring.xml]
08:15:29,718 INFO [org.springframework.jdbc.datasource.DriverManagerDataSource] (http-localhost-127.0.0.1-8080-1) Loaded JDBC driver: com.microsoft.sqlserver.jdbc.SQLServerDriver
08:15:30,736 INFO [stdout] (http-localhost-127.0.0.1-8080-1) ---------------------------------------List size= 5
08:15:30,832 SEVERE [javax.enterprise.resource.webcontainer.jsf.application] (http-localhost-127.0.0.1-8080-1) Error Rendering View[/sarsoura/list.xhtml]: java.io.NotSerializableException: org.springframework.jdbc.datasource.DriverManagerDataSource
at java.io.ObjectOutputStream.writeObject0(Unknown Source) [rt.jar:1.7.0_55]
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source) [rt.jar:1.7.0_55]
您对这个问题有什么想法吗?。非常感谢。