本文介绍了由于“切入点中的正式未绑定"导致的 Spring AOP BeanCreationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Spring AOP 有问题.

I have problem with Spring AOP.

我正在尝试在 Spring MVC 应用程序中实现方面.

I'm trying to implement aspects in Spring MVC aplication.

我得到的错误是:

Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'org.springframework.cache.annotation.AnnotationCacheOperationSource#0':
Initialization of bean failed; nested exception is
  java.lang.IllegalArgumentException:
  error at ::0 formal unbound in pointcut 

是什么导致了这个问题?

What causes this problem?

在 XML 文件中我有这个:

In XML file I have this:

<aop:aspectj-autoproxy proxy-target-class="true"/>

我的方面类:

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ApplicationMonitor {
    private static final Logger logger = Logger.getLogger(ApplicationMonitor.class);

    @Pointcut(value = "execution(* hr.mycompany.controller.impl.MyCompanyController.update(Object))") 
    public void updateMC(Object obj){}

    @Before(value="ApplicationMonitor.updateMC(Object)")
    public void beforeUpdateMC(JoinPoint jp) {
        Object obj = jp.getArgs()[0];
        logger.info("beforeUpdateMC " + obj);
    }
}

在 pom.xml 文件中我有这个依赖项:

In pom.xml file I have this dependencies:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.1.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjtools</artifactId>
        <version>1.8.10</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.10</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.10</version>
    </dependency>
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2</version>
    </dependency>

编辑(更改切入点后):

我改变了切入点,现在我收到了这样的错误:

I changed pointcut and now I'm getting errors like this:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [hr.mycompany.dao.IGenericHibernateDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}


09:11:27,871 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/BasicData-portlet]] (http--0.0.0.0-8083-2) StandardWrapper.Throwable: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyCompanyService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private hr.mycompany.dao.IGenericHibernateDao hr.mycompany.services.impl.MyCompanyService.vwObjectDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [hr.mycompany.dao.IGenericHibernateDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

我的应用程序有多个层和类,如下所示:

My app has multiple layers and classes like this:

@Controller 
public class MyCompanyController implements IMyCompanyController{

    @Autowired
    private IMyComapnyService myCompanyService;

}


@Service
public class MyCompanyService implements IMyComapnyService {

    @Autowired
    private IGenericHibernateDao<Object, Integer>  vwObjectDao;

}

我正在尝试为 @Controller 和 @Service @Autowired 方法提供建议.

I'm trying to make advice for @Controller and @Service @Autowired method.

推荐答案

用户 heonlyrao 提出了正确的建议,但使用了错误的切入点语法.你想这样做:

User heonlyrao suggested the right thing but with the wrong pointcut syntax. You want to do this:

    @Pointcut("execution(* hr.mycompany.controller.impl.MyCompanyController.update(*)) && args(obj)") 
    public void updateMC(Object obj) {}

    @Before("updateMC(obj)")
    public void beforeUpdateMC(JoinPoint jp, Object obj) {
        // Not needed if parameter is bound via args()
        // Object obj = jp.getArgs()[0];
        logger.info("beforeUpdateMC " + obj);
    }

或者,类似于heonlyrao的建议,如果您不在多个通知中重复使用相同的切入点,您也可以将其内联:

Or, similar to heonlyrao's suggestion and if you do not re-use the same pointcut in multiple advices, you can also inline it:

    @Before("execution(* hr.mycompany.controller.impl.MyCompanyController.update(*)) && args(obj)")
    public void beforeUpdateMC(JoinPoint jp, Object obj) {
        // Not needed if parameter is bound via args()
        // Object obj = jp.getArgs()[0];
        logger.info("beforeUpdateMC " + obj);
    }

说明:您的错误消息说:在切入点中正式未绑定.这意味着您要么在切入点和/或通知方法签名中使用了未反映在切入点中的形式参数,反之亦然.

Explanation: Your error message says: formal unbound in pointcut. This means that you are either using a formal parameter in your pointcut and/or advice method signature which is not reflected in the pointcut or vice versa.

这篇关于由于“切入点中的正式未绑定"导致的 Spring AOP BeanCreationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 04:49