百度文库网站立足岗位做奉献,企业网站如何做优化,王烨桦,seo教程最新目录 半注解形式#xff1a;
业务层接口实现类#xff1a;
编写切面类#xff1a;
在配置文件里面唯一需要加的#xff1a;
测试类#xff1a;
全注解形式#xff1a;
不要配置文件#xff0c;改为配置类#xff1a;
同样的业务层接口实现类#xff1a;
同样的…目录 半注解形式
业务层接口实现类
编写切面类
在配置文件里面唯一需要加的
测试类
全注解形式
不要配置文件改为配置类
同样的业务层接口实现类
同样的自定义切面类
测试类改为不加载配置文件加载配置类 半注解形式
业务层接口实现类
public interface UserService {public void save();
}Service
public class UserServiceImpl implements UserService {//保存方法
// Overridepublic void save() {System.out.println(业务层方法执行了);//int i5/0;}
}
编写切面类
/*** Component // 把该类交给IOC去管理* Aspect // 声明是切面类 aop:aspect refmyXmlAspect*/
Component
Aspect
public class MyAnnoAspect {/*** 通知的方法*/// Before(value 切入点的表达式)Before(execution(public void com.qcby.demo1.UserServiceImpl.save()))public void log(){System.out.println(前置方法执行了);}
}
在配置文件里面唯一需要加的
1、开启注解扫描
2、开启aop自动代理
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd!--开启注解扫描 扫描所有被spring管理的类--context:component-scan base-packagecom.qcby/!--开启AOP注解支持--aop:aspectj-autoproxy/
/beans
测试类
/*** 测试类* 测试AOP的增强* RunWith(SpringJUnit4ClassRunner.class) 声明为测试单元* ContextConfiguration(classpath:applicationContext.xml) 加载配置文件*/
RunWith(SpringJUnit4ClassRunner.class)
ContextConfiguration(classpath:applicationContext.xml)
public class TestDemo1 {Autowiredprivate UserService userService;Testpublic void run(){userService.save();}
}
运行结果 全注解形式
不要配置文件改为配置类
Configuration // 配置类
ComponentScan(value com.qcby) // 扫描包
EnableAspectJAutoProxy // 开启自动代理 aop:aspectj-autoproxy /
public class SpringConfig {
}
同样的业务层接口实现类
public interface UserService {public void save();
}Service
public class UserServiceImpl implements UserService {//保存方法
// Overridepublic void save() {System.out.println(业务层方法执行了);//int i5/0;}
}
同样的自定义切面类
/*** Component // 把该类交给IOC去管理* Aspect // 声明是切面类 aop:aspect refmyXmlAspect*/
Component
Aspect
public class MyAnnoAspect {/*** 通知的方法*/// Before(value 切入点的表达式)Before(execution(public void com.qcby.demo1.UserServiceImpl.save()))public void log(){System.out.println(前置方法执行了);}
}
测试类改为不加载配置文件加载配置类
/*** 测试类* 测试AOP的增强* RunWith(SpringJUnit4ClassRunner.class) 声明为测试单元* ContextConfiguration(classpath:applicationContext.xml) 加载配置文件*/
RunWith(SpringJUnit4ClassRunner.class)
ContextConfiguration(classes SpringConfig.class)
public class TestDemo1 {Autowiredprivate UserService userService;Testpublic void run(){userService.save();}
}
运行结果