spring mvc - Aspect Auditing Field changes -


i'm trying capture filed changes using aspect auditing posibilities , custom anotations, can't old value. here code:

public aspect fieldauditaspect {  @autowired activityservice activityservice; @autowired userservice userservice;  pointcut auditfield(object t, object value): set(@ge.shemo.model.client.aopaudit * *) && args(value) && target(t);  before (object target, object newvalue): auditfield(target, newvalue) {     fieldsignature sig = (fieldsignature) thisjoinpoint.getsignature();     field field = sig.getfield();     field.setaccessible(true);      object oldvalue;     try {         oldvalue = field.get(target);     } catch (illegalaccessexception e) {         throw new runtimeexception("failed create audit action", e);     }      system.out.println("changed " + oldvalue + " " + newvalue);    }  } 

this custom annotation use mapping desired filed capture changes:

@retention(runtime) @target(value = { type, field}) public @interface aopaudit {  } 

here 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>ge.shemo</groupid> <artifactid>shemoproject</artifactid> <version>1.0</version> <packaging>war</packaging>   <properties>     <java.version>1.8</java.version>     <junit.version>4.11</junit.version>     <slf4j.version>1.7.5</slf4j.version>     <logback.version>1.0.13</logback.version>     <spring.version>4.1.9.release</spring.version>     <aspectj.version>1.8.2</aspectj.version>     <java.source>1.8</java.source>     <java.target>1.8</java.target>     <spring-security.version>3.2.0.release</spring-security.version>     <hibernate.version>5.0.1.final</hibernate.version>     <jackson-json.version>2.3.1</jackson-json.version>     <commons-dbcp.version>1.2.2</commons-dbcp.version>     <commons-lang3.version>3.1</commons-lang3.version> </properties>  <build>     <pluginmanagement>         <plugins>             <plugin>                 <artifactid>maven-compiler-plugin</artifactid>                 <version>3.5.1</version>                 <configuration>                     <source>${java.source}</source>                     <target>${java.target}</target>                     <encoding>utf-8</encoding>                 </configuration>             </plugin>             <plugin>                 <groupid>org.apache.maven.plugins</groupid>                 <artifactid>maven-resources-plugin</artifactid>                 <version>3.0.1</version>                 <configuration>                     <encoding>utf-8</encoding>                 </configuration>             </plugin>             <plugin>                 <groupid>org.apache.maven.plugins</groupid>                 <artifactid>maven-source-plugin</artifactid>                 <version>3.0.1</version>             </plugin>             <plugin>                 <groupid>org.codehaus.mojo</groupid>                 <artifactid>aspectj-maven-plugin</artifactid>                 <version>1.8</version>                 <configuration>                     <showweaveinfo>true</showweaveinfo>                     <source>${java.source}</source>                     <target>${java.target}</target>                     <compliancelevel>${java.target}</compliancelevel>                     <encoding>utf-8</encoding>                     <verbose>false</verbose>                     <xnoinline>false</xnoinline>                 </configuration>                 <dependencies>                     <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>                 </dependencies>             </plugin>             <plugin>                 <groupid>org.apache.maven.plugins</groupid>                 <artifactid>maven-surefire-plugin</artifactid>                 <version>2.19.1</version>             </plugin>         </plugins>     </pluginmanagement>     <plugins>         <plugin>             <groupid>org.codehaus.mojo</groupid>             <artifactid>aspectj-maven-plugin</artifactid>             <executions>                 <execution>                     <id>aspectj-weave</id>                     <phase>compile</phase>                     <goals>                         <goal>compile</goal>                     </goals>                 </execution>                 <execution>                     <id>aspectj-weave-test</id>                     <phase>test-compile</phase>                     <goals>                         <goal>test-compile</goal>                     </goals>                     <configuration>                     </configuration>                 </execution>             </executions>         </plugin>         <plugin>             <artifactid>maven-compiler-plugin</artifactid>         </plugin>     </plugins> </build>  <dependencies>     <!-- logging dependencies -->     <dependency>         <groupid>org.slf4j</groupid>         <artifactid>jcl-over-slf4j</artifactid>         <version>${slf4j.version}</version>     </dependency>      <dependency>         <groupid>org.slf4j</groupid>         <artifactid>slf4j-api</artifactid>         <version>${slf4j.version}</version>     </dependency>      <dependency>         <groupid>ch.qos.logback</groupid>         <artifactid>logback-classic</artifactid>         <version>${logback.version}</version>     </dependency>      <!-- spring dependencies -->       <dependency>         <groupid>org.springframework</groupid>         <artifactid>spring-context-support</artifactid>         <exclusions>             <exclusion>                 <groupid>commons-logging</groupid>                 <artifactid>commons-logging</artifactid>             </exclusion>         </exclusions>     </dependency>      <dependency>         <groupid>org.springframework</groupid>         <artifactid>spring-webmvc</artifactid>     </dependency>     <dependency>         <groupid>org.springframework</groupid>         <artifactid>spring-test</artifactid>     </dependency>     <!-- spring data jpa dependencies -->       <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>        <!-- testing dependencies -->     <dependency>         <groupid>junit</groupid>         <artifactid>junit</artifactid>         <version>${junit.version}</version>         <scope>test</scope>     </dependency>      <!-- db dependencies -->       <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>      <dependency>         <groupid>javax.mail</groupid>         <artifactid>mail</artifactid>         <version>1.4.3</version>     </dependency>      <!-- web dependencies -->       <dependency>         <groupid>taglibs</groupid>         <artifactid>standard</artifactid>         <version>1.1.2</version>         <scope>compile</scope>     </dependency>     <dependency>         <groupid>jstl</groupid>         <artifactid>jstl</artifactid>         <version>1.2</version>         <scope>compile</scope>     </dependency>     <dependency>         <groupid>org.hibernate</groupid>         <artifactid>hibernate-core</artifactid>         <version>5.0.1.final</version>     </dependency>        <!-- optional -->      <dependency>         <groupid>org.hibernate</groupid>         <artifactid>hibernate-osgi</artifactid>         <version>5.0.1.final</version>     </dependency>     <dependency>         <groupid>org.hibernate</groupid>         <artifactid>hibernate-envers</artifactid>         <version>5.0.1.final</version>     </dependency>     <dependency>         <groupid>org.hibernate</groupid>         <artifactid>hibernate-c3p0</artifactid>         <version>5.0.1.final</version>     </dependency>     <dependency>         <groupid>org.hibernate</groupid>         <artifactid>hibernate-proxool</artifactid>         <version>5.0.1.final</version>     </dependency>     <dependency>         <groupid>org.hibernate</groupid>         <artifactid>hibernate-infinispan</artifactid>         <version>5.0.1.final</version>     </dependency>     <dependency>         <groupid>org.hibernate</groupid>         <artifactid>hibernate-ehcache</artifactid>         <version>5.0.1.final</version>     </dependency>      <dependency>         <groupid>net.sf.dozer</groupid>         <artifactid>dozer</artifactid>         <version>5.5.1</version>     </dependency>       <dependency>         <groupid>joda-time</groupid>         <artifactid>joda-time</artifactid>         <version>2.8.2</version>     </dependency>     <dependency>         <groupid>commons-lang</groupid>         <artifactid>commons-lang</artifactid>         <version>2.5</version>     </dependency>     <dependency>         <groupid>com.google.code.gson</groupid>         <artifactid>gson</artifactid>         <version>2.3.1</version>     </dependency>       <dependency>         <groupid>commons-fileupload</groupid>         <artifactid>commons-fileupload</artifactid>         <version>1.3.1</version>     </dependency>      <dependency>         <groupid>com.microsoft.sqlserver</groupid>         <artifactid>sqljdbc4</artifactid>         <version>4.0</version>     </dependency>      <dependency>         <groupid>com.googlecode.json-simple</groupid>         <artifactid>json-simple</artifactid>         <version>1.1.1</version>     </dependency>       <dependency>         <groupid>org.json</groupid>         <artifactid>json</artifactid>         <version>20151123</version>     </dependency>     <dependency>         <groupid>javax.servlet</groupid>         <artifactid>servlet-api</artifactid>         <version>2.5</version>     </dependency>     <dependency>         <groupid>javax.servlet</groupid>         <artifactid>javax.servlet-api</artifactid>         <version>3.0.1</version>     </dependency>       <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->     <dependency>         <groupid>org.springframework.data</groupid>         <artifactid>spring-data-jpa</artifactid>         <version>1.7.1.release</version>     </dependency>       <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->     <dependency>         <groupid>org.springframework</groupid>         <artifactid>spring-aspects</artifactid>         <version>4.1.9.release</version>     </dependency>        <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->     <dependency>         <groupid>com.google.guava</groupid>         <artifactid>guava</artifactid>         <version>19.0</version>     </dependency>      <!-- https://mvnrepository.com/artifact/com.sparkpost/sparkpost-lib -->     <dependency>         <groupid>com.sparkpost</groupid>         <artifactid>sparkpost-lib</artifactid>         <version>0.16.1</version>     </dependency>       <!-- scope provided because processor needed compiler -->     <dependency>         <groupid>fr.xebia.extras</groupid>         <artifactid>selma-processor</artifactid>         <version>0.15</version>         <scope>provided</scope>     </dependency>      <!-- real dependency have in binaries -->     <dependency>         <groupid>fr.xebia.extras</groupid>         <artifactid>selma</artifactid>         <version>0.15</version>     </dependency>       <dependency>         <groupid>org.aspectj</groupid>         <artifactid>aspectjrt</artifactid>         <version>${aspectj.version}</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> 

this entity class:

@entity @entitylisteners(auditingentitylistener.class) public class client {  @aopaudit private string firstname; @aopaudit private string lastname; @enumerated(enumtype.string) private gender gender; ..... 

this function update entity:

 public client saveclient(clientdto clientdto) {      dozerbeanmapper mapper = new dozerbeanmapper();     client client = getclient(clientdto.getid); //getting client db      client =  mapper.map(clientdto, client.class); //casting dto entity using dozerbeanmapper      saveclient(client); 

everything works fine except don't old value - old value null. need old value save don't understand why i'm not getting old value.

can tell me i'm doing wrong?

i'm using spring mvc + jpa

let me try explain how supposed work. fieldauditaspect triggered before set operations fields having @aopaudit annotation, , target(t) in pointcut definition means object instance affected aspect.

in case following:

client =  mapper.map(clientdto, client.class); 

this means mapper create new instance of client, has empty field values, , populate corresponding clientdto field values. so null old values taken from.

to work expect should same fetched client object, follows:

client =  mapper.map(clientdto, client); 

client object used here instead of client.class

if should see old field values, present in fetched client object, in combination new one, present in clientdto.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -