论坛首页 Java企业应用论坛

想不通为什么这里构造注入正常,set注入就出错

浏览 10720 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-07-20  
下面是配制文件
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

	<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/test"></property>
		<property name="username" value="root"></property>
		<property name="password" value="agsfd"></property>
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="mappingResources">
			<value>hibernate/A.hbm.xml</value>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
			</props>
		</property>

	</bean>

	<!-- 
	-->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<bean id="transactionTemplate"
		class="org.springframework.transaction.support.TransactionTemplate">
		<property name="transactionManager">
			<ref bean="transactionManager" />
		</property>
	</bean>
	<bean id="hibernateTemplate"
		class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>

	<bean id="iTestDao" class="test.TestDao">
		<property name="hibernateTemplate">
			<ref bean="hibernateTemplate" />
		</property>
	</bean>
	<bean id="programmaticManager" class="test.ProgrammaticManager">
	  <constructor-arg>
	   <ref bean="iTestDao"/>
	   </constructor-arg>
		<property name="transactionManager">
			<ref bean="transactionManager" />
		</property>
	</bean>
	<!-- 
	   <property name="iTestDao">
	   <ref bean="iTestDao"/></property>
	-->

</beans>

下面是ProgrammaticManager类,就是这个类iTestDao ,set注入出现在异常,构造注入就正常
package test;

import hibernate.A;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

public class ProgrammaticManager {
	private ITestDao iTestDao;
	private PlatformTransactionManager transactionManager;
	public ProgrammaticManager() {
		// TODO 自动生成构造函数存根
	}
	public ProgrammaticManager(ITestDao it) {
		this.iTestDao=it;
	}
	public PlatformTransactionManager getTransactionManager() {
		return transactionManager;
	}
	
	public void setTransactionManager(PlatformTransactionManager transactionManager) {
		this.transactionManager = transactionManager;
	}
	
	public ITestDao getITestDao() {
		return iTestDao;
	}
	
	public void setITestDao(ITestDao iTestDao) {
		this.iTestDao=iTestDao;
	}
	private TransactionDefinition getDefinition(int isolationLevel) {
        DefaultTransactionDefinition def = new DefaultTransactionDefinition(
            TransactionDefinition.PROPAGATION_REQUIRED);
        def.setIsolationLevel(isolationLevel);
        
        return def;
    }
	 public void insert() {
	        TransactionStatus status = transactionManager.getTransaction(
	            getDefinition(TransactionDefinition.ISOLATION_READ_COMMITTED));
	        try {
//	        	ApplicationContext acx = new ClassPathXmlApplicationContext(
//	    		"file:src/applicationContext.xml");
//	    		iTestDao = (ITestDao) acx.getBean("iTestDao");
	            A ab = new A(new Integer(185),"c++");
	            A abc = new A(new Integer(192),"java");
	            iTestDao.insert(ab);
	            iTestDao.insert(abc);
	            transactionManager.commit(status);
	        } catch (Throwable t) {
	            transactionManager.rollback(status);
	        }
	    
	    }

}


下面是异常信息
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'programmaticManager' defined in URL [file:src/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'iTestDao' of bean class [test.ProgrammaticManager]: Bean property 'iTestDao' is not writable or has an invalid setter method. Did you mean 'ITestDao'?
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1133)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:865)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:404)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:255)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:252)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:287)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:689)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:358)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:91)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:75)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:65)
	at test.Test.main(Test.java:9)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'iTestDao' of bean class [test.ProgrammaticManager]: Bean property 'iTestDao' is not writable or has an invalid setter method. Did you mean 'ITestDao'?
	at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:793)
	at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:645)
	at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:78)
	at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:59)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1130)
	... 13 more

   发表时间:2007-07-20  
试过spring 2.0 和2.1 m2
0 请登录后投票
   发表时间:2007-07-20  
提示"invalid setter method" 要么属性名有问题要么set的类型不匹配
0 请登录后投票
   发表时间:2007-07-20  
bluepopopo 写道
提示"invalid setter method" 要么属性名有问题要么set的类型不匹配

构造注入就没问题,属性名和类型都是一样的
0 请登录后投票
   发表时间:2007-07-20  
test.TestDao是接口吧,<bean id="iTestDao" class="test.TestDao">,这里为什么不用接口实现呢?

另外iXXXDao感觉怪怪的,直接XXXDao就好了?
0 请登录后投票
   发表时间:2007-07-20  
把iTestDao全改成testDao就解决
0 请登录后投票
   发表时间:2007-07-20  
tom.duan 写道
test.TestDao是接口吧,<bean id="iTestDao" class="test.TestDao">,这里为什么不用接口实现呢?

另外iXXXDao感觉怪怪的,直接XXXDao不就好了?

TestDao就是实现ITestDao接口
0 请登录后投票
   发表时间:2007-07-20  
据本人所知,第一个字母是小写,而第二个字母是大写的属性,它的get和set方法名字不是get+属性第一个字母大写。例如iTestDao的set方法不是setITestDao,可能是setItestDao。所以不建议使用第一个字母是小写,而第二个字母是大写的属性。
0 请登录后投票
   发表时间:2007-07-20  
spring默认命名方式不能以双大写字母开头。。。。
1 请登录后投票
   发表时间:2007-07-24  
在依赖注入的时候的问题,把属性名改成全部小写试一下?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics