简体   繁体   中英

How to configure AOP in Spring

I haven't used AOP before in Spring before, so I"m just trying to get a simple example working with my application.

This is the code I used:

/**
 * The product cache aspect manages the product cache.
 *
 */
@Aspect
public class ProductCacheAspect {

    private static Logger logger = LogManager.getLogger(ProductCacheAspect.class.getName());

    @Autowired
    private MemcachedClient memcachedClient;

    @AfterReturning(
            pointcut = "execution(* com.ideafactory.mvc.products.common.services.ProductService.get(..))",
            returning= "result")
    public void logAfterReturning(JoinPoint joinPoint, Object result) {
        logger.debug("Executed point cut");
        logger.debug(result);
    }
}

And I think I have it configured correctly:

@Configuration
@ComponentScan(basePackages = {"com.ideafactory"})
@PropertySource(value = {"classpath:application.properties"})
@EnableScheduling
**@EnableAspectJAutoProxy**
@EnableCaching
@EnableTransactionManagement
public class AppConfig {
...
}

I also have the pom.xml set up to import AspectJ.

But when I try to execute the get method on the ProductService, it doesn't enter the aspect method. I'm not sure how to troubleshoot it now.

Can anyone suggest what could be wrong?

========== Method being advised =============

    @Override
    @Transactional(readOnly = true, rollbackFor = Exception.class)
    public Product get(long productId) {
        logger.entry();


        Product product = null;


        // check the cache first, if it's in cache we'll use that.
        product = (Product) memcachedClient.get(Product.getCacheKey(productId));


        if (product == null)
        {
            product = productRepository.findOne(productId);
            // now we initialise the product.
            if (product != null) {
                Hibernate.initialize(product.getImageSets());
                Hibernate.initialize(product.getTags());
                Hibernate.initialize(product.getVariantOptions());
                Hibernate.initialize(product.getVariants());
                Hibernate.initialize(product.getChildRelationships());
                memcachedClient.set(Product.getCacheKey(productId), 3600, product);
            }
        }

        logger.exit();
        return product;

    }

=========== Class definition =============

package com.ideafactory.mvc.products.common.services;
@Service
public class ProductServiceImpl implements ProductService
{
...
}

============ Adding POM.xml =================

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ideafactory</groupId>
    <artifactId>java_ecommerce</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>


    <properties>
        <java.version>1.7</java.version>
        <junit.version>4.11</junit.version>
        <log4j.version>2.0-rc2</log4j.version>
        <spring.version>4.0.6.RELEASE</spring.version>
        <spring-data-jpa.version>1.7.0.RELEASE</spring-data-jpa.version>
        <spring-security.version>3.2.0.RELEASE</spring-security.version>
        <hibernate.version>4.3.6.Final</hibernate.version>
        <aspectj.version>1.8.2</aspectj.version>
        <mysql.version>5.1.26</mysql.version>
        <jackson-json.version>2.3.1</jackson-json.version>
        <jackson-mapper-asl.version>1.9.13</jackson-mapper-asl.version>
        <commons-dbcp.version>1.4</commons-dbcp.version>
        <commons-lang3.version>3.1</commons-lang3.version>
        <commons-io.version>2.4</commons-io.version>
        <commons-upload.version>1.3.1</commons-upload.version>
        <jmagick.version>6.6.9</jmagick.version>
        <jadira.core.version>3.2.0.GA</jadira.core.version>
        <joda-money-version>0.9.1</joda-money-version>

        <!-- Cache -->
        <spy-memcache-verion>2.11.4</spy-memcache-verion>
    </properties>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>

    <dependencies>
        <!-- Logging dependencies -->

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-jcl</artifactId>
            <version>${log4j.version}</version>
        </dependency>

         <dependency>
             <groupId>org.apache.logging.log4j</groupId>
             <artifactId>log4j-slf4j-impl</artifactId>
             <version>2.0</version>
         </dependency>

        <!-- Spring dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- Spring Data JPA dependencies -->

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>${spring-data-jpa.version}</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <!-- SpringSecurity dependencies -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>${spring-security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${spring-security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring-security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>${spring-security.version}</version>
        </dependency>

        <!-- Dependencies required for AOP Programming -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>${aspectj.version}</version>
        </dependency>


        <!-- Testing dependencies -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- DB dependencies -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>

        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>${commons-dbcp.version}</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson-json.version}</version>
        </dependency>

        <!-- Jackson JSON Mapper -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>${jackson-mapper-asl.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.3</version>
        </dependency>

        <!-- Web dependencies -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>compile</scope>
        </dependency>


        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>

        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>jstl-impl</artifactId>
            <version>1.2</version>
            <exclusions>
                <exclusion>
                    <artifactId>servlet-api</artifactId>
                    <groupId>javax.servlet</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>jsp-api</artifactId>
                    <groupId>javax.servlet.jsp</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>jstl-api</artifactId>
                    <groupId>javax.servlet.jsp.jstl</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-extras</artifactId>
            <version>3.0.3</version>
        </dependency>

        <!-- Velocity for email templates -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-tools</artifactId>
            <version>2.0</version>
        </dependency>



        <!-- Validations -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.1.1.Final</version>
        </dependency>
        <dependency>
            <groupId>commons-validator</groupId>
            <artifactId>commons-validator</artifactId>
            <version>1.4.0</version>
        </dependency>


        <!-- dependency on SOLR data -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-solr</artifactId>
            <version>1.2.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-solrj</artifactId>
            <version>4.9.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-core</artifactId>
            <version>4.9.0</version>
        </dependency>

        <!-- This is for spring JPA framework auditing -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time-hibernate</artifactId>
            <version>1.3</version>
        </dependency>

        <dependency>
            <groupId>org.joda</groupId>
            <artifactId>joda-money</artifactId>
            <version>${joda-money-version}</version>
        </dependency>
        <dependency>
            <groupId>org.jadira.usertype</groupId>
            <artifactId>usertype.core</artifactId>
            <version>${jadira.core.version}</version>
        </dependency>

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>${commons-upload.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons-io.version}</version>
        </dependency>

        <dependency>
            <groupId>org.im4java</groupId>
            <artifactId>im4java</artifactId>
            <version>1.4.0</version>
        </dependency>

        <!-- Cache Configuration -->
        <dependency>
            <groupId>net.spy</groupId>
            <artifactId>spymemcached</artifactId>
            <version>${spy-memcache-verion}</version>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>${spring.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

Your pointcut is trying to advise the Interface execution which IIRC is not possible (ie: the interface is not the code that is executed). You should be advising an implementation. Additionally, as suggested by @Angad, you may need to also annotate it with @Component such that Spring actually autoscans for it (unless you have a custom auto detect filter). I do not believe that Spring will automatically detect/instantiate @Aspect beans.

So you have a couple of options.

1: Build your pointcut to actually advise the implementation itself:

@AfterReturning(
        pointcut = "execution(* com.ideafactory.mvc.products.common.services.ProductServiceImpl.get(..))",
        returning= "result")
public void logAfterReturning(JoinPoint joinPoint, Object result) {
    logger.debug("Executed point cut");
    logger.debug(result);
}

2: Write your pointcut to advise the interface and any classes that implement it (note the + symbol at the end of the interface name):

@AfterReturning(
        pointcut = "execution(* com.ideafactory.mvc.products.common.services.ProductService+.get(..))",
        returning= "result")
public void logAfterReturning(JoinPoint joinPoint, Object result) {
    logger.debug("Executed point cut");
    logger.debug(result);
}

I see that you want to use AOP to define caching on methods. If so you may want to take a look at Simple Spring Memcached . It provides caching in memcached through custom annotations ( @ReadThroughSingleCache, @UpdateMultiCache, @ReadThroughAssignCache, ... ) or Spring Cache annotations ( @Cacheable ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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