使用maven完成spring aop的xml配置

本文介绍了如何使用Maven创建Spring AOP项目,包括设置groupid、artifactid和version,解决因JDK版本问题导致的项目结构不全,调整项目目录结构,修改JDK版本以及在项目中配置pom.xml,创建相关类并定义切入点表达式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先确定本地maven环境配置ok,并与eclipse集成.

NEW->Other->Maven->Maven Project

接下来就是maven的groupid,artifact id,version的填写

groupid:所在项目组的名字,一般是公司url倒置+项目组名

artifactid:简单来说就是项目名

version:版本号可以用默认的

接下来一路按next

生成项目后可以发现项目内容不全,造成这样的原因就是因为在构建maven项目后,默认的jdk是1.5版本的,而你自己的jdk并不是1.5版本的,所以我们要做两件事

1,补充结构,右击项目->Properties->Order and Export,自行补充缺乏的目录.

src/main/java:所有的接口和实现类均放置在这里.

src/main/resources:所有的配置文件均放置在这里

src/main/test:所有的测试类,测试单元均放置在这里

2,修改jdk版本,如果现在写一个接口,再实现这个接口,重写它的某个方法,可以发现@Override失效,这就是因为jdk1.5还不能支持重写抽象类方法的重写标注.

解决方法:右击项目->Properties->Java Build Path->Compiler level修改成自己的jdk版本

最后refresh项目


在完成以上操作后,配置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.fengsigaoju</groupId>
  <artifactId>spring-test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>spring-test</name>
  <url>http://maven.apache.org</url>
  <properties>
       <spring-version>3.2.4.RELEASE</spring-version>
  </properties>
  <dependencies>
   <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>${spring-version}</version>
    </dependency>
    <dependency>
 <groupId>org.aspectj</groupId>
 <artifactId>aspectjweaver</artifactId>
 <version>1.6.8</version>
</dependency>
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
</dependency>
  </dependencies>
  <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<downloadSources>true</downloadSources>
</configuration>
</plugin>
</plugins>
</build>
</project>
每一个dependency中就是一个需要的jar包,具体下载到本地是jar还是war,可以在Packaging中配置

在src/main/java新建一个包为com.hello.aop

新建两个类

AfterSay.java

package com.hello.aop;

public class AfterSay {
  public void aftersay(){
	  System.out.println("说之后");
  }
}

BeforeSay.java

package com.hello.aop;

public class BeforeSay {
 public void beforesay(){
	 System.out.println("说之前");
 }
}

新建com.hello.demo

Say接口:

package com.hello.demo;

public interface Say {
 void say();
}

SayHello.java:

package com.hello.demo;

public class SayHello implements Say {
    private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public void say() {
	System.out.println("hello,"+name);
   }
	
}
在src/main/resources上添加application.xml(有就不需要了)new ->file->application.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd">
 <bean id="say" class="com.hello.demo.SayHello">
 <property name="name" value="fengsigaoju"></property>
 </bean>
 <bean id="bf" class="com.hello.aop.BeforeSay">
 </bean>
<bean id="af" class="com.hello.aop.AfterSay">
</bean>

<aop:config>
   <aop:aspect ref="bf"><!-- 具体哪一个切入类 -->
      <aop:before method="beforesay" pointcut="execution(* com.hello.demo.SayHello.say(..))"></aop:before><!-- method为类的某个方法,ref是哪一个类,pointcut是执行到那边增强(即切入点的表达式,调用该方法增强 -->
   </aop:aspect>
   <aop:aspect ref="af">
   <aop:after method="aftersay" pointcut="execution(* com.hello.demo.SayHello.say(..))"></aop:after><!-- 具体 -->
   </aop:aspect>
</aop:config>
</beans> 

在src/test/java中新建包com.hello.test

新建Test.java

package com.hello.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hello.demo.Say;
public class Test {
	public static void main(String[] args) {
	ApplicationContext context= new ClassPathXmlApplicationContext("application.xml");
	 Say say =(Say)context.getBean("say");
	 say.say();
	}
}

运行可以获得结果

最终项目结构:




说先切入点的表达式,最严格写法:

pointcut="execution (public void execution (public  void com.hello.demo.SayHello.say(..))"

public可以省略

pointcut="execution(public void execution (void com.hello.demo.SayHello.say(..))"

void 表示函数返回值,必须与该函数对应,一般可以用*匹配所有类型

pointcut="execution(public void execution (* com.hello.demo.SayHello.say(..))"

省略某个包(比如增强某个包下面所有的包)

pointcut="execution(public void execution (* com.*.demo.SayHello.say(..))"
指的是增强com.包下面所有的包里面的demo.SayHello.say方法


但是上面一种方式只能省略一层包,要省略所有的可以用*..*

pointcut="execution(public void execution (* *..*.demo.SayHello.say())"

甚至可以像模糊查询一样省略某个包,某个类的部分名字

 pointcut="execution(* com.hello.*emo.SayHello.say(..))"
可以匹配到demo

最后say里面是参数,一般直接写..表示匹配所有参数

对于部分对于一个切面有多个操作的,为了避免大量的写pointcut="exectuin...."这种句型,可以先描写切入点,再进行引用切入点

具体写法可以修改配置文件如下:

<aop:config>
   <aop:pointcut id="pointcut" expression="execution (* com.hello.demo.SayHello.say(..))"/><!-- 一定要放在一开始 -->
   <aop:aspect ref="bf">
   <aop:before method="beforesay" pointcut-ref="pointcut"/>
   </aop:aspect>
   <aop:aspect ref="af">
   <aop:after method="aftersay" pointcut-ref="pointcut"/>
   </aop:aspect>
</aop:config>





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值