码迷,mamicode.com
首页 > 编程语言 > 详细

spring 切面织入报错:java.lang.ClassCastException: com.sun.proxy.$Proxy7 cannot be cast to...

时间:2014-06-20 20:58:03      阅读:900      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   java   ext   color   

报这个错,只有一个原因,就是转化的类型不对.

接口过父类的子类,在强制转换的时候,一定要用接口父类来定义。

代码示例:

package com.luoluo.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;

import javax.annotation.Resource;
import javax.sql.DataSource;

import com.luoluo.dao.UserDAO;
import com.luoluo.model.User;

public class UserDAOImpl implements UserDAO {
	private DataSource dataSource;
	

	public DataSource getDataSource() {
		return dataSource;
	}

	@Resource
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	
	public void save(User user) {
		//Hibernate
		//JDBC
		//XML
		//NetWork
		try {
			Connection conn = dataSource.getConnection();
			conn.createStatement().executeUpdate("insert into user values (null,‘zhang‘,‘123456‘)");
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("user saved!");
	}

	@Override
	public void delete(User user) {
		System.out.println("user deleted!");		
	}

}

 

package com.luoluo.myapp;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.luoluo.dao.UserDAO;
import com.luoluo.dao.impl.LogInterceptor;
import com.luoluo.model.User;

public class UserServiceTest {
	@Test
	public void testAspect(){
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {"servlet-context.xml", "root-context.xml"});
		UserDAO userDAOImpl=(UserDAO)ctx.getBean("userDAOImpl");//由于UserDAOImpl实现了UserDAO接口,所以强制转换必须用父类UserDAO来定义
		
		LogInterceptor logInterceptor =(LogInterceptor) ctx.getBean("logInterceptor");
		System.out.println(logInterceptor.getClass().getName());
		userDAOImpl.save(new User());
		userDAOImpl.delete(new User());
	}

}

  

  

 

spring 切面织入报错:java.lang.ClassCastException: com.sun.proxy.$Proxy7 cannot be cast to...,布布扣,bubuko.com

spring 切面织入报错:java.lang.ClassCastException: com.sun.proxy.$Proxy7 cannot be cast to...

标签:style   class   blog   java   ext   color   

原文地址:http://www.cnblogs.com/vtyluoluo/p/3793069.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!