电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Spring AOP面向切面編程實現及配置詳解

瀏覽:5日期:2023-08-15 08:23:25

動態代理

特點

字節碼隨用隨創建,隨用隨加載

作用

不用修改源碼對方法增強

分類

基于接口的動態代理

基于子類的動態代理

創建

使用Proxy類中的newProxyInstance方法

要求

被代理類最少實現一個接口,沒有則不能使用

newProxyInstance方法參數

classLoader:類加載器

用于加載代理對象字節碼的,和被代理對象使用相同的類加載器

class[ ]:字節碼數組

用于讓代理對象和被代理對象有相同方法,固定寫法。

InvocationHandler:用于提供增強的代碼

是讓我們寫如何代理。一般都是寫一個該接口的實現類,通常情況下都是匿名內部類,不是必須的

此接口的實現類都是誰用誰寫

IProducer proxyProducer = (IProducer) Proxy.newProxyInstance(producer.getClass().getClassLoader(),producer.getClass().getInterfaces(),new InvocationHandler(){ 作用:執行被代理對象的任何接口方法都會經過該方法 * proxy 代理對象的引用 * method 當前執行的方法 * args 執行當前方法所需的參數 * return 和被代理對象有相同的返回值@overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable{// 提供增強的代碼Object returnValue = null1. 獲取方法執行的參數Float money = (Float)args[0]2. 判斷當前方法是否為指定方法if('saleProduct'.equals(method.getName())){returnValue = method.invoke(producer,money*0.8)}return returnValue;}})//代理方法調用的是上面invoke中的方法proxyProducer.saleProduct(100000)

注意 如果代理的類沒有接口,則代理不可用。

AOPxml配置

連接點Joinpoint:指那些被攔截的點,在spring中,這些點指的是方法,因為spring只支持方法類型的連接點。

切入點Pointcut:所謂切入點指的是要對哪些Joinpoint進行攔截的定義。方法會被增強。

所有的切入點都是連接點,但不是所有的連接點都是切入點。

通知Advice:指攔截到Joinpoint之后所要做的事情

在invoke方法里的,有前置通知,后置通知,異常通知,最終通知

引入Introduction

目標對象Target :即被代理的對象

織入Weaving:把增強應用到目標對象來創建新的代理對象的過程。Spring采用動態代理織入。

創建接口類,實現類

創建aop通知功能函數

xml配置

<beans xmlns='http://www.springframework.org/schema/beans' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:aop='http://www.springframework.org/schema/aop' xsi:schemaLocation=' http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd'> <!--配置spring的IOC,把service對象配置進來--> <bean class='hjj.web.service.impl.AccountServiceImpl'></bean> <!--spring中基于xml的aop配置步驟 1. 把通知bean也交給spring來管理 2. 使用aop:config標簽表明aop的配置 3. 使用aop:aspect標簽表明配置切面id:給切面提供一個唯一表示ref:指定通知類bean的id 4. 在aop:aspect標簽的內部使用對應的標簽來配置通知的類型現在讓pringLog方法在切入點方法執行前執行aop:before表示配置前置通知 method:用于指定Logger類中哪個方法是前置通知 point屬性:用于指定切入點表達式,該表達式指的是對業務層中哪些方法增強 切入點表達式: 關鍵字:execution(表達式) 訪問修飾符 返回值 包名.類名.方法名(參數列表) 全通配寫法:* *..*.*(..) 訪問修飾符可以省略 *可以代表任何返回值 *.*.*可以表示包的關系 *..表示中間任意包 *.* 表示類名和方法 (..)表示任意參數或者可以寫返回值類型 int, java.lang.String 實際開發寫法:切到業務層實現類下的所有方法 * 業務層包.*.*(..) --> <!--配置logger類--> <bean class='hjj.web.utils.Logger'></bean> <!--配置AOP--> <aop:config> <!--配置切面--> <aop:aspect ref='logger'><!--配置通知類型,并且建立通知方法和切入點方法的關聯--><aop:before method='printLog' pointcut='execution(public void hjj.web.service.impl.AccountServiceImpl.saveAccount())'></aop:before> </aop:aspect> </aop:config>// 通知類型 <aop:aspect ref='logger'><!--配置通知類型,并且建立通知方法和切入點方法的關聯--><!--<aop:before method='printLog' pointcut='execution(public void hjj.web.service.impl.AccountServiceImpl.saveAccount())'></aop:before>--><aop:before method='beforePrintLog' pointcut='execution(* hjj.web.service.impl.AccountServiceImpl.saveAccount())'></aop:before><aop:after-returning method='afterPrintLog' pointcut='execution(* hjj.web.service.impl.AccountServiceImpl.saveAccount())'></aop:after-returning><aop:after-throwing method='afterThrowingPringLog' pointcut='execution(* hjj.web.service.impl.AccountServiceImpl.saveAccount())'></aop:after-throwing><aop:after method='finalPrintLog' pointcut='execution(* hjj.web.service.impl.AccountServiceImpl.saveAccount())'></aop:after> </aop:aspect> </beans>

<!-- 配置切入點表達式,ID屬性用于指定表達式的唯一標識,expression屬性用于指定表達式內容,此標簽也可以放在aspect外面--> <aop:pointcut expression='execution(* hjj.web.service.impl.AccountServiceImpl.saveAccount())'/> <aop:before method='beforePrintLog' pointcut-ref='pt1'></aop:before>

AOPxml注解

aop注解配置

/** * 記錄日志的工具類,提供了公共的代碼 */@Component('logger')@Aspect // 表示當前類是一個切面public class Logger {@Pointcut('execution()')private void pt1(){} /** * 用于打印日志:計劃在其切入點方法執行前執行(切入點方法就是業務層方法) */ @Before(pt1()) public void beforePrintLog() { System.out.println('前置'); } public void afterPrintLog() { System.out.println('后置'); } public void afterThrowingPringLog() { System.out.println('異常'); } public void finalPrintLog() { System.out.println('最終'); } // 環繞通知為我們提供了ProceedingJoinPoint,有一個方法proceed(),此方法就明確了調用切入點方法 // 為我們提供了一種可以在代碼中手動控制增強方法合適執行的方式 public Object aroundPrintLog(ProceedingJoinPoint pjp) { Object returnValue = null; try { Object[] args = pjp.getArgs(); // 得到方法執行所需參數 System.out.println('前置'); returnValue = pjp.proceed(args); // 明確調用業務層的方法 System.out.println('后置'); } catch (Throwable throwable) {// throwable.printStackTrace(); System.out.println('異常'); } finally { System.out.println('最終'); } return returnValue;// System.out.println('環繞通知'); }}

xml:

配置spring創建容器要掃描的包

<context:component-scan base-package='包路徑'></context:component-scan><aop:aspectj-autoproxy></aop:aspectj-autoproxy>

注意 如果用注解自帶的調用順序會出現問題,用環繞通知順序正常

事務控制

導包

<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx --><dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.2.4.RELEASE</version></dependency>

事務管理器:org.springframework.orm.hibernate5.hibernate5.HibernateTransactionManager

在bean.xml中配置

1. 配置事物管理器

<beans xmlns='http://www.springframework.org/schema/beans' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:aop='http://www.springframework.org/schema/aop' xmlns:tx='http://www.springframework.org/schema/tx' xsi:schemaLocation=' http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd'><bean class='org.springframework.orm.hibernate5.hibernate5.HibernateTransactionManager'><property name='dataSource' ref='dataSource'><bean>

2.配置事物的通知

<tx:advice transaction-manager='transactionManager'>

5.配置事物的屬性

<tx:attributes><tx:method name='*' propagation='required' read-only=’false’/><tx:method name='find*' propagation='support' read-only=’true’/>isolation:指定事物的隔離級別,默認值是default,表示使用數據庫的默認隔離級別propagation:用于指定事物的傳播行為,默認是REQUIRED,表示一定會有事物,增刪改的選擇,查詢可以使用supportread-only:用于指定事物是否只讀,查詢才設置為truetimeout:用于指定事物的超市時間,默認值是-1,表示不超時,如果指定了數值,以秒為單位rollback-for:用于指定一個異常,當產生該異常時事物回滾,產生其他異常時,事物不回滾。沒有默認值,表示任何異常都回滾no-rollback-for:用于指定一個異常,當產生該異常,事務不會回滾,產生其他異常,事務回滾。沒有默認值,表示任何異常都回滾。</tx:attributes></tx:advice>

3.配置aop切入點表達式

<aop:config><aop:pointcut expression='execute(* 包.包.*.*(..))'>

4. 建立切入點表達式喝事物通知的對應關系

<aop:advisor advice-ref='txAdvice' pointcut-ref='pt1'></aop>

<beans>

基于注解的事務控制

1. 配置事物管理器

<beans xmlns='http://www.springframework.org/schema/beans' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:aop='http://www.springframework.org/schema/aop' xmlns:tx='http://www.springframework.org/schema/tx' xmlns:context='http://www.springframework.org/schema/context' xsi:schemaLocation=' http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd' http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd'>

3. 配置spring創建容器時要掃描的包

<context:component-scan base-package='包的地址'>

4. 開啟spring對注解事物的支持

<tx:annotation-driven transaction-manager='transactionManager>'

6. 在需要事物支持的地方使用注解@Transactional

2.在實現類中

@Service(accountService)@Transactionalpublic class 實現類 implements 接口類{@Autowired// 在持久層也要配置private IaccountDao accountDao}

基于注解的配置類

1.創建一個配置總配置類

@Configuration// 用于配置需要掃描的包@ComponentScan('hjj.web')@Import({HibernateConfig.class, TransactionConfig.class})@PropertySource('hibernateConfig.properties')@EnableTransactionManagement //開啟注解的支持public class SpringConfiguration{}

2.另一個java類,連接數據庫相關的類

publci class HibernateConfig{@Value('${hibernate.username}')private String username;@Value('${hibernate.password}')private String password// 注入進容器@Bean(name='HibernateTemplate')public Hibernate crateHibernateTemplate(DataSource datasource){return new HibernateTemplate(dataSource)}@Bean(name='dataSource')public DataSource crateDataSource(){配置數據庫的用戶名密碼 創建數據源對象}}

3. 新建一個properties,配置文件類

hibernate.username = hibernate.password =

4. 創建和事物相關的配置類

public class TransactionConfig {//創建事務管理器對象@Bean(name='transactionManager')public PlatformTransactionManager createTransactionManager(DataSource dataSource){return new DataSourceTransactionManager(dataSource)}}

5. main方法所在的類

@ContextConfiguration(classes=SpringConfiguration.class)public class test{psvm{業務邏輯}}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 氟氨基酮、氯硝柳胺、2-氟苯甲酸、异香兰素-新晨化工 | 丹佛斯压力传感器,WISE温度传感器,WISE压力开关,丹佛斯温度开关-上海力笙工业设备有限公司 | 真空上料机(一种真空输送机)-百科 | 手持气象站_便携式气象站_农业气象站_负氧离子监测站-山东万象环境 | 沈阳缠绕包装机厂家直销-沈阳海鹞托盘缠绕包装机价格 | Dataforth隔离信号调理模块-信号放大模块-加速度振动传感器-北京康泰电子有限公司 | 防水套管厂家-柔性防水套管-不锈钢|刚性防水套管-天翔管道 | uv机-uv灯-uvled光固化机-生产厂家-蓝盾机电| 常州企业采购平台_常州MRO采购公司_常州米孚机电设备有限公司 | 盘扣式脚手架-附着式升降脚手架-移动脚手架,专ye承包服务商 - 苏州安踏脚手架工程有限公司 | 玻璃瓶厂家_酱菜瓶厂家_饮料瓶厂家_酒瓶厂家_玻璃杯厂家_徐州东明玻璃制品有限公司 | 动库网动库商城-体育用品专卖店:羽毛球,乒乓球拍,网球,户外装备,运动鞋,运动包,运动服饰专卖店-正品运动品网上商城动库商城网 - 动库商城 | 房车价格_依维柯/大通/东风御风/福特全顺/江铃图片_云梯搬家车厂家-程力专用汽车股份有限公司 | 贵州成人高考网_贵州成考网| 活性氧化铝|无烟煤滤料|活性氧化铝厂家|锰砂滤料厂家-河南新泰净水材料有限公司 | 100国际学校招生 - 专业国际学校择校升学规划 | 东莞市超赞电子科技有限公司 全系列直插/贴片铝电解电容,电解电容,电容器 | 动库网动库商城-体育用品专卖店:羽毛球,乒乓球拍,网球,户外装备,运动鞋,运动包,运动服饰专卖店-正品运动品网上商城动库商城网 - 动库商城 | 盛源真空泵|空压机-浙江盛源空压机制造有限公司-【盛源官网】 | 重庆磨床过滤机,重庆纸带过滤机,机床伸缩钣金,重庆机床钣金护罩-重庆达鸿兴精密机械制造有限公司 | 上海律师咨询_上海法律在线咨询免费_找对口律师上策法网-策法网 广东高华家具-公寓床|学生宿舍双层铁床厂家【质保十年】 | BESWICK球阀,BESWICK接头,BURKERT膜片阀,美国SEL继电器-东莞市广联自动化科技有限公司 | 安规_综合测试仪,电器安全性能综合测试仪,低压母线槽安规综合测试仪-青岛合众电子有限公司 | 烟台螺纹,烟台H型钢,烟台钢材,烟台角钢-烟台市正丰金属材料有限公司 | 礼堂椅厂家|佛山市艺典家具有限公司| 硬质合金模具_硬质合金非标定制_硬面加工「生产厂家」-西迪技术股份有限公司 | 氢氧化钙设备_厂家-淄博工贸有限公司 | 动物解剖台-成蚊接触筒-标本工具箱-负压实验台-北京哲成科技有限公司 | 登车桥动力单元-非标液压泵站-非标液压系统-深圳市三好科技有限公司 | 水篦子|雨篦子|镀锌格栅雨水篦子|不锈钢排水篦子|地下车库水箅子—安平县云航丝网制品厂 | 不干胶标签-不干胶贴纸-不干胶标签定制-不干胶标签印刷厂-弗雷曼纸业(苏州)有限公司 | 高考志愿规划师_高考规划师_高考培训师_高报师_升学规划师_高考志愿规划师培训认证机构「向阳生涯」 | 检验科改造施工_DSA手术室净化_导管室装修_成都特殊科室建设厂家_医疗净化工程公司_四川华锐 | 济南拼接屏_山东液晶拼接屏_济南LED显示屏—维康国际官网 | 防水套管-柔性防水套管-刚性防水套管-上海执品管件有限公司 | 泰安办公家具-泰安派格办公用品有限公司 | 高考志愿规划师_高考规划师_高考培训师_高报师_升学规划师_高考志愿规划师培训认证机构「向阳生涯」 | 打造全球沸石生态圈 - 国投盛世 锂电混合机-新能源混合机-正极材料混料机-高镍,三元材料混料机-负极,包覆混合机-贝尔专业混合混料搅拌机械系统设备厂家 | 飞象网 - 通信人每天必上的网站| 日本细胞免疫疗法_肿瘤免疫治疗_NK细胞疗法 - 免疫密码 | 连续油炸机,全自动油炸机,花生米油炸机-烟台茂源食品机械制造有限公司 |