How to configure AOP in Spring -



How to configure AOP in Spring -

i haven't used aop before in spring before, i"m trying simple illustration working application.

this code used:

/** * product cache aspect manages 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 think have configured correctly:

@configuration @componentscan(basepackages = {"com.ideafactory"}) @propertysource(value = {"classpath:application.properties"}) @enablescheduling **@enableaspectjautoproxy** @enablecaching @enabletransactionmanagement public class appconfig { ... }

i have pom.xml set import aspectj.

but when seek execute method on productservice, doesn't come in aspect method. i'm not sure how troubleshoot now.

can suggest wrong?

========== method beingness advised =============

@override @transactional(readonly = true, rollbackfor = exception.class) public product get(long productid) { logger.entry(); product product = null; // check cache first, if it's in cache we'll utilize that. product = (product) memcachedclient.get(product.getcachekey(productid)); if (product == null) { product = productrepository.findone(productid); // initialise 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(); homecoming 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 info 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 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 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 info --> <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> <!-- 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 trying advise interface execution iirc not possible (ie: interface not code executed). should advising implementation. additionally, suggested @angad, may need annotate @component such spring autoscans (unless have custom auto observe filter). not believe spring automatically detect/instantiate @aspect beans.

so have couple of options.

1: build pointcut advise 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 pointcut advise interface , classes implement (note + symbol @ end of 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); }

spring spring-mvc

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -