您的位置:首页 > 编程语言 > Java开发

Spring AOP例子

2015-10-03 23:01 549 查看
1、建项目 springAOP/cn.google.spring.aop

导入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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: