Spring - 将AOP添加到项目

时间:2016-04-24 18:41:45

标签: java spring aop spring-data-jpa spring-aop

我有以下eof bean:

@Configuration
@EnableJpaRepositories("....repositories")
public class JpaConfig {
    ...
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource,
                                                                       JpaVendorAdapter jpaVendorAdapter,
                                                                       Properties jpaProperties) {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactory.setDataSource(dataSource);
        entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter);
        entityManagerFactory.setPackagesToScan("....entities");
        entityManagerFactory.setJpaProperties(jpaProperties);
        return entityManagerFactory;
    }
}

在我向项目中添加一个方面之前,它完美地工作(即使使用声明的aop依赖项)。将方面标记为组件后,我得到以下异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [.../configs/JpaConfig.class]: No matching factory method found: factory bean 'jpaConfig'; factory method 'entityManagerFactory()'. Check that a method with the specified name exists and that it is non-static.

这是我的Maven依赖项:

    <!--AOP-->

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.2.5.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.9</version>
    </dependency>

    <!--Data-->

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.1.0.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.1.0.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.2.4.Final</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.10.1.RELEASE</version>
    </dependency>

    <!--Database-->

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4.1208.jre7</version>
    </dependency>

    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <version>2.4.5</version>
        <scope>compile</scope>
    </dependency>

将根据要求提供其他信息。非常感谢你。

更新 方面:

@Aspect
@Component
public class TestAspect {
    @Before("execution(public * *(..))")
    public void test() {
        System.out.println("Public Method invoked");
    }
}

更新 有趣的是,这方面的工作和打印测试字符串。

0 个答案:

没有答案