빅 데이터 WEB 단계 Spring 프레임 워 크 AOP 절단면 프로 그래 밍 (2)
1. 접점 의 execution 표현 식
 execution (* com. xyz. service.. (..) com. xyz. service 는 모든 종류의 모든 방법 을 책임 집 니 다.com. xyz 로 시작 하 는 모든 하위 패 키 지 를 service 아래 의 모든 종류의 모든 방법 으로 신청 합 니 다. 예 를 들 어 com. xyz. a. b. service com. xyz. a. service com. xyz. a. b. c. service 는 위의 조건 을 만족 시 킬 것 입 니 다
2. 5 대 통지 의 구체 적 인 실현
3. AOP 주해
4. 주 해 를 통 해 절 입 점 표현 식 의 인용 생 성
질문
6. 사용자 정의 주석
        
        PersonServlet
            PersonService
                PersonDao
       servlet           :
                    cn.tedu.big1601.servlet.PersonServlet
                    save()
                     XxxException
                        message
  :
                。
                ,       ,    。
@Component
@Aspect
public class ExceptionAspect {
    @AfterThrowing(value = "execution(* com.tj..*(..))" ,throwing = "throwable" )
    public void after(JoinPoint  jp , Throwable throwable){
        System.out.println("     :"+jp.getTarget().getClass());
        System.out.println(jp.getSignature().getName()+"()     !");
        System.out.println("      :"+throwable.getClass());
        System.out.println("    :"+throwable.getMessage());
    }
}
   servlet              
1.    
2.                
3.     
     
  :                 
@Component
@Aspect
public class RuntimeAspect {
    @Around("execution(* com.tj..*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable{
        Long begin = System.currentTimeMillis();
        Object result = pjp.proceed();
        Long end = System.currentTimeMillis();
        System.out.println(pjp.getTarget().getClass()+" "+pjp.getSignature().getName()+"     "+ (end - begin)+"  !");
        return result;
    }
}
           ,       。
                        
            
public class TxManage {
    /**
     *     
     * */
    public static void stattx(){
        System.out.println("     ");
    }
    /**
     *     
     * */
    public  static void commitTx(){
        System.out.println("    ");
    }
    /**
     *     
     * */
    public static void rollback(){
        System.out.println("    ");
    }
}
       
@Target(value = { ElementType.METHOD })
@Retention(value = RetentionPolicy.RUNTIME)
public @interface TxAnnotation {
    String value() default "";
}
              
@Component
public class PersonServiceImpl implements PersonService{
    @Autowired
    private PersonDao dao;
    @TxAnnotation
    @Cacheable("add")
    @Override
    public void savePerson(Person person) {
        dao.savePerson(person);
    }
    @TxAnnotation
    @Cacheable("get")
    @Override
    public Person getPerson(int id) {
        Person person = dao.getPerson(id);
        return person;
    }
    @TxAnnotation
    @Cacheable("del")
    @Override
    public void delPerson(int id) {
        dao.delPerson(id);
    }
}
      
@Component
@Aspect
public class TxAspect {
    @Around(value = "execution(* com.tj..*(..)) && @annotation(ann)")
    public Object around(ProceedingJoinPoint pjp ,TxAnnotation ann  ) throws Throwable{
        Object result = null;
        try{
            TxManage.stattx();
            result = pjp.proceed();
            TxManage.commitTx();
        }catch(Exception e){
            TxManage.rollback();
        }
        return result; 
    }
}
   :                 @PrivilegeInfo       
PrivilegeInfo(name=”add”)                 add     
         
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface PrivilegeInfo {
    String value() ;
}
                  
@Component
public class PersonServlet {
    @Autowired
    private PersonService ps;
    /**
     *       
     * */
    @PrivilegeInfo("add")
    public void  savePerson(Person person){
//      int  i  = 1/0;
        ps.savePerson(person);
    }
    /**
     *       
     * */
    @PrivilegeInfo("get")
    public Person getPerson(int id){
        Person person = ps.getPerson(id);
        return person ; 
    }
    /**
     *       
     * */
    @PrivilegeInfo("del")
    public void delPerson(int id  ){
        ps.delPerson(id);
    }
}
       
@Component
@Aspect
public class PrivilegInfoAspect {
    //       
    List list =Arrays.asList("add" , "get");
    @Around("execution(* com.tj..*(..)) && @annotation(ann)")
    public Object around(ProceedingJoinPoint pjp , PrivilegeInfo ann) throws Throwable{
        String value = ann.value();
        Object result = null;
        if(list.contains(value)){
            System.out.println("         !");
            result = pjp.proceed();
        }else{
            System.out.println("        , ");
        }
        return result;
    }
}
      :1.savePerson                    (Map)      
2.getPerson     Map                   ,                   
3.       getPerson                               Person        ,              
          
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Cacheable {
    String value();
}
            
@Component
public class PersonServiceImpl implements PersonService{
    @Autowired
    private PersonDao dao;
    @TxAnnotation
    @Cacheable("add")
    @Override
    public void savePerson(Person person) {
        dao.savePerson(person);
    }
    @TxAnnotation
    @Cacheable("get")
    @Override
    public Person getPerson(int id) {
        Person person = dao.getPerson(id);
        return person;
    }
    @TxAnnotation
    @Cacheable("del")
    @Override
    public void delPerson(int id) {
        dao.delPerson(id);
    }
}
       
@Component
@Aspect
public class CacheableAspect {
    //  
    Map map =  new HashMap();
    @Around("execution(* com.tj..*(..))&& @annotation(ann)")
    public Object befer(ProceedingJoinPoint jp  , Cacheable ann) throws Throwable{
        Object result = null;
        String v = ann.value();
        if(v.equals("add")){
            Person  person = (Person) jp.getArgs()[0];
            int id = person.getId();
            if(map.containsKey(id)){
                System.out.println("      ");
            }else{
                System.out.println("     ");
                map.put(id, person);
                result = jp.proceed();
            }
        }else if(v.equals("get")){
            int id = (Integer) jp.getArgs()[0];
            if(map.containsKey(id)){
                System.out.println("      ");
                result  = map.get(id);
            }else{
                result = jp.proceed();
                Person person =  (Person) result;
                map.put(person.getId(), person);
            }
        }
        return result;
    } 
}
   이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.