之前我在Spring之前没有使用过AOP,所以我只想尝试一个使用我的应用程序的简单示例。
这是我使用的代码:
/**
* 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);
}
}
我认为我已正确配置:
@Configuration
@ComponentScan(basePackages = {"com.ideafactory"})
@PropertySource(value = {"classpath:application.properties"})
@EnableScheduling
**@EnableAspectJAutoProxy**
@EnableCaching
@EnableTransactionManagement
public class AppConfig {
...
}
我也设置了pom.xml来导入AspectJ。
但是当我尝试在ProductService上执行get方法时,它不会进入aspect方法。我现在还不确定如何排除故障。
任何人都可以提出可能出错的建议吗?
==========建议的方法=============
@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;
}
===========班级定义=============
package com.ideafactory.mvc.products.common.services;
@Service
public class ProductServiceImpl implements ProductService
{
...
}
============添加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>
答案 0 :(得分:0)
你的切入点试图告诉接口执行哪个IIRC是不可能的(即:接口不是执行的代码)。您应该建议实施。此外,正如@Angad所建议的那样,您可能还需要使用@Component
对其进行注释,以便Spring实际对其进行自动扫描(除非您有自定义自动检测过滤器)。我不相信Spring会自动检测/实例化@Aspect bean。
所以你有几个选择。
1:构建切入点以实际建议实现本身:
@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:编写切入点以建议接口和实现它的任何类(请注意接口名称末尾的+
符号):
@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);
}
答案 1 :(得分:0)
我看到你想使用AOP来定义方法的缓存。如果是这样,您可能需要查看Simple Spring Memcached。它通过自定义注释(@ReadThroughSingleCache, @UpdateMultiCache, @ReadThroughAssignCache, ...
)或Spring Cache注释(@Cacheable
)在memcached中提供缓存。