aop基于xml开发
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!--配置包扫描-->
<context:component-scan base-package="com.hgd.spring.service"/>
<!--配置通知和增强-->
<bean id="logAdvice" class="com.hgd.spring.advice.LogAdvice"/>
<!--AOP配置-->
<aop:config>
<aop:pointcut id="savePoint" expression="execution(void com.hgd.spring.service.UserServiceImpl.save())"/>
<aop:pointcut id="deletePoint" expression="execution(void com.hgd.spring.service.UserServiceImpl.delete())"/>
<aop:pointcut id="addPoint" expression="execution(void com.hgd.spring.service.UserServiceImpl.add())"/>
<aop:pointcut id="throwssPoint" expression="execution(void com.hgd.spring.service.UserServiceImpl.throwss())"/>
<aop:pointcut id="executoPoint" expression="execution(void com.*.*.*.*ServiceImpl.executo())"/>
<!--前置通知-->
<aop:aspect ref="logAdvice">
<aop:before method="log" pointcut-ref="savePoint"/>
</aop:aspect>
<!--后置通知-->
<aop:aspect ref="logAdvice">
<aop:after-returning method="log" pointcut-ref="deletePoint"/>
</aop:aspect>
<!--最终通知-->
<aop:aspect ref="logAdvice">
<aop:after method="log" pointcut-ref="addPoint"/>
</aop:aspect>
<!--环绕通知-->
<aop:aspect ref="logAdvice">
<aop:around method="log" pointcut-ref="throwssPoint"/>
</aop:aspect>
<!--异常通知-->
<aop:aspect ref="logAdvice">
<aop:after-throwing method="log" pointcut-ref="executoPoint"/>
</aop:aspect>
</aop:config>
</beans>
@Service
public class UserServiceImpl implements UserService {
public void save() {
System.out.println("save...........");
}
public void delete() {
System.out.println("delete......................");
}
public void add() {
System.out.println("add.........................");
}
public void throwss(){
System.out.println("throwss.............................");
}
public void executo() {
if (10/0==0){
return;
}
System.out.println("executo.................");
}
}
public interface UserService {
void save();
void delete();
void add();
void throwss();
void executo();
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class AopDemo {
@Autowired
private UserService userService;
@Test
public void aop1(){
userService.save();
userService.delete();
userService.add();
userService.throwss();
//userService.executo();
}
}
aop注解开发
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
">
<context:component-scan base-package="com.hgd.spring"/>
<!--解析aop配置-->
<aop:aspectj-autoproxy/>
</beans>
@Component("logAdvice")
@Aspect
public class LogAdvice {
@Before("LogAdvice.fn()")
@After("LogAdvice.fn()")
@AfterThrowing("LogAdvice.fn()")
@Around("LogAdvice.fn()")
@AfterReturning("LogAdvice.fn()")
public void login(){
System.out.println("aop..........................");
}
@Pointcut(value = "execution(* com.hgd.spring.service.UserServiceImpl.*())")
public void fn(){
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring.xml")
public class Demo {
@Autowired
private UserService userService;
@Test
public void demo(){
userService.executo();
userService.save();
userService.delete();
userService.add();
userService.throwss();
}
}
aop纯注解开发
@Configuration
@ComponentScan(basePackages = "com.hgd.spring")
@EnableAspectJAutoProxy
public class SpringConfiguration {
}
@Component("logAdvice")
@Aspect
public class LogAdvice {
@Before("com.hgd.spring.advice.LogAdvice.fn()")
@After("com.hgd.spring.advice.LogAdvice.fn()")
@AfterThrowing("com.hgd.spring.advice.LogAdvice.fn()")
@Around("com.hgd.spring.advice.LogAdvice.fn()")
@AfterReturning("com.hgd.spring.advice.LogAdvice.fn()")
public void login(){
System.out.println("aop..........................");
}
@Pointcut(value = "execution(* com.hgd.spring.service.UserServiceImpl.*())")
public void fn(){
}
}
public interface UserService {
void save();
void delete();
void add();
void throwss();
void executo();
}
@Service
public class UserServiceImpl implements UserService {
public void save() {
System.out.println("save...........");
}
public void delete() {
System.out.println("delete......................");
}
public void add() {
System.out.println("add.........................");
}
public void throwss(){
System.out.println("throwss.............................");
}
public void executo() {
if (10/0==0){
return;
}
System.out.println("executo.................");
}
}