1

メソッド getFirstName() が User オブジェクトで呼び出されたときに実行されるアスペクトを定義しました。しかし、それは起こりません。

私のアスペクトクラス:

@Component
@Aspect
public class JpaGetFirstNameAspect {

  @Pointcut("execution(* de.playground.model.User.getFirstName(..))")
  public void pointCutSetFirstName() {
  }

  @Before("pointCutGetFirstName()")
  public void beforeGetFirstName(JoinPoint joinPoint) {
    System.out.println(">>>> Before retrieving first name ... " + joinPoint.getSignature().getName());  
  }

  @After("pointCutGetFirstName()")
  public void afterGetFirstName() {
    System.out.println(">>>> After execution of getFirstName method ... ");
  }

  @AfterReturning(pointcut = "pointCutGetFirstName()", returning = "firstName")
  public void afterReturningGetFirstName(JoinPoint joinPoint, String firstName) {
    System.out.println("<<<< " + joinPoint.getSignature().getName());
    System.out.println("<<<< returned first name of user " + firstName);
  }

 @AfterThrowing(pointcut = "pointCutGetFirstName()", throwing = "exc")
  public void afterThrowingGetFirstName(Exception exc) {
    System.out.println("|||| Retrieved Exception from getFirstName method ... " + exc.toString());
  }
}

私のapplication-context.xml:

<beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"        xmlns:tx="http://www.springframework.org/schema/tx"     xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="
        http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx     http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop.xsd
       ">

  <context:annotation-config />
  <context:load-time-weaver />
  <context:component-scan base-package="de.playground.service" />

</beans>    

私のテストクラス:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/test/resources/META-INF/spring/application-context.xml" })
public class AspectIntegrationTest {

  @Autowired
  private GenericApplicationContext context;

  private ClassPathXmlApplicationContext ctx;

  public AspectIntegrationTest() {

  }


  @AfterClass
  public static void tearDownClass() {

  }

  @BeforeClass
  public static void setUpClass() {
  }

  @Before
  public void setUp() throws Exception {
    ctx = new ClassPathXmlApplicationContext();
  }

  @After
  public void tearDown() {

  }

  @Test
  public void testHijackingUser() {

    User user1 = new User();
    user1.setCreatedOn(new Date());
    user1.setLastModified(new Date());
    user1.setFirstName("Max");
    user1.setLastName("Mustermann");
    user1.setUsername("max.mustermann");
    user1.setPassword("start123");
    System.out.println(">user 1 = " + user1.getFirstName());

    assertNotNull(user1);
  }
}

私の aop.xml:

<!DOCTYPE aspectj PUBLIC
    "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
    <weaver options="-Xset:weaveJavaxPackages=true -verbose -showWeaveInfo">
        <!-- only weave classes in this package -->
        <include within="de.playground.service.*"     />  
    </weaver>
    <aspects>
        <!-- use only this aspect for weaving -->
        <aspect     name="de.playground.aspect.JpaGetFirstNameAspect" />
    </aspects>
</aspectj>

私の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>
  <parent>
    <groupId>de.playground.platform</groupId>
    <artifactId>playground-dev</artifactId>
    <version>0.0.2-SNAPSHOT</version>
  </parent>
  <artifactId>playground-script</artifactId>
    <dependencies>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>${version.aspectj}</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>${version.aspectj}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${version.spring}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${version.spring}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${version.spring}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${version.spring}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <properties>
    <version.aspectj>1.8.5</version.aspectj>
    <version.spring>4.1.6.RELEASE</version.spring>
  </properties>
  <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <configuration>
            <forkMode>once</forkMode>
            <argLine>-javaagent:"${settings.localRepository}/org/springframework/spring-instrument/${version.spring}/spring-instrument-${version.spring}.jar"</argLine>
            <useSystemClassloader>true</useSystemClassloader>
          </configuration>
        </plugin>
      </plugins>
    </build>
</project>
4

1 に答える 1

0

動作しました。更新された pom.xml

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <forkMode>once</forkMode>
          <argLine>-javaagent:"${settings.localRepository}/org/springframework/spring-instrument/${version.spring}/spring-instrument-${version.spring}.jar"</argLine>
          <useSystemClassloader>true</useSystemClassloader>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.7</version>
        <configuration>
          <showWeaveInfo>true</showWeaveInfo>
          <source>1.7</source>
          <target>1.7</target>
          <Xlint>ignore</Xlint>
          <complianceLevel>1.7</complianceLevel>
          <encoding>UTF-8</encoding>
          <verbose>false</verbose>
          <aspectLibraries>
            <aspectLibrary>
              <groupId>org.springframework</groupId>
              <artifactId>spring-aspects</artifactId>
            </aspectLibrary>
          </aspectLibraries>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>test-compile</goal>
            </goals>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${version.aspectj}</version>
          </dependency>
          <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>${version.aspectj}</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
于 2015-05-20T15:48:32.490 に答える