简体   繁体   English

Spring AOP实现后的Spring配置问题

[英]Spring configuration problem after Spring AOP implementation

I'm trying to implement Spring AOP in multiple layers application and make advice for @Service and @Controller classes. 我正在尝试在多层应用程序中实现Spring AOP,并为@Service和@Controller类提供建议。

Everything works fine without aspect class. 没有方面类,一切都很好。 When I add that part of code it causes Spring configuration problem. 当我添加那部分代码时,会导致Spring配置问题。

@Aspect class: @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 aspect configuration: Spring XML方面的配置:

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

Application @Controller and @Service classes: 应用程序@Controller和@Service类:

@Controller 
public class MyCompanyController implements IMyCompanyController{

    @Autowired
    private IMyComapnyService myCompanyService;

}


@Service
public class MyCompanyService implements IMyComapnyService {

    @Autowired
    private IGenericHibernateDao<Object, Integer>  vwObjectDao;

}

Error: 错误:

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)}

Where the problem is? 问题出在哪里?

EDIT: 编辑:

Part of class with Hibernate methods: 使用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);
    }

    ...

}

EDIT (22-02-2019): 编辑(22-02-2019):

When I change this line of code: 当我更改此行代码时:

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

like this: 像这样:

<aop:aspectj-autoproxy />

Error disapears, but aspect does not work. 错误消失了,但是方面不起作用。

I found solution. 我找到了解决方案。

I change this line of code in Spring XML config file: 我在Spring XML配置文件中更改了以下代码行:

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

I set proxy-target-class to false: 我将proxy-target-class设置为false:

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

I deleted this dependency from pom.xml file: 我从pom.xml文件中删除了此依赖项:

<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>2.2</version>
</dependency> 

I set the same spring-aop vesion like Spring version I use in Spring XML config file. 我设置了与Spring XML配置文件中使用的Spring版本相同的spring-aop版本。

I changed this: 我改变了这个:

http://www.springframework.org/schema/aop/spring-aop.xsd

like this: 像这样:

http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM