论坛首页 Java企业应用论坛

关于用spring的aop拦截带有JdbcTemplate参数的方法时抛出异常

浏览 11672 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-05-10  
   拦截其它方法时都没有遇到这样的问题。以下是异常信息。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in class path resource [spring/daoContext.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy1] to required type [org.springframework.jdbc.core.JdbcTemplate] for property 'jdbcTemplate'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy1] to required type [org.springframework.jdbc.core.JdbcTemplate] for property 'jdbcTemplate': no matching editors or conversion strategy found
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessException details (1) are:
PropertyAccessException 1:
org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy1] to required type [org.springframework.jdbc.core.JdbcTemplate] for property 'jdbcTemplate'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy1] to required type [org.springframework.jdbc.core.JdbcTemplate] for property 'jdbcTemplate': no matching editors or conversion strategy found
Caused by: java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy1] to required type [org.springframework.jdbc.core.JdbcTemplate] for property 'jdbcTemplate': no matching editors or conversion strategy found^^

配置其中一个文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans SYSTEM
	"file:///E:/program/project/start/bmsop/dtd/spring-beans-2.0.dtd">
<beans>
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
		<property name="url" value="jdbc:mysql://localhost/helloworld"/>
		<property name="username" value="root"/>
		<property name="password" value="root"/>
		<property name="maxActive" value="100"/>
		<property name="maxIdle" value="100"/>
	</bean>
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<bean id="userDao"
		class="example.helloworld.dao.jdbc.UserDaoJdbcImpl">
		<property name="jdbcTemplate" ref="jdbcTemplate"/>
	</bean>
</beans>

类:
/**
 * 
 */
package example.helloworld.dao.jdbc;

import java.util.List;

import example.helloworld.dao.UserDao;
import example.helloworld.domain.User;

/**
 * @author
 * 
 */
public class UserDaoJdbcImpl extends Base implements UserDao {

	public void updateUser(User user) {
		String sql = "UPDATE user SET name=?,age=?,mail=? WHERE id=?";
		Object param[] = new Object[] { user.getName(), user.getAge(),
				user.getMail(), user.getId() };
		getJdbcTemplate().update(sql, param);
	}

	public void addUser(User user) {
		String sql = "INSERT INTO user(name,age,mail) VALUES(?,?,?)";
		Object param[] = new Object[] { user.getName(), user.getAge(),
				user.getMail() };
		this.getJdbcTemplate().update(sql, param);
	}

	@SuppressWarnings("unchecked")
	public List<User> getAllUser() {
		return (List<User>) getJdbcTemplate().query("SELECT * FROM user",
				new UserRowMapper());
	}

	public User getUserById(int id) {
		return (User) this.getJdbcTemplate().queryForObject(
				"SELECT * FROM user WHERE id=?", new Integer[] { id },
				new UserRowMapper());
	}

}



package example.helloworld.dao.jdbc;

import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @author
 * 
 */
public abstract class Base {
	@SuppressWarnings("unused")
	private JdbcTemplate jdbcTemplate;

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
}
   发表时间:2007-05-10  
这个应该是你的配置文件和你类中间设置属性不对应导致的,请仔细检查你的配置文件和相关的类
0 请登录后投票
   发表时间:2007-05-10  
    我把关于aop的bean都放在一个单独的配置文件里头。在我初始化applicationContext时若不读入aop配置文件,所有类和方法都工作正常,但加入aop配置文件后。applicationContext初始化会失败。
0 请登录后投票
   发表时间:2007-05-10  
   补上aop有关的配置和类。
配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans SYSTEM "../../web/WEB-INF/lib/spring-beans-2.0.dtd">
<beans>
	<bean id="logIntercepter" class="example.components.log.AopLog"/>
	<bean id="advisor"
		class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
		<property name="advice" ref="logIntercepter"/>
		<property name="pattern">
			<value>.*</value>
		</property>
	</bean>
	<bean id="autoProxyCreator"
		class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
	</bean>
</beans>

类:

/**
 * 
 */
package example.components.log;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 * @author
 * 
 */
public class AopLog implements MethodInterceptor {
	Printer printer = new Printer();

	public Object invoke(MethodInvocation invocation) throws Throwable {
		printer.getLog().info(
				"进入:" + invocation.getClass().getName() + "类的"
						+ invocation.getMethod().getName() + "方法.");
		printer.getLog().info("输入的参数为:");
		printer.print(invocation.getArguments());
		Object result = invocation.proceed();
		printer.getLog().info("方法返回的结果为:");
		printer.print(result);
		return result;
	}

}
0 请登录后投票
   发表时间:2007-05-17  
这里只是要创建dao的aop代理,为什么要用DefaultAdvisorAutoProxyCreator呢?从错误信息来看,应该是jdbcTemplate也生成了代理,这个代理set到你的userDao就出现了错误:org.springframework.beans.TypeMismatchException。
0 请登录后投票
   发表时间:2007-05-17  
  我写这个aop的目的是在项目开发阶段,拦截所有的方法,打印出所有传入方法的参数与返回值。
因为我把aop的相关配置都单独放在一个配置文件中。所以可以随时不要这个功能。
  如果像你说的那样,那么所有被拦截的没有实现接口的bean,都不能被注入到另外的bean当中?有没有什么解决方法?
0 请登录后投票
   发表时间:2007-05-18  
你的org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator这个bean需要设置属性proxyTargetClass为true,这样产生的代理对象才会instanceof原来的类。
0 请登录后投票
论坛首页 Java企业应用版

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