applicationContext.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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/work"></property>
<property name="username" value="root"></property>
<property name="password" value="fung"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>
com/yourcompany/struts/action/Student.hbm.xml
</value>
<value>
com/yourcompany/struts/action/Course.hbm.xml
</value>
<value>com/yourcompany/struts/action/Sc.hbm.xml</value></list>
</property></bean>
<bean id="StudentDAO"
class="com.yourcompany.struts.action.StudentDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean name="/login" class="com.yourcompany.struts.action.LoginAction"
abstract="false" lazy-init="default" autowire="default"
scope="prototype" dependency-check="default">
<property name="studentDAO" ref="StudentDAO" />
</bean>
<bean id="CourseDAO"
class="com.yourcompany.struts.action.CourseDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="scdao" class="com.yourcompany.struts.action.ScDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean name="/selscore" class="com.yourcompany.struts.action.SelscoreAction"
abstract="false" lazy-init="default" autowire="default"
scope="prototype" dependency-check="default">
<property name="scDAO" ref="ScDAO" />
<property name="studentDAO" ref="StudentDAO" />
<property name="courseDAO" ref="CourseDAO" />
</bean>
<bean id="userDao" class="com.yourcompany.struts.test.UserDao"></bean>
</beans>
Servlet 部分代码如下:
package com.yourcompany.struts.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.hibernate.SessionFactory;import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;public class getScore extends HttpServlet{
private static final long serialVersionUID = 36073319881026L;
private ScDAO scDao;public void setscdao(ScDAO userDao) {
this.scDao = userDao;
}
public ScDAO getscdao() {
return this.scDao;
} public void init() throws ServletException {
super.init();
ServletContext servletContext = this.getServletContext();
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
AutowireCapableBeanFactory autowireCapableBeanFactory = ctx
.getAutowireCapableBeanFactory();
autowireCapableBeanFactory.configureBean(this, "Scdao"); **********************在这里出现了问题!
}public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException ,ServletException
{
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
String stuID=request.getParameter("stuID");
System.out.println(stuID);
//scDao.findByProperty("sid", stuID);
scDao.findAll();
List array=scDao.findAll();
}public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException,ServletException{
doGet(request,response);}
}错误如下:
org.springframework.beans.NotWritablePropertyException: Invalid property 'sessionFactory' of bean class [com.yourcompany.struts.action.getScore]: Bean property 'sessionFactory' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?我是个新手,哪位能帮帮忙~!
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/work"></property>
<property name="username" value="root"></property>
<property name="password" value="fung"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>
com/yourcompany/struts/action/Student.hbm.xml
</value>
<value>
com/yourcompany/struts/action/Course.hbm.xml
</value>
<value>com/yourcompany/struts/action/Sc.hbm.xml</value></list>
</property></bean>
<bean id="StudentDAO"
class="com.yourcompany.struts.action.StudentDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean name="/login" class="com.yourcompany.struts.action.LoginAction"
abstract="false" lazy-init="default" autowire="default"
scope="prototype" dependency-check="default">
<property name="studentDAO" ref="StudentDAO" />
</bean>
<bean id="CourseDAO"
class="com.yourcompany.struts.action.CourseDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="scdao" class="com.yourcompany.struts.action.ScDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean name="/selscore" class="com.yourcompany.struts.action.SelscoreAction"
abstract="false" lazy-init="default" autowire="default"
scope="prototype" dependency-check="default">
<property name="scDAO" ref="ScDAO" />
<property name="studentDAO" ref="StudentDAO" />
<property name="courseDAO" ref="CourseDAO" />
</bean>
<bean id="userDao" class="com.yourcompany.struts.test.UserDao"></bean>
</beans>
Servlet 部分代码如下:
package com.yourcompany.struts.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.hibernate.SessionFactory;import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;public class getScore extends HttpServlet{
private static final long serialVersionUID = 36073319881026L;
private ScDAO scDao;public void setscdao(ScDAO userDao) {
this.scDao = userDao;
}
public ScDAO getscdao() {
return this.scDao;
} public void init() throws ServletException {
super.init();
ServletContext servletContext = this.getServletContext();
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
AutowireCapableBeanFactory autowireCapableBeanFactory = ctx
.getAutowireCapableBeanFactory();
autowireCapableBeanFactory.configureBean(this, "Scdao"); **********************在这里出现了问题!
}public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException ,ServletException
{
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
String stuID=request.getParameter("stuID");
System.out.println(stuID);
//scDao.findByProperty("sid", stuID);
scDao.findAll();
List array=scDao.findAll();
}public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException,ServletException{
doGet(request,response);}
}错误如下:
org.springframework.beans.NotWritablePropertyException: Invalid property 'sessionFactory' of bean class [com.yourcompany.struts.action.getScore]: Bean property 'sessionFactory' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?我是个新手,哪位能帮帮忙~!
this.scDao = userDao;
}
public ScDAO getScDao() {
return this.scDao;
}这样试试。
没有继承hibernateDaoSupport吧。或者没有setSessionFactory方法
没有继承hibernateDaoSupport吧。或者没有setSessionFactory方法
_______________________________________________________________
或者没有setSessionFactory方法 如何解决?
package com.yourcompany.struts.action;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;/**
* A data access object (DAO) providing persistence and search support for Sc
* entities. Transaction control of the save(), update() and delete() operations
* can directly support Spring container-managed transactions or they can be
* augmented to handle user-managed Spring transactions. Each of these methods
* provides additional information for how to configure it for the desired type
* of transaction control.
*
* @see com.yourcompany.struts.action.Sc
* @author MyEclipse Persistence Tools
*/public class ScDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(ScDAO.class);
// property constants
public static final String SCORE = "score";
protected void initDao() {
// do nothing
} public void save(Sc transientInstance) {
log.debug("saving Sc instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
} public void delete(Sc persistentInstance) {
log.debug("deleting Sc instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
} public Sc findById(com.yourcompany.struts.action.ScId id) {
log.debug("getting Sc instance with id: " + id);
try {
Sc instance = (Sc) getHibernateTemplate().get(
"com.yourcompany.struts.action.Sc", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
} public List findByExample(Sc instance) {
log.debug("finding Sc instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
} public List findByProperty(String propertyName, Object value) {
log.debug("finding Sc instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Sc as model where model." + propertyName
+ "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
} public List findByScore(Object score) {
return findByProperty(SCORE, score);
} public List findAll() {
log.debug("finding all Sc instances");
try {
String queryString = "from Sc";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
} public Sc merge(Sc detachedInstance) {
log.debug("merging Sc instance");
try {
Sc result = (Sc) getHibernateTemplate().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
} public void attachDirty(Sc instance) {
log.debug("attaching dirty Sc instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
} public void attachClean(Sc instance) {
log.debug("attaching clean Sc instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
} public static ScDAO getFromApplicationContext(ApplicationContext ctx) {
return (ScDAO) ctx.getBean("ScDAO");
}
public void getAll()
{
String hql="from Sc x where x.sid=?";
String name="12315";
System.out.println("xxx...\n");
List list=this.getHibernateTemplate().find(hql,(Object)name);
System.out.println(list); }
}