首页 新闻 搜索 专区 学院

SpringAOP异常

0
悬赏园豆:20 [已关闭问题] 关闭于 2017-12-29 12:01

很奇怪,加了这2行就会报错,不加就什么事都没有

Log4j.java

 

 

项目结构图:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
cn.csu.controller.UserController's constructor
cn.csu.service.UserService's constructor
cn.csu.dao.impl.UserDao's constructor
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.csu.service.UserService cn.csu.controller.UserController.service; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.csu.dao.impl.UserDao cn.csu.service.UserService.userDao; nested exception is java.lang.IllegalArgumentException: Can not set cn.csu.dao.impl.UserDao field cn.csu.service.UserService.userDao to com.sun.proxy.$Proxy9
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at cn.csu.test.SpringTest.main(SpringTest.java:12)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.csu.service.UserService cn.csu.controller.UserController.service; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.csu.dao.impl.UserDao cn.csu.service.UserService.userDao; nested exception is java.lang.IllegalArgumentException: Can not set cn.csu.dao.impl.UserDao field cn.csu.service.UserService.userDao to com.sun.proxy.$Proxy9
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    ... 13 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.csu.dao.impl.UserDao cn.csu.service.UserService.userDao; nested exception is java.lang.IllegalArgumentException: Can not set cn.csu.dao.impl.UserDao field cn.csu.service.UserService.userDao to com.sun.proxy.$Proxy9
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1014)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:957)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
    ... 15 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.csu.dao.impl.UserDao cn.csu.service.UserService.userDao; nested exception is java.lang.IllegalArgumentException: Can not set cn.csu.dao.impl.UserDao field cn.csu.service.UserService.userDao to com.sun.proxy.$Proxy9
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    ... 26 more
Caused by: java.lang.IllegalArgumentException: Can not set cn.csu.dao.impl.UserDao field cn.csu.service.UserService.userDao to com.sun.proxy.$Proxy9
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(Unknown Source)
    at java.lang.reflect.Field.set(Unknown Source)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:504)
    ... 28 more
问题补充:

IUserDao.java:

package cn.csu.dao;

import java.util.Map;

public interface IUserDao {

    void addUser();

    void deleteUserById(int id);
    
    void updtaeUserById(int id);
    
    void queryUser(String sql,Map<String,Object> map);
    
}

UserDaoImpl.java:

package cn.csu.dao.impl;

import java.util.Map;
import org.springframework.stereotype.Repository;
import cn.csu.dao.IUserDao;


@Repository
public class UserDaoImpl implements IUserDao {

    public UserDaoImpl() {
        System.out.println(this.getClass().getName() + "'s constructor");
    }

    @Override
    public void addUser() {
        System.out.println("addUser ");

    }

    @Override
    public void deleteUserById(int id) {
        System.out.println("deleteUser ");

    }

    @Override
    public void updtaeUserById(int id) {
        System.out.println("updateUser ");

    }

    @Override
    public void queryUser(String sql, Map<String, Object> map) {
        System.out.println("queryUser ");

    }

}

 

UserService.java:

package cn.csu.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.csu.dao.impl.UserDaoImpl;
import cn.csu.entity.Employee;

@Service
public class UserService {

    @Autowired
    public UserDaoImpl userDao;

    public UserService() {
        System.out.println(this.getClass().getName() + "'s constructor");

    }

    /**
     * @param userDao the userDao to set
     */
    public void setUserDao(UserDaoImpl userDao) {
        this.userDao = userDao;
    }

    public void register(Employee e) {

        userDao.addUser();
    }
}

 

 

 

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    
    <context:component-scan base-package="cn.csu"></context:component-scan>
    <bean id="log" class="cn.log.Log4j">
    <constructor-arg  value="str"></constructor-arg>
    </bean>
    
    <aop:config>
        <aop:aspect ref="log">
            <aop:pointcut expression="execution(* *.addUser(..))"
                id="addUser" />

            
            <aop:after pointcut-ref="addUser" method="afterMethod" />
            <aop:before pointcut-ref="addUser" method="beforeMethod" />
            
        </aop:aspect>

    </aop:config>
    
</beans>
rm_rf的主页 rm_rf | 初学一级 | 园豆:6
提问于:2017-11-03 23:18
< >
分享
所有回答(2)
0

ref 的addUser的问题    你可以先定义一个 bean  在引用id  就好了   

原因就是找不到这个方法

程序员大宇 | 园豆:211 (菜鸟二级) | 2017-11-08 14:29

我引用的addUser就是上面声明的切点啊  现在引用它就是定义它的前置和后置通知

支持(0) 反对(0) rm_rf | 园豆:6 (初学一级) | 2017-11-08 14:35

报错信息是:Can not set cn.csu.dao.impl.UserDao field cn.csu.service.UserService.userDao to com.sun.proxy.$Proxy9

支持(0) 反对(0) rm_rf | 园豆:6 (初学一级) | 2017-11-11 17:53

不能把userDao的值赋给代理,我很纳闷

支持(0) 反对(0) rm_rf | 园豆:6 (初学一级) | 2017-11-11 17:53
0

IUserDao.java:

package cn.csu.dao;

import java.util.Map;

public interface IUserDao {

    void addUser();

    void deleteUserById(int id);
    
    void updtaeUserById(int id);
    
    void queryUser(String sql,Map<String,Object> map);
    
}

 

UserDaoImpl.java:

rm_rf | 园豆:6 (初学一级) | 2017-11-19 17:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册