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>