Spring AOP例子
2015-10-03 23:01
549 查看
1、建项目 springAOP/cn.google.spring.aop
导入Person.java,Person.hbm.xml,hibernate.cfg.xml
2、建接口 PersonDao.java
3、建实现类 PersonDaoImpl,继承Hibernate 的工具类
Hibernate 的工具类:
4、建 MyTransaction.java
5、建 applicationContext.xml,导入springAOP命名空间
6、导入 springAOP 的jar包
在 spring/spring-framework-2.5.5/lib/aspectj 中两个jar包导入
分别是:aspectjrt.jar
aspectjweaver.jar
7、在 5 中的applicationContex.xml 中
execution(public * * (..)) 所有的公共方法
execution(* set* (..)) 以set开头的任意方法
execution(* com.xyz.service.AccountService.* (..)) com.xyz.service.AccountService 类中的所有方法
execution(* com.xyz.service.*.* (..)) com.xyz.service 包中的所有的类的所有的方法
execution(* com.xyz.service..*.* (..)) com.xyz.service 包及子包中的所有的类的所有的方法
execution(* cnitcast.spring.sh..*.* (String, ?, Integer)) com.xyz.service 包中的所有的类的有三个参数:
第一个参数为String,
第二个参数为任意类型,
第三个参数为Integer类型的方法。
8、测试类PersonTest.java
导入Person.java,Person.hbm.xml,hibernate.cfg.xml
public class Person { private long pid; private String pname; private String psex; 封装…… }
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="cn.heima.i_aop.Person"> <id name="pid" column="id" length="200" type="java.lang.Long"> <generator class="increment"></generator> </id> <property name="pname" column="pname" length="20" type="java.lang.String"></property> <property name="psex" column="psex" length="10" type="java.lang.String"></property> </class> </hibernate-mapping>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="connection.url"> jdbc:mysql://localhost:3306/my001 </property> <property name="hbm2ddl.auto">update</property> <property name="show_sql">true</property> <property name="current_session_context_class">thread</property> <mapping resource="cn/heima/i_aop/User.hbm.xml" /> </session-factory> </hibernate-configuration>
2、建接口 PersonDao.java
public interface PersonDao{ public void savePerson(Person person); }
3、建实现类 PersonDaoImpl,继承Hibernate 的工具类
public class PersonDaoImpl extends HibernateUtils implements PersonDao{ @Override public void savePerson(Person person){ sessionFactory.getCurrentSession().save(person); } }
Hibernate 的工具类:
public class HibernateUtils{ public static SessionFactory sessionFactory; static{ Configuration configuration = new Configuration(); configuration.configure("/spring_sh/springAOP/cn/itcast/spring/hibernate.cfg.xml"); sessionFactory = configuration.buildSessionFactory(); } }
4、建 MyTransaction.java
public class MyTransaction extends HibernateUtils{ private Transaction transaction; // 通过该参数,获得目标方法的一些信息 public void beginTransaction(JoinPoint joinPoint){ s.o.p("bbb"); transaction = sessionFactory.getCurrentSession().beginTransaction(); } public void commit(Object var){ s.o.p(var) transaction.commit(); } public void finallyMethod(){ s.o.p("finally method"); } // 异常通知 public void throwingMethod(Throwable ex){ s.o.p(ex.getMessage()); } // 环绕通知 public void aroundMethod(ProceedingJoinPoint jointPoint) throws Throwable{ jointPoint.proceed(); // 调用目标方法 s.o.p("aa"); } }
5、建 applicationContext.xml,导入springAOP命名空间
<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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> </beans>
6、导入 springAOP 的jar包
在 spring/spring-framework-2.5.5/lib/aspectj 中两个jar包导入
分别是:aspectjrt.jar
aspectjweaver.jar
7、在 5 中的applicationContex.xml 中
<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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 导入目标类,导入切面 --> <bean id="personDao" class="cn.itcast.spring.aop.sh.PersonDaoImpl"></bean> <bean id="myTransaction" class="cn.itcast.spring.aop.sh.MyTransaction"></bean> <!-- aop的配置 --> <aop:config> <!-- 切入点表达式:定位方法用,有的方法需要切面配合,有的不需要 --> <aop:aspect ref="myTransaction"> <aop:pointcut expression="execution(* cn.itcast.spring.aop.sh.PersonDaoImpl.*(..))" id="perform"/> <aop:before method="beginTransaction" pointcut-ref="perform"/> <aop:after-returning method="commit" pointcut-ref="perform" returning="var"/> </aop:aspect> </aop:config> </beans>
execution(public * * (..)) 所有的公共方法
execution(* set* (..)) 以set开头的任意方法
execution(* com.xyz.service.AccountService.* (..)) com.xyz.service.AccountService 类中的所有方法
execution(* com.xyz.service.*.* (..)) com.xyz.service 包中的所有的类的所有的方法
execution(* com.xyz.service..*.* (..)) com.xyz.service 包及子包中的所有的类的所有的方法
execution(* cnitcast.spring.sh..*.* (String, ?, Integer)) com.xyz.service 包中的所有的类的有三个参数:
第一个参数为String,
第二个参数为任意类型,
第三个参数为Integer类型的方法。
8、测试类PersonTest.java
public class PersonTest{ @Test public void test(){ ApplicationContext context = new ClassPathXmlApplicationContext("cn/itcast/spring/aop/sh/applicationContext.xml"); PersonDaoImpl personDao = (PersonDaoImpl)context.getBean("personDao"); Person person = new Person(); person.setPname("aaas"); personDao.savePerson(person); } }
相关文章推荐
- java中包装类及装箱拆箱
- [深入理解Java虚拟机]第一章实战 自己编译JDK
- 关于findviewbyid
- Spring AOP概念
- Spring MVC 解读---<context:component-scan/>
- eclipse主题颜色配置
- Spring MVC 解读——<mvc:annotation-driven/>
- 10.03JAVA应用学习知识点
- java中匿名内部类的使用
- Java多线程实验
- Spring DI 继承
- Windows下搭建Eclipse+Android4.0开发环境
- Java对象在JVM中的生命周期
- java中的基本语法
- 如何快速构建基于Spring4.0的Rest API(攻略)
- java读取Excel文件
- Spring3.2新注解@ControllerAdvice
- Spring 用注解实现IOC控制反转
- Java 数组
- Java设计模式之代理模式