1.建立工程后,首先所需要的导入jar
----------------------------------------------------------------------------------------------------------------------
2.导入jar后,就该配置xml 注意下面红的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:aop="/schema/aop"
xmlns:jms="/schema/jms"
xmlns:context="/schema/context"
xsi:schemaLocation="
/schema/beans /schema/beans/spring-beans-2.5.xsd
/schema/jms /schema/jms/spring-jms-2.5.xsd
/schema/aop /schema/aop/spring-aop-2.5.xsd
/schema/context /schema/context/spring-context-2.5.xsd">
<aop:aspectj-autoproxy/>
</beans>
----------------------------------------------------------------------------------------------------------------------
3.创建接口
package spring.aop.service;
public interface PeopleService {
public void save(String name);
public void update(Integer id,String name);
public String getPeopleName(Integer id);
}
----------------------------------------------------------------------------------------------------------------------
4.实现累(业务bean)
package spring.aop.impl;
import spring.aop.service.PeopleService;
spring out 是什么意思public class PeopleServiceBean implements PeopleService{
@Override
public String getPeopleName(Integer id) {
System.out.println("this is getPeopleName method");
return "XXX";
}
@Override
public void save(String name) {
System.out.println("this is save method");
}
@Override
public void update(Integer id, String name) {
System.out.println("this is update method");
}
}
5.接下来了解下注解符号
1.第一步首先声明切面类
package spring.aop.interceptor;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class MyInterceptor {
2.声明切入点 ---拦截
@Pointcut("execution(* spring.aop.annotation..*.*(..))")
public void anyMethod(){}//设置切入点(也就是说这个anyMethod()方法来代替切入的方法)
//切入点:第一个*指的是对于任意返回类型,接下来spring.aop.annotation 包
..指的是:本包或者字包里面,*这里指的是任意类,然后接下来*指的是任意方法(..)指的是任意参数
3.设置前置通知
@Before("anyMethod()")
//拦截方法,在执行前先执行anyMethod()
public void doAccessCheck(String name){
//这个是前置通知的方法,在调用bean实例方法前
System.out.println("这是前置通知"+name);
}
}
6.编写测试类
import org.junit.Test;
import t.ApplicationContext;
import t.support.AbstractApplicationContext;
import t.support.ClassPathXmlApplicationContext;
import spring.aop.service.PeopleService;
public class Junit4_test {
@Test
public void test(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("l");
PeopleService peopleService=(Bean("peopleService");
peopleService.save("xixi");
// PeopleName(11);
}
}
实验总结:
过程中遇到问题
org.springframework.beans.factory.BeanDefinitionStoreException: IOException
该问题是由于建立该项目是Java项目,l直接放在根目录里面,没有放在src里面,导致出错,如果是web项目是放在webroot里面
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论