【发布时间】:2019-02-26 16:34:12
【问题描述】:
我正在尝试在多层应用程序中实现 Spring AOP,并为 @Service 和 @Controller 类提供建议。
没有方面类一切正常。当我添加那部分代码时,它会导致 Spring 配置问题。
@Aspect 类:
@Aspect
@Component
public class ApplicationMonitor {
private static final Logger logger = Logger.getLogger(ApplicationMonitor.class);
@Pointcut(value = "execution(* hr.mycompany.controller.impl.MyCompanyController.update(*)) && args(obj)")
public void updateMC(Object obj){}
@Before(value="updateMC(obj)")
public void beforeUpdateMC(JoinPoint jp, Object obj) {
Object obj = jp.getArgs()[0];
logger.info("beforeUpdateMC " + obj);
}
}
Spring XML 方面配置:
<aop:aspectj-autoproxy proxy-target-class="true"/>
应用程序@Controller 和@Service 类:
@Controller
public class MyCompanyController implements IMyCompanyController{
@Autowired
private IMyComapnyService myCompanyService;
}
@Service
public class MyCompanyService implements IMyComapnyService {
@Autowired
private IGenericHibernateDao<Object, Integer> vwObjectDao;
}
错误:
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)}
问题出在哪里?
编辑:
具有 Hibernate 方法的类的一部分:
@Transactional(readOnly = true)
public abstract class GenericHibernateDao<T, PK extends Serializable> implements IGenericHibernateDao<T, PK> {
private static final Logger log = LoggerFactory.getLogger(GenericHibernateDao.class);
@Autowired
@Qualifier(value = "hibernateSessionFactory")
protected SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@SuppressWarnings("unchecked")
@Transactional(readOnly = false)
public PK save(T entity) {
Assert.notNull(entity, "Argument entity cannot be null in a call to GenericHibernateDao.save !");
Session session = getSessionFactory().getCurrentSession();
return (PK) session.save(entity);
}
...
}
编辑(2019 年 2 月 22 日):
当我更改这行代码时:
<aop:aspectj-autoproxy proxy-target-class="true"/>
像这样:
<aop:aspectj-autoproxy />
错误消失,但方面不起作用。
【问题讨论】:
-
你有
hr.mycompany.dao.IGenericHibernateDao和它的一个实现?你能展示其中的基本部分吗? -
@pirho 实现包含休眠方法。我对错误感到困惑,因为没有方面实现一切都很好。
-
请将这些内容编辑到您的问题中并删除 cmets。
-
我想我已经告诉过你如何在另一个问题中修复你的方面代码,所以也许将工作方面复制到这个后续问题而不是损坏的问题中是有意义的,这样每个人都可以专注于你的新问题。或者您是否仍在使用语法无效的方面?如果您认为该方面会导致问题,请向我们准确展示您所拥有的。
-
@kriegaex 抱歉,这是我的错误 - 我从上一个问题中复制粘贴了方面 - 但方面现在是正确的 - 我完全按照你纠正我的方式更改了它们。我现在将在我的问题中编辑代码的方面部分。