95992828九五至尊2

什么样简单了解spring aop和作业。面向切面编程。

十一月 20th, 2018  |  九五至尊ii

据此打比方的法知情吧:

平,原理简介

      初家的掌握,仅仅为个人好记

切点(Pointcut)

切点的概念会配合通知所而织入的一个要多单连接点。通常使用明确的类似与法名称来指定这些切点,或是利用正则表达式定义匹配的好像以及措施名称模式来指定这些切点。

切点用于规范定位应该当什么地方以切面的通

       aop:由三局部构成:工具箱,工人,为工友分配工具

切面(Aspect)

断面是通及切点的结缘。通知以及切点共同定义了有关切面的全部内容——它是啊,在何时和何处完成另外力量。

在XML中声称切面

Spring提供了声明式切面的选料。

Spring的AOP配置元素简化了因POJO切面的宣示:

 

AOP配置元素 功能
<aop:advisor> 定义一个AOP通知器
<aop:after> 定义一个AOP后置通知(不考虑被通知的方法是否执行成功)
<aop:after-returning> 定义一个AOP返回后通知
<aop:after-throwing> 定义一个AOP抛出后通知
<aop:around> 定义一个AOP环绕通知
<aop:aspect> 定义一个切面
<aop:before> 定义一个AOP前置通知
<aop:config> 顶层的AOP元素。大多数<aop:*>元素必须包含在<aop:config>里
<aop:pointcut> 定义一个切点
<aop:declare-parents> 为被通知的对象引入额外的接口,并透明地实现

 

 

 

 

 

 

 

 

 

 

       tx事务:由四有些构成:管理者,制度,工人,向工友通知管理制度

 二,一个大概的案例

扬言前置和后置通知

XML Bean配置文件:

 1 <?xml version="1.0" encoding="UTF-8"?>  
 2 <beans xmlns="http://www.springframework.org/schema/beans"  
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
 4     xmlns:p="http://www.springframework.org/schema/p"  
 5     xmlns:context="http://www.springframework.org/schema/context"  
 6     xmlns:aop="http://www.springframework.org/schema/aop"  
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans   
 8     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
 9     http://www.springframework.org/schema/context   
10     http://www.springframework.org/schema/context/spring-context-3.0.xsd  
11     http://www.springframework.org/schema/aop  
12     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">  
13       
14     <!-- Root Context: defines shared resources visible to all other web components -->  
15     <bean id="advised" class="com.springtest.AdvisedObj" />  
16     <bean id="advising" class="com.springtest.AdvisingObj" />  
17   
18     <aop:config>  
19         <aop:aspect ref="advising">  
20               
21             <aop:pointcut id="performance" expression="execution(* com.springtest.AdvisedObj.perform(..))" />  
22               
23             <aop:before pointcut-ref="performance" method="beforeExecute" />  
24             <aop:after  pointcut-ref="performance" method="afterExecute" />       
25                 
26         </aop:aspect>  
27     </aop:config>  
28   
29 </beans>  

Java Code:

 1 package com.springtest;  
 2   
 3 import org.springframework.context.ApplicationContext;  
 4 import org.springframework.context.support.FileSystemXmlApplicationContext;  
 5   
 6 import org.slf4j.Logger;  
 7 import org.slf4j.LoggerFactory;  
 8   
 9 import com.springtest.AdvisedObj;  
10 import com.springtest.AdvisingObj;  
11   
12 public class SpringCoreTest{  
13       
14     public static void main(String[] args){  
15         ApplicationContext ctx =   
16                 new FileSystemXmlApplicationContext("//Users/... .../beans_config.xml");  
17           
18         AdvisedObj advisedObj = (AdvisedObj)ctx.getBean("advised");  
19         advisedObj.perform();  
20     }  
21 }  

 1 package com.springtest;  
 2   
 3 import org.slf4j.Logger;  
 4 import org.slf4j.LoggerFactory;  
 5   
 6 public class AdvisedObj {  
 7     private static final Logger logger = LoggerFactory.getLogger(AdvisedObj.class);  
 8       
 9     public void perform(){  
10         logger.info("AdvisedObj perform().");  
11     }  
12 }  

 1 package com.springtest;  
 2   
 3 import org.slf4j.Logger;  
 4 import org.slf4j.LoggerFactory;  
 5   
 6 public class AdvisingObj {  
 7     private static final Logger logger = LoggerFactory.getLogger(AdvisingObj.class);  
 8       
 9     public void beforeExecute(){  
10         logger.info("AdvisingObj beforeExcecute().");  
11     }  
12       
13     public void afterExecute(){  
14         logger.info("AdvisingObj afterExecute().");  
15     }  
16 }  

1 00:52:24.350 [main] INFO  com.springtest.AdvisingObj - AdvisingObj beforeExcecute().  
2 00:52:24.367 [main] INFO  com.springtest.AdvisedObj - AdvisedObj perform().  
3 00:52:24.367 [main] INFO  com.springtest.AdvisingObj - AdvisingObj afterExecute().

至于Spring
AOP配置元素,大多数的AOP配置元素必须在<aop:config>元素的光景文内使用。

每当拥有的通元素中,pointcut属性定义了通所祭的切点。pointcut属性的值是利用AspectJ切点表达式语法所定义之切点

<aop:pointcut>元素所定义的切点可以给与一个<aop:aspect>元素外的备通知元素所引用。如果想叫定义之切点能够以多独断面使用,可以拿<aop:pointcut>元素放在<aop:config>元素的图域内。

 个人总结:面向切面编程就是当有着的代码都早已勾勒了之后,需要以及时段代码上添加另外的事情逻辑,但是还要不影响原本的政工逻辑的实践。比如身为日志,事务等等。

以地方的案例来讲就是是AdvisedObj
perform()是好之主方法,现在得在就段代码之前之后要丰富另的政工逻辑,所以即使会出面向切面编程。

实则面向切面编程主要要明当下三词话

1,什么时加。原有的政工逻辑已经有,现在欲以初的工作逻辑的根基及长其他的逻辑,但是又未影响原有的事务逻辑,比如就是日志,事务等等。

2,加在那里 <aop:pointcut
id=”performance” expression=”execution(*
com.springtest.AdvisedObj.perform(..))” /> 
,通过它来支配,可以决定及保证,类,方法。可以加以在概念的东西事先,之后或拱。

3,加什么,就是 <aop:aspect
ref=”advising”> 这里定义之这advising   它是这般定义的<bean
id=”advising” class=”com.springtest.AdvisingObj” /> 
其中管里面的道定义及

<aop:before pointcut-ref=”performance”
method=”beforeExecute” />
<aop:after pointcut-ref=”performance”
method=”afterExecute” />

当下其间来就算足以了。

 

 三,面向编程思想在类型被之应用

     为什么这样敞亮也?个人认为好记:

3.1,业务代码逻辑

为数据库插入的代码

public class MerchantInfoDao extends com.bill99.inf.ibatis.BaseDao{
    public MerchantInfo createMerchantInfo(MerchantInfo model){
        //model.setId(super.getSequence("seqName"));//如果主键是sequence生成
        dao.insert("orgMerchantInfo.insertOrgMerchantInfo",model);
        return model;
    }

 

起数库取的代码

1 public List<MerchantInfo> queryMerchantInfoByModel(MerchantInfo model){
2         return (List<MerchantInfo>)dao.queryForList("orgMerchantInfo.queryModelOrgMerchantInfo",model);
3     }

 

实体类的代码

   1 package com.bill99.boss.domain.org.model;
   2 
   3 import java.util.List;
   4 
   5 import javax.validation.constraints.NotNull;
   6 import javax.validation.constraints.Pattern;
   7 
   8 import org.hibernate.validator.constraints.Length;
   9 import org.hibernate.validator.constraints.NotEmpty;
  10 import org.springframework.web.multipart.commons.CommonsMultipartFile;
  11 
  12 import com.bill99.boss.data.security.annotation.Crypto;
  13 import com.bill99.boss.domain.org.service.NotEmptyForBusinessValidate;
  14 
  15 @NotEmptyForBusinessValidate
  16 @Crypto
  17 public class MerchantInfo
  18 {
  19     private Long merchantCode;
  20     private String merchantId;
  21 
  22     @NotNull(message = "【商户类型】不能为空")
  23     private Long merchantType;
  24     
  25     private Long websiteMerchantType;
  26 
  27     @NotEmpty(message = "【客户名称】不能为空")
  28     @Length(max = 128, message = "【客户名称】长度不超过128")
  29     private String merchantName;
  30 
  31     @Length(max = 128, message = "【客户对外营业名称】长度不超过128")
  32     private String merchantShortname;
  33 
  34     @Length(max = 128, message = "【客户别名】长度不超过128")
  35     private String merchantAlias;
  36 
  37     private String relationType;
  38 
  39     private String grade;
  40 
  41     private Long statusLevel1;
  42 
  43     private Long statusLevel2;
  44 
  45     private String orgBelongto;
  46 
  47     private String parentCustcode;
  48 
  49     private Long childrenCustomer;
  50 
  51     private String companyType;
  52 
  53     private String mainUsers;
  54 
  55     @Length(max = 32, message = "【法人代表姓名】长度不超过32")
  56     private String legalName;
  57 
  58     @Crypto
  59     private String legalId;
  60 
  61     private Long legalSex;
  62 
  63     private String province;
  64 
  65     private String city;
  66 
  67     @Length(max = 32, message = "【固定电话】长度不超过32")
  68     @Pattern(regexp = "((0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?)?", message = "【固定电话】[区号-]+7至8位号码+[-分机号]")
  69     private String telephone;
  70 
  71     @NotEmpty(message = "【手机】不能为空")
  72     @Length(max = 32, message = "【手机】长度不超过32")
  73     @Pattern(regexp = "(1[3|4|5|7|8|][0-9]{9})?", message = "【手机】无效的手机号码格式")
  74     @Crypto
  75     private String mobilephone;
  76 
  77     @Pattern(regexp = "(([a-zA-Z0-9_-]|\\.|\\*)+@([a-zA-Z0-9_-]|\\.)+\\.([a-zA-Z0-9_-])+)?", message = "【电子邮箱】格式不正确")
  78     @Length(max = 50, message = "【邮箱地址】长度不超过50")
  79     private String email;
  80 
  81     @Length(max = 255, message = "【邮编】长度不超过255")
  82     @Pattern(regexp = "\\d*", message = "【邮编】只能填整数")
  83     private String postcode;
  84 
  85     @Length(max = 128, message = "【经营地址】长度不超过128")
  86     private String address;
  87 
  88     @Length(max = 200, message = "【经营场所所属权】长度不超过200")
  89     private String busiPropertyBelongto;
  90 
  91     @Length(max = 32, message = "【营业执照注册号】长度不超过32")
  92     private String businessRegno;
  93 
  94     @Length(max = 10, message = "【经营场所面积(平米)】长度不超过10")
  95     @Pattern(regexp = "\\d*", message = "【经营场所面积(平米)】只能填整数")
  96     private String businessArea;
  97 
  98     @Length(max = 100, message = "【经营范围】长度不超过100")
  99     private String businessScope;
 100 
 101     private String orgCode;
 102 
 103     private java.util.Date registDate;
 104 
 105     @Length(max = 10, message = "【注册资本(万元)】长度不超过10")
 106     private String registFund;
 107 
 108     private java.util.Date handleDate;
 109 
 110     private java.util.Date cancelDate;
 111 
 112     @Length(max = 600, message = "【注册地址】长度不超过600")
 113     private String registAddress;
 114 
 115     @Length(max = 100, message = "【约定业务】长度不超过100")
 116     private String constraintBusiness;
 117 
 118     private String industryCode;
 119 
 120     @Length(max = 500, message = "【行业大类】长度不超过500")
 121     private String industryParentType;
 122 
 123     @Length(max = 500, message = "【行业小类】长度不超过500")
 124     private String industrySubType;
 125 
 126     private String area;
 127 
 128     private String partnerid;
 129 
 130     private java.util.Date foundDate;
 131 
 132     private String operationTime;
 133 
 134     @Length(max = 10, message = "【员工数量】长度不超过10")
 135     @Pattern(regexp = "\\d*", message = "【员工数量】只能填整数")
 136     private String employees;
 137 
 138     private Long isBlack;
 139 
 140     @Length(max = 20, message = "【授权书编号】长度不超过20")
 141     private String authorizeNum;
 142 
 143     private String authfileStatus;
 144 
 145     @Length(max = 10, message = "【实收资本】长度不超过10")
 146     private String capitalPaid;
 147 
 148     @Length(max = 128, message = "【客户网址】长度不超过128")
 149     private String website;
 150 
 151     private String creater;
 152 
 153     private String ext1;
 154 
 155     private String ext2;
 156 
 157     private String ext3;
 158 
 159     private java.util.Date createDate;
 160 
 161     private java.util.Date lastupDate;
 162 
 163     private String orderField;
 164 
 165     private String foundDateStr;
 166 
 167     private String billAccountType;
 168     private String billAccount;
 169 
 170     private CommonsMultipartFile businessFile;
 171 
 172     private String cityCode;
 173 
 174     private String provinceCode;
 175 
 176     private String applyId;
 177     
 178     private String orderId;
 179 
 180     private int orderStatus;
 181     
 182     private String accountNo;
 183     
 184     private String channelShortName;
 185     
 186     private String channelUser;
 187     
 188     private String source;
 189     
 190     private String merchantCodeStr; 
 191     
 192     private String createDateStr;
 193     
 194     private String merchantBeginTime;
 195     
 196     private String merchantEndTime;
 197 
 198     // 客户网站名称
 199     @Length(max = 200, message = "【 客户网站名称】长度不超过200")
 200     private String websiteName;
 201 
 202     // ICP网站备案人
 203     @Length(max = 20, message = "【ICP备案人】长度不超过20")
 204     private String icpOwner;
 205 
 206     // 网站备案年限
 207     @Length(max = 20, message = "【网站备案年限】长度不超过20")
 208     private String icpAge;
 209 
 210     // ICP备案号
 211     @Length(max = 100, message = "【 ICP备案号】长度不超过100")
 212     private String icpNo;
 213 
 214     // ICP备案公司名称
 215     @Length(max = 200, message = "【ICP备案公司名称】长度不超过200")
 216     private String icpCompanyName;
 217 
 218     private Long merchantCodeCopy;
 219 
 220     private String businessFileName; 
 221     
 222     private String memberCode;
 223     
 224     private String account_name;
 225     
 226     @Crypto
 227     private String accountMemberCode;
 228     
 229     private String cardType;
 230     
 231     /**
 232      * 证件类型为其它时,补充描述
 233      */
 234     private String cardDesc;
 235     
 236     private String isWdBusinessCircle;
 237     
 238     private String merchantSource;
 239     
 240     private String accountType;
 241     
 242     private String file_underline;
 243     
 244     private String merchantSourceAttr;
 245     
 246     private String channelFullName;
 247     
 248     private String agentType;
 249     
 250     @Length(max = 500, message = "【申请人备注】长度不超过500")
 251     private String remark;
 252     
 253     @Length(max = 32, message = "【国家(地区)】长度不超过32")
 254     private String country;
 255 
 256     private String isListedCompanies;
 257     
 258     @Length(max = 128, message = "【上市证券市场】长度不超过128")
 259     private String listedSecuritiesMarket;
 260     
 261     @Length(max = 128, message = "【控股人】长度不超过128")
 262     private String controllingShareholder;
 263     
 264     @Length(max = 32, message = "【年营业额】长度不超过32")
 265     private String annualTurnover;
 266 
 267     @Length(max = 128, message = "【主要客户所在国家(地区)】长度不超过128")
 268     private String majorClientCountries;
 269     
 270     private String registrationStatus;
 271     
 272     @Length(max = 128, message = "【控股比例】长度不超过128")
 273     private String holdingRatio;
 274     
 275     @Length(max = 32, message = "【注册地所在省份】长度不超过32")
 276     private String registProvince;
 277     
 278     @Length(max = 32, message = "【注册地所在城市】长度不超过32")
 279     private String registCity;
 280     
 281     private String riskLevel;
 282     
 283     private String registCode;
 284     
 285     private String businessType;
 286     
 287     private String customerStatus;
 288     
 289     private String isChannelCustomer;
 290     
 291     @Length(max = 64, message = "【经营品牌】长度不超过64")
 292     private String customerBrand;
 293     
 294     private String keyCustomer;
 295     
 296     private String manualDowngrade;
 297     
 298     private String parentCustname;
 299     
 300     private String headquarters;
 301     
 302     private String multiAssetTag;
 303     
 304     private String custBgIntroduction;
 305     
 306     private String custIndustryStatus;
 307     
 308     private String custAnnualSales;
 309     
 310     private String mainProductService;
 311     
 312     private String custSaleModel;
 313     
 314     private String shareMargin;
 315     
 316     private String accessType;
 317     
 318     private String saleOrg;
 319     
 320     private String saler;
 321     
 322     private String frontHelper;
 323     
 324     private String frontHelperId;
 325     
 326     private String clueSource;
 327     
 328     private String clueProvider;
 329     
 330     private String clueConnSituation;
 331     
 332     private String expectedAvgMonthlyAmount;
 333     
 334     private String expectedAvgMonthlyAmount99Bill;
 335     
 336     private String expectedAvgTransactionValue;
 337     
 338     private String salerDomainName;
 339     
 340     private String salerId;
 341     
 342     private String squareType;
 343 
 344     private String ffanDept;
 345 
 346     private String squareName;
 347 
 348 
 349     private String subjectType;
 350 
 351     private String businessFormat;
 352 
 353     @Length(max = 64, message = "【经营类目】长度不超过64")
 354     private String categorys;
 355 
 356     @Length(max = 64, message = "【品牌许可】长度不超过64")
 357     private String brandLicense;
 358     
 359     private String isActivated;
 360 
 361     private String identityType;
 362     
 363     private String complianceRisk;
 364     
 365     private String complianceRiskRemark;
 366     
 367     private String sendAppMsg; // 资质后补app提醒次数
 368 
 369     private String complianceRiskReason;
 370     
 371     private String relationMerchantType;
 372     
 373     private String relationMerchantName;
 374     
 375     private String foreignBusinessName;
 376     /**
 377      * 统一社会信用代码(三证合一)1是,0和null不是
 378      */
 379     private String isUniformSocialCredit;
 380     
 381     private List<String> salerIdGroups;
 382     
 383     private List<String> frontHelperGroups;
 384     
 385     private String changeList;
 386     
 387     private String mccCode;
 388 
 389     private String assetsAttrName;
 390     private String charval;
 391     
 392     public String getAssetsAttrName() {
 393         return assetsAttrName;
 394     }
 395 
 396 

 

自从定义注解的代码

 1 @Documented
 2 @Retention(RetentionPolicy.RUNTIME)
 3 @Target({java.lang.annotation.ElementType.TYPE,
 4         java.lang.annotation.ElementType.PARAMETER,
 5         java.lang.annotation.ElementType.FIELD,
 6         java.lang.annotation.ElementType.METHOD})
 7 public @interface Crypto {
 8     public abstract DataType dataType();
 9 
10     public abstract String[] keys();
11 
12     public abstract boolean ignore();
13 }

 

 

上面的平等效仿代码是完全的事务逻辑,即使下面什么都未写,在点的代码中把实体类中之诠释给错过丢还不影响代码的履。

脚要召开的虽通过面向切面的思量,在数插入到数据库的时刻将

private String legalId;
private String mobilephone;

立简单只字段加密,

起数额库取出来的时光便会自然而然的解密。

      在aop 中出切面:切面内的事物用来公共使用,类似于工具箱:

3.2,配置文件中的代码

 1     <aop:config>
 2         <aop:pointcut id="decryptPointCut"
 3             expression="execution(* com.bill99.boss.domain.org.dao.*Dao.*(..))" />
 4         <aop:pointcut id="encryptPointCut"
 5             expression="execution(* com.bill99.boss.domain.org.dao.*Dao.*(..)) " 
 6              />
 7         <aop:advisor pointcut-ref="encryptPointCut"         advice-ref="encryptAdvice"
 8         order="1" />     
 9         <aop:advisor pointcut-ref="decryptPointCut"         advice-ref="decryptAdvice"
10         order="2" />     
11     </aop:config>
12     
13     
14     
15     <bean id="decryptAdvice" class="com.bill99.boss.data.security.advice.DecryptAdvice">
16         <property name="cryptoService">
17             <ref bean="cryptoService" />
18         </property>
19     </bean>
20     <bean id="encryptAdvice" class="com.bill99.boss.data.security.advice.EncryptAdvice">
21         <property name="cryptoService">
22             <ref bean="cryptoService" />
23         </property>
24     </bean>
25     
26     
27     
28     
29     <bean id="cryptoService" class="com.bill99.boss.data.security.CryptoService">
30         <property name="parsers">
31             <map>
32                 <entry key="json" value-ref="jsonParser" />
33                 <entry key="text" value-ref="textParser" />
34                 <entry key="map" value-ref="mapParser" />
35             </map>
36         </property>
37     </bean>
38     
39     
40     
41     
42     <bean id="jsonParser" class="com.bill99.boss.data.security.parser.JsonParser"></bean>
43     <bean id="textParser" class="com.bill99.boss.data.security.parser.TextParser"></bean>
44     <bean id="mapParser" class="com.bill99.boss.data.security.parser.MapParser"></bean>

 

 

 加密的代码。注意下面的代码的意就是是使实现了implements
MethodBeforeAdvice这个接口就必须的重写before(Method method, Object[]
aobj, Object obj) 这个法子。

其中method是纯属面类的章程
Object[]凡法中的参数
Object是类名
method,—com.bill99.boss.domain.org.dao.MerchantInfoDao.createMerchantInfo(com.bill99.boss.domain.org.model.MerchantInfo)
Object[]
aobj—[com.bill99.boss.domain.org.model.MerchantInfo@4fed47cd]
object.getClass().isAnnotationPresent(Crypto.class) —object就是aobj
Object obj—com.bill99.boss.domain.org.dao.MerchantInfoDao@6bf5a8bf

下面会因取得到之Object[] aobj–做判断。

假若方法方面来Crypto.class这个注解的语虽执行怎样的艺术。

假设实体类吃发出夫办法就行什么样的方法。

中间加了一个list集合防止污染过来的参数需要一个个遍历

 1 public class EncryptAdvice implements MethodBeforeAdvice
 2 {
 3     private CryptoService cryptoService;
 4 
 5     
 6     @Override
 7     public void before(Method method, Object[] aobj, Object obj) throws Throwable
 8     {
 9        /* if(!isEnableCrypto())
10         {
11             return;
12         }*/
13         
14         if (method.isAnnotationPresent(Crypto.class))
15         {
16             Crypto crypto = method.getAnnotation(Crypto.class);
17             if(crypto.ignore())
18             {
19                 return;
20             }
21         }
22         
23         for (Object object : aobj)
24         {
25             if (null == object)
26             {
27                 continue;
28             }
29 
30             if (object instanceof List)
31             {
32                 List<Object> list = (List<Object>) object;
33                 for (Object o : list)
34                 {
35                     if (!o.getClass().isAnnotationPresent(Crypto.class))
36                     {
37                        break;
38                     }
39                     cryptoService.encrypt(o);
40                 }
41             }
42             else
43             {
44                 if (object.getClass().isAnnotationPresent(Crypto.class))
45                 {
46                     cryptoService.encrypt(object);
47                 }
48             }
49         }
50 
51         Annotation[][] an = method.getParameterAnnotations();
52         if (an.length > 0)
53         {
54             for (int i = 0; i < an.length; i++)
55             {
56                 for (int j = 0; j < an[i].length; j++)
57                 {
58                     Annotation annotation = an[i][j];
59                     if (annotation.annotationType() == Crypto.class)
60                     {
61                         Crypto crypto = (Crypto) annotation;
62                         AbstractParser parser = cryptoService.getParsers().get(crypto.dataType().getType());
63                         parser.crypto(aobj[i], crypto.keys(), CryptoMode.Encrypt);
64                     }
65                 }
66             }
67         }
68 
69     }
70 
71     /**
72      * @param cryptoService
73      *            the cryptoService to set
74      */
75     public void setCryptoService(CryptoService cryptoService)
76     {
77         this.cryptoService = cryptoService;
78     }
79 }

 

 

 

 cryptoService.encrypt(object);它见面调用下面的主意

脚的法门是经过反射来收获实体类中之习性变量。

来判断属性当中发生没产生注解@Crypto这个注解假如有些话就会启动加密的措施。

 

 1  private void cryptography(Object object, CryptoMode mode) throws SecurityException, HSMException
 2     {
 3         Field[] fields = object.getClass().getDeclaredFields();
 4         for (int i = 0; i < fields.length; i++)
 5         {
 6             Field field = fields[i];
 7             if (field.isAnnotationPresent(Crypto.class))
 8             {
 9                 // 获取该字段的注解对象
10                 Crypto anno = field.getAnnotation(Crypto.class);
11 
12                 Object value = getFileValue(field.getName(), object);
13                 Object cryptoValue = null;
14 
15                 if (null != value)
16                 {
17                     AbstractParser parser = parsers.get(anno.dataType().getType());
18                     cryptoValue = parser.crypto(value, anno.keys(), mode);
19                     setFileValue(field.getName(), object, cryptoValue);
20                 }
21             }
22         }
23     }

 

 

 

 cryptoValue = parser.crypto(value, anno.keys(), mode);

尽管是她去调用加密的不二法门的。加密底方式来那么些在此间就是隐瞒了。

而且解密也是平的理。

注意afterReturning(Object returnValue, Method method, Object[] arg2,
Object arg3) throws Throwable

内参数的意思,就是说

returnValue的返回值

Method method 执行之法门对象

Object[] arg2 方法执行着九五至尊ii传送过来的参数

Object arg3  执行措施的目标

具体代码意思见加密的意思。

 1 public class DecryptAdvice implements AfterReturningAdvice
 2 {
 3     private CryptoService cryptoService;
 4     
 5     //private CpConfigService cpConfigService;
 6     
 7     @SuppressWarnings("unchecked")
 8     @Override
 9     public void afterReturning(Object returnValue, Method method, Object[] arg2, Object arg3) throws Throwable
10     {
11         /*if(!isEnableCrypto())
12         {
13             return;
14         }*/
15         
16         if (method.isAnnotationPresent(Crypto.class))
17         {
18             Crypto crypto = method.getAnnotation(Crypto.class);
19             if(crypto.ignore())
20             {
21                 return;
22             }
23         }
24         
25         
26         if (arg2.length > 0)
27         {
28             for (int i = 0; i < arg2.length; i++)
29             {
30                 if (null == arg2[i])
31                 {
32                     continue;
33                 }
34                 decrypt(arg2[i]);
35             }
36         }
37 
38         if (null == returnValue)
39         {
40             return;
41         }
42        
43         if (returnValue instanceof List)
44         {
45             List<Object> list = (List<Object>) returnValue;
46             for (Object object : list)
47             {
48                 if (object == null)
49                 {
50                     continue;
51                 }
52                 decryptSingle(method,arg2,object);
53 
54             }
55         }
56         else
57         {
58             decryptSingle(method,arg2,returnValue);
59         }
60     }
61     

 

 

 

 1    private void decryptSingle(Method method, Object[] params,Object result) throws SecurityException, HSMException
 2     {
 3         if (result instanceof Map)
 4         {
 5             if (method.isAnnotationPresent(Crypto.class))
 6             {
 7                 Crypto crypto = method.getAnnotation(Crypto.class);
 8                 
 9                 AbstractParser parser = cryptoService.getParsers().get(crypto.dataType().getType());
10                 parser.crypto(result, crypto.keys(), CryptoMode.Decrypt);
11             }
12         }
13         else
14         {
15             //如果返回值和参数值相同引用,则直接返回
16             for (int i = 0; i < params.length; i++)
17             {
18                 if (params[i] == result)
19                 {
20                     return;
21                 }
22             }
23             decrypt(result);
24         }
25     }
26     

 

 

 

 总结:通过地方的例子我们好看看于面向切面的编程的长河被,我们如果只是拘留工作逻辑代码的时节,我们会意识,我们什么还无操作,为什么到了数额的身份证以及手机号还是加过密的,当我们取得出来的时,有自然而然的见面叫解密掉呢。

我们首先第一单意识虽是是休是数据库进行了哟操作,我们透过数据库的客户端工具我们会发现数据库什么还没有做。

进而我们回来看dao层究竟开了哟,发现也未曾加密解密之代码。

俺们还望上找寻service,control层都并未发现。

这时我们虽见面充分迷惑,究竟是那里进行的操作为。

下我们或许回到打断点来拘禁,发现于service层还尚未加密,但是到dao层的上取得之多少就给加密了。这时候我们便会见再迷惑了。

要在搜索找的言语,我们会发现实体类吃发出一个自定义的笺注加密解密之字样。

我们满心欢喜的当找到答案的时这也发现此注解什么都无开,只不过是概念了以下而已。什么还不曾召开。

 

 

上述就是自个儿以首先接触到当时段代码的心得,假如尚未面向切面的意识的当儿,有时候高手写的代码很不便知晓的。假如下重新出现仿佛之气象咱得通往这点考虑的。

 

享受出去供大家玩赏。

 

                             ref就是是家伙箱的具体bean。。            
                  

<aop:aspect id="***"
            ref="*****">

                   
切点:切点是广大合乎切点表达式的事物,只要符合就得采用集体的物。根据表达式,挑来总体合的。。成为工人

 <aop:pointcut id="XXXXXXX"
                          expression="execution(* party.infoo.spring.AOP.service.*.*(..))"/>

 

                   
通知:什么前置后置返回等等,无非就是是概念了啊时下什么,所以理解成呢工友分配工具

<aop:before method="doBefore"
                        pointcut-ref="XXXXXXX"/>
            <aop:after method="doAfter"
                       pointcut-ref="XXXXXXX"/>
            <aop:around method="doAround"
                        pointcut-ref="XXXXXXX"/>
            <aop:after-returning method="doAfterReturning"
                                 pointcut-ref="XXXXXXX"/>
            <aop:after-throwing method="doAfterThrowing"
                                pointcut-ref="XXXXXXX"
                                throwing="ex"/>

 

 

     事务:

                      管理者:

 <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

 

                       
 管理制度的内容即是啊工人做啊任务:如下,是对工人的律     

                                   
  管理制度(事务通知)不容许无缘无故存在,所以其需transaction-manager=“
管理者 ”               

<tx:advice id="transactionInterceptor" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="insert*" propagation="REQUIRED" />
        <tx:method name="update*" propagation="REQUIRED" />
        <tx:method name="edit*" propagation="REQUIRED" />
        <tx:method name="save*" propagation="REQUIRED" />
        <tx:method name="add*" propagation="REQUIRED" />
        <tx:method name="new*" propagation="REQUIRED" />
        <tx:method name="set*" propagation="REQUIRED" />
        <tx:method name="remove*" propagation="REQUIRED" />
        <tx:method name="delete*" propagation="REQUIRED" />
        <tx:method name="change*" propagation="REQUIRED" />
        <tx:method name="get*" propagation="REQUIRED" read-only="true" />
        <tx:method name="find*" propagation="REQUIRED" read-only="true" />
        <tx:method name="load*" propagation="REQUIRED" read-only="true" />
        <tx:method name="*" propagation="REQUIRED" read-only="true" />
    </tx:attributes>
</tx:advice>

 

                 工人:工友的概念和aop中近乎,只要符合条件的都是工人

<aop:pointcut id="serviceOperation" expression="execution(* party.infoo.service..*.*(..))"/>

                 向工人通知管理制度: advisor

 <aop:advisor advice-ref="transactionInterceptor" pointcut-ref="serviceOperation"/>

 

 

            

 

相关文章

标签:,

Your Comments

近期评论

    功能


    网站地图xml地图