簡體   English   中英

Spring AOP實現后的Spring配置問題

[英]Spring configuration problem after Spring AOP implementation

我正在嘗試在多層應用程序中實現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);
    }

    ...

}

編輯(22-02-2019):

當我更改此行代碼時:

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

像這樣:

<aop:aspectj-autoproxy />

錯誤消失了,但是方面不起作用。

我找到了解決方案。

我在Spring XML配置文件中更改了以下代碼行:

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

我將proxy-target-class設置為false:

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

我從pom.xml文件中刪除了此依賴項:

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

我設置了與Spring XML配置文件中使用的Spring版本相同的spring-aop版本。

我改變了這個:

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

像這樣:

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