我对Spring AOP有问题。
我正在尝试在Spring MVC应用程序中实现各个方面。
我得到的错误是:
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
是什么原因导致此问题?
在XML文件中,我有这个:
<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文件中,我具有以下依赖性:
<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>
编辑(更改切入点后):
我更改了切入点,现在出现了这样的错误:
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)}
我的应用程序具有多个层和类似的类:
@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方法提供建议。
答案 0 :(得分:1)
用户 heonlyrao 提出了正确的建议,但使用了错误的切入点语法。您要这样做:
@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 的建议,如果您不在多个建议中重复使用相同的切入点,也可以内联它:
@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);
}
说明:您的错误消息是:formal unbound in pointcut
。这意味着您在切入点和/或建议方法签名中使用的形式参数没有反映在切入点中,反之亦然。