beans.xml配置
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <context:annotation-config/> <context:component-scan base-package="com"/> <aop:aspectj-autoproxy/> <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
在这里数据源,sessionFactory省略不写了
userDao代码:
@Component("userDao") public class UserDAO extends HibernateDaoSupport { private static final Logger log = LoggerFactory.getLogger(UserDAO.class); // property constants public static final String SEX = "sex"; public static final String NAME = "name"; public static final String PASSWORD = "password"; public static final String AGE = "age"; protected void initDao() { // do nothing } @Resource(name="sessionFactory") public void setBaseSessionFactory(SessionFactory sessionFactory){ super.setSessionFactory(sessionFactory); } public List findAll() { log.debug("finding all User instances"); try { String queryString = "from User"; return getHibernateTemplate().find(queryString); } catch (RuntimeException re) { log.error("find all failed", re); throw re; } } }
UserServiceImpl文件
package com.service.impl; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.dao.UserDAO; import com.mode.User; import com.service.UserService; @Component("userService") public class UserServiceImpl implements UserService { private UserDAO dao; public UserDAO getDao() { return dao; } @Resource(name="userDao") public void setDao(UserDAO dao) { System.out.println("kkkkk"); this.dao = dao; } public List<User> list() { // TODO Auto-generated method stub return dao.findAll(); } public User login(String name, String password) { // TODO Auto-generated method stub return dao.getUserByNameAndPassword(name, password); } public User getUserById(String id) { // TODO Auto-generated method stub return null; } }
切面代码:
package com.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect @Component("myAspect") public class MyAspect { @Before("execution(* com.dao.UserDAO.*(..))") public void before(){ System.out.println("_________________________"); } }
测试代码:
ApplicationContext context = new ClassPathXmlApplicationContext("beans7.xml"); UserService service = (UserService) context.getBean("userService"); System.out.println(service); System.out.println(service.list());
问题:
Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'userDao' must be of type [com.dao.UserDAO], but was actually of type [$Proxy13]
很奇怪,如果我把切面的@component去掉就可以正常运行;为什么?