0

我需要对以下配置文件进行哪些更改才能让 maven 使用 Spring Aspects(带有注释的那个)编译我的 Java 7 项目?

pom.xml
webapplicationContext.xml
etc....

这是我的方面类(它捕获NoResultExceptionfromTypedQuery.getSingleResult()并将其转换为空结果值)

package com.example.aspect;

import javax.persistence.NoResultException;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class NullifyNoResultExceptionAspect {

    @Pointcut("execution(public * com.example.dao.impl.*.*(..))")
    public void methodToBeProtected() {}

    @Around("methodToBeProtected()")
    public Object queryToProtect(ProceedingJoinPoint pjp) throws Throwable {
        try {
            return pjp.proceed();
        } catch (NoResultException nre) {
            return null;
        }
    }

}

我正在使用 Eclipse Juno sr1 和 Java 7 EE。
我的构建环境是带有 Nexus 存储库的 Maven (2)。

4

1 回答 1

2

以下对我有用:

  1. aop.xml在目录下创建一个名为:的文件META-INF(与persistence.xml.

  2. 修改以在您的bean 定义webApplicationContext.xml中包含 Spring 方面 bean (在主项目中)。 emfsrc/java/resources

  3. aspectj-maven-plugin您的pom.xml.

  4. 确保告诉 aspectj 插件您要编译 Java 7 代码,否则它将尝试使用默认的 AspectJ-1.6 编织器来编织您的 1.7 代码。

1 - aop.xml

<!DOCTYPE aspectj PUBLIC
        "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>

    <weaver>

        <!-- only weave classes in our application-specific packages -->
        <include within="com.example.*"/>

    </weaver>

    <aspects>

        <!-- weave in just this aspect -->        
        <aspect name="com.example.aspect.NullifyNoResultExceptionAspect"/>

    </aspects>

  </aspectj>

2 - WebApplicationContext.xml
你的 emf bean 应该是这样的:

<bean id="emf"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="za.org.serviceseta.model" />
    <property name="jpaVendorAdapter">
        <bean
            class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="${hibernate.showsql}" />
            <property name="generateDdl" value="${jpa.generateDdl}" />
            <property name="databasePlatform"
                value="org.hibernate.dialect.MySQLDialect" />
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>

    </property>
    <property name="loadTimeWeaver">
        <bean
            class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    </property>
</bean>

_注意,有些教程告诉你包含<context:load-time-weaver />,但这仅适用于toplink,不适用于spring;春想见豆。

3 - pom.xml - 第 1 部分:告诉jetty-maven-plugin使用 spring javaagent

<build>
    <plugins>
        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>9.0.0.M5</version>
            <configuration>
                <forkMode>once</forkMode>
                <argLine>
                     -javaagent:"${settings.localRepository}/org/springframework/spring-instrument/${spring.framework.version}/spring-instrument-${spring.framework.version}.jar"
                </argLine>
....

3 - pom.xml - 第 2 部分:aspectj-maven-plugin

<project>
....
    <properties>
        <aspectj.version>1.7.2</aspectj.version>
        <!-- note that AspectJ warns you that it prefers 1.6.11, but this works -->
        .....

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.4</version>
    <configuration>
        <showWeaveInfo>true</showWeaveInfo>
        <source>1.7</source>     <!-- need these for Java 7! -->
        <target>1.7</target>     <!-- need these for Java 7! -->
        <Xlint>ignore</Xlint>
        <complianceLevel>1.7</complianceLevel>  <!-- need these for Java 7! -->
        <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>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>3.2.1.RELEASE</version>
        </dependency>
    </dependencies>
</plugin>

3 - pom.xml 第 3 部分:依赖项

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>${aspectj.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>3.2.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-agent</artifactId>
    <version>2.5.6</version>
</dependency>

如果我遗漏了什么,请随时发表评论。

于 2013-02-27T17:46:31.533 回答