顺 达 平 台 代 理 怎么做?做顺 达 平 台 代 理 待遇如何?【Q和V同:67719 】【顶尖待遇一步到位】-注-册-.-充-值-.-代-理-.-开-户,无需打开页面.欢迎随时咨询!理解了前面动态代理对象的原理之后,其实还是有很多不足之处,因为如果在项目中有20多个类,每个类有100多个方法都需要判断是不是要开事务,那么方法调用那里会相当麻烦。
spring中的AOP很好地解决了这个问题,通过 execution表达式 指定哪些包中的那些类 哪些方法 用到事务
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(* cn.itcast.spring.sh..*.*(String,?,Integer)) cn.itcast.spring.sh包及子包中所有的类的有三个参数
第一个参数为String,第二个参数为任意类型,
第三个参数为Integer类型的方法
springAOP的具体加载步骤:
1、当spring容器启动的时候,加载了spring的配置文件
2、为配置文件中所有的bean创建对象
3、spring容器会解析aop:config的配置
1、解析切入点表达式,用切入点表达式和纳入spring容器中的bean做匹配
如果匹配成功,则会为该bean创建代理对象,代理对象的方法=目标方法+通知
如果匹配不成功,不会创建代理对象
4、在客户端利用context.getBean获取对象时,如果该对象有代理对象则返回代理对象,如果代理对象,则返回目标对象
顺 达 平 台 代 理 怎么做?做顺 达 平 台 代 理 待遇如何?【Q和V同:67719 】【顶尖待遇一步到位】-注-册-.-充-值-.-代-理-.-开-户,无需打开页面.欢迎随时咨询!说明:如果目标类没有实现接口,则spring容器会采用cglib的方式产生代理对象,如果实现了接口,会采用jdk的方式
实例:
引入包
aspectjrt.jar
aspectjweaver.jar
Person.java
1 package cn.itcast.sh.aop;
2
3
4 import java.io.Serializable;
5
6
7 /**
8 * 对象的序列化的作用:让对象在网络上传输,以二进制的形式传输
9 * @author Think
10 * Serializable标示接口
11 */
12 public class Person implements Serializable{
13 private Long pid;
14 private String pname;
15
16 public Person(){}
17
18 public Person(String pname){
19 this.pname = pname;
20 }
21
22 public Long getPid() {
23 return pid;
24 }
25 public void setPid(Long pid) {
26 this.pid = pid;
27 }
28 public String getPname() {
29 return pname;
30 }
31 public void setPname(String pname) {
32 this.pname = pname;
33 }
34 public String getPsex() {
35 return psex;
36 }
37 public void setPsex(String psex) {
38 this.psex = psex;
39 }
40 private String psex;
41 }
View Code
1 package cn.itcast.sh.aop;
2
3 public interface PersonDao {
4 public void savePerson(Person person);
5 }
View Code
1 package cn.itcast.sh.aop;
2
3 import org.hibernate.Session;
4
5 import cn.itcast.hibernate.sh.utils.HiberanteUtils;
6
7 public class PersonDaoImpl extends HiberanteUtils implements PersonDao{
8
9 @Override
10 public void savePerson(Person person) {
11 Session session=sessionFactory.getCurrentSession();
12 session.save(person);
13 }
14
15 }
View Code
1 package cn.itcast.sh.aop;
2
3 import org.hibernate.Transaction;
4
5 import cn.itcast.hibernate.sh.utils.HiberanteUtils;
6
7 public class MyTransaction extends HiberanteUtils{
8
9 private Transaction tran;
10 public void beginTransaction()
11 {
12 tran=sessionFactory.getCurrentSession().beginTransaction();
13 }
14 public void commit()
15 {
16 tran.commit();
17 }
18 }
View Code
hibernate.cfg.xml
1 <?xml version=‘1.0‘ encoding=‘utf-8‘?>
2 <!DOCTYPE hibernate-configuration PUBLIC
3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5 <hibernate-configuration>
6 <!--
7 一个session-factory只能连接一个数据库
8 -->
9 <session-factory>
10 <!--
11 数据库的用户名
12 -->
13 <property name="connection.username">root</property>
14
15 <property name="connection.driver_class">
16 com.mysql.jdbc.Driver
17 </property>
18
19 <!--
20 密码
21 -->
22 <property name="connection.password">friends</property>
23 <!--
24 url
25 -->
26 <property name="connection.url">
27 jdbc:mysql://localhost:3306/hibernate_basic
28 </property>
29 <!--
30 作用:根据持久化类和映射文件生成表
31 validate
32 create-drop
33 create
34 update
35 -->
36 <property name="hbm2ddl.auto">update</property>
37
38 <property name="hibernate.dialect">
39 org.hibernate.dialect.MySQLInnoDBDialect
40 </property>
41 <!-- 用于配置当前线程用的 -->
42 <property name="current_session_context_class">thread</property>
43 <!--
44 显示hibernate内部生成的sql语句
45 -->
46 <property name="show_sql">true</property>
47 <property name="format_sql">true</property>
48 <mapping resource="cn/itcast/sh/aop/Person.hbm.xml" />
49
50 </session-factory>
51 </hibernate-configuration>
View Code
顺 达 平 台 代 理 怎么做?做顺 达 平 台 代 理 待遇如何?【Q和V同:67719 】【顶尖待遇一步到位】-注-册-.-充-值-.-代-理-.-开-户,无需打开页面.欢迎随时咨询!重点applicationContext-aop.xml配置spring提供的切面
顺 达 平 台 代 理 怎么做?做顺 达 平 台 代 理 待遇如何?
猜你喜欢
转载自www.cnblogs.com/zhaojun-20002003/p/12784165.html
今日推荐
周排行