下面是配制文件
<!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
分享到:
相关推荐
'如果单一使用穷举 Form 对象的操作就会出错 '所以要把这些页面过滤出来,同时在页面中使用 sql("检测的字串")才行 '垃圾猪 zero@new57.com 'http://blog.csdn.net/cfaq '将本页用 include 方法放在头部以让所有页都...
2. **参数化查询**:为了避免SQL注入攻击,`Sql_set`可能支持预编译的参数化SQL语句。例如,使用PreparedStatement,将用户输入转化为占位符,如`"SELECT * FROM Users WHERE username = ? AND password = ?"`,然后...
这里的`$id`是用户输入,如果不经验证,就可能导致SQL注入。 2. **INSERT查询**:用于向数据库中插入新记录,如`insert into news(id,url,text) values(2,'x','$t')`。`$t`代表用户提供的文本,需确保其安全性。 3. ...
#### 四、为什么使用Spring 引入Spring框架可以为项目带来以下几个方面的好处: 1. **降低组件之间的耦合度**:通过使用IoC和DI,组件间的依赖关系变得更为松散,有利于系统的扩展和维护。 2. **使用容器提供的...
同时,Spring支持通过构造函数和set方法两种方式来注入依赖。 9. **自动赋值**:Spring提供了自动装配机制,如byname、byType、constructor和autodetect,可以根据属性名、类型或构造函数匹配来自动找到并设置依赖...
在这里,即使`username`和`email`都为`null`,由于使用了`<set>`元素,最终生成的SQL语句也不会有多余的逗号。 ### 动态SQL的安全性 尽管MyBatis动态SQL提供了一种强大而灵活的方式来构造SQL语句,但它也存在一定...
则通过一系列内置的标签(如`<if>`、`<choose>`、`<when>`、`<otherwise>`、`<trim>`、`<where>`、`<set>`等)实现了对SQL语句的灵活控制,并将这些标签置于XML映射文件中进行定义,这样就极大地简化了SQL语句的构造...
这里,LambdaQueryWrapper通过方法引用来设置条件,使得代码更易读且降低了出错的可能性。 总的来说,MyBatis-Plus的QueryWrapper是Spring Boot中进行数据操作的强大工具,它简化了SQL语句的编写,提高了开发效率。...
- **框架设计**:Spring框架就大量使用反射来实现依赖注入和AOP(面向切面编程)。 - **序列化/反序列化**:如`java.io.ObjectInputStream`和`java.io.ObjectOutputStream`利用反射恢复对象状态。 - **单元测试**:...
9. **依赖注入**:通过构造函数或属性注入,使得组件之间解耦,便于测试和维护。 10. ** Traits **:解决单继承限制,允许代码重用,提高代码的灵活性。 以上只是基于一般经验的推测,实际的“PHP的十个高级技巧”...
- 最后,设置字符编码以确保数据的正确读写,这里使用`SET NAMES`语句。 4. **SQL查询执行**: - `query()`方法是核心功能,它接收SQL语句作为参数,执行查询并返回结果资源。 - 如果SQL语句为空,会抛出错误。 ...
使用`getFields()`获取公共字段,`getDeclaredFields()`获取所有字段,然后通过`set()`和`get()`方法操作字段。 2. 访问方法:`Method`类代表类的方法。`getMethods()`获取公共方法,`getDeclaredMethods()`获取...