问题描述
这是applicationContext.xml配置文件<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><beanclass="java.util.Date"id="date"></bean><beanid="datesource"class="com.mchange.v2.c3p0.ComboPooledDataSource"><!--连接信息--><propertyname="driverClass"value="com.mysql.jdbc.Driver"></property><propertyname="jdbcUrl"value="jdbc:mysql://localhost:3306/shop"></property><propertyname="user"value="root"></property><propertyname="password"value="root"></property></bean><!--LocalSessionFactoryBean--><beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><propertyname="datesource"ref="datesource"></property><propertyname="configLocation"value="classpath:hibernate.cfg.xml"></property></bean><beanid="hibernateTemplate"class="org.springframework.orm.hibernate3.HibernateTemplate"><propertyname="sessionFactory"ref="sessionFactory"></property></bean><!--配置事物--><beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><propertyname="sessionFactory"ref="sessionFactory"></property></bean><!--配置通知事物--><tx:adviceid="advice"transaction-manager="transactionManager"><tx:attributes><tx:methodname="save*"propagation="REQUIRED"/><tx:methodname="update*"propagation="REQUIRED"/><tx:methodname="delete*"propagation="REQUIRED"/><tx:methodname="*"propagation="NOT_SUPPORTED"read-only="true"/></tx:attributes></tx:advice><!--引入命名空间--><aop:config><aop:pointcutexpression="execution(*cn.it.shop.service.impl.*.*(..))"id="pointcut"/><aop:advisoradvice-ref="advice"pointcut-ref="pointcut"/></aop:config><beanid="categoryService"class="cn.it.shop.service.impl.CategoryServiceImpl"><propertyname="hibernateTemplate"ref="hibernateTemplate"></property></bean></beans>-----------------------------------------------------------------------------------------------------------------------------------------------这里是hibernate.cfg.xml<?xmlversion='1.0'encoding='UTF-8'?><!DOCTYPEhibernate-configurationPUBLIC"-//Hibernate/HibernateConfigurationDTD3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!--GeneratedbyMyEclipseHibernateTools.--><hibernate-configuration><session-factory><propertyname="show_sql">true</property><!--映射文件--><mappingresource="cn/it/shop/pojo/Category.hbm.xml"/></session-factory></hibernate-configuration>-----------------------------------------------------------------------------------------------------------------------------------------------这是Category.hbm.xml<?xmlversion="1.0"encoding="utf-8"?><!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!--MappingfileautogeneratedbyMyEclipsePersistenceTools--><hibernate-mapping><classname="cn.it.shop.pojo.Category"table="category"catalog="shop"><idname="id"type="java.lang.Integer"><columnname="id"/><generatorclass="native"></generator></id><propertyname="type"type="java.lang.String"><columnname="type"length="20"/></property><propertyname="hot"type="java.lang.Boolean"><columnname="hot"/></property></class></hibernate-mapping>-----------------------------------------------------------------------------------------------------------------------------------------------这是测试类packagecn.it.shop.ssh;importorg.hibernate.Session;importorg.junit.AfterClass;importorg.junit.BeforeClass;importorg.junit.Test;importorg.springframework.context.support.ClassPathXmlApplicationContext;importcn.it.shop.pojo.Category;importcn.it.shop.service.CategoryService;importcn.it.shop.util.HibernateSessionFactory;publicclassSSHTest{privatestaticClassPathXmlApplicationContextapplicationContext=null;@BeforeClass//初始化资源publicstaticvoidbeforeClass(){System.out.println("--beforeClass()--");applicationContext=newClassPathXmlApplicationContext("applicationContext.xml");}@AfterClasspublicstaticvoidafterClass(){System.out.println("--afterClass()--");applicationContext.destroy();}@Test//1.测试Spring的开发环境publicvoidtest01(){System.out.println("--test01()--");System.out.println(applicationContext.getBean("date"));}@Test//2.测试Hibernate的开发环境.事物必须手动提交,不方便publicvoidtest02(){Sessionsession=HibernateSessionFactory.getSession();Categorycategory=newCategory("男士休闲",true);session.getTransaction().begin();session.save(category);session.getTransaction().commit();HibernateSessionFactory.closeSession();}@Test//3.测试Hibernate与Spring的开发环境.变成声明式事物publicvoidtest03(){CategoryServicecategoryService=(CategoryService)applicationContext.getBean("categoryService");Categorycategory=newCategory("男士休闲1",true);categoryService.save(category);}}-----------------------------------------------------------------------------------------------------------------------------------------------这是测试类运行结果:
解决方案
解决方案二:
<propertyname="datesource"ref="datesource"></property>后面的ref对应的值可以随便取,但是前面的property的name值可不能随便取,是dataSource
解决方案三:
<propertyname="datesource"ref="datesource"></property>,哈哈,name="datesource"意味着为变量名为dateSource的变量赋值的,不能随便写的...可以用代码助手自动生成啊
解决方案四:
Spring异常提示已经很明白了呀,DataSource这里的问题呀!beanproperty'datasource'isnotwritable