`
fuerbosi
  • 浏览: 465380 次
文章分类
社区版块
存档分类
最新评论

Spring AOP 用法初探

 
阅读更多

以下从E文中摘得:

Common AspectJ annotations :

  1. @Before – Run before the method execution
  2. @After – Run after the method returned a result
  3. @AfterReturning – Run after the method returned a result, intercept the returned result as well.
  4. @AfterThrowing – Run after the method throws an exception
  5. @Around – Run around the method execution, combine all three advices above.

大致讲了下切面的情况,在这里我举个例子,来说明在使用过程中的调用关系。
1.在配置文件applicationContext.xml:
	<bean id="strategyAspect" class="cn.iscas.ac.gz.sems.domain.strategy.aop.ControllorStrategyAspect">
		<property name="concentratorDomainService" ref="concentratorDomainService" />
    </bean>

2.要切的JAVA类,这里写接口,实现略。
public int turnOnOffLightByTermId(String concentUID, String termUID, String operateMode, boolean isTurnOnLight) throws Exception;
	
	
public int turnOnOffLightByGroupId(String concentUID, int GroupId, String operateMode, boolean isTurnOnLight) throws Exception;
	
	
public int turnOnOffLightByConcentId(String concentUID, String operateMode, boolean isTurnOnLight) throws Exception;
	
	
public int dimmingByTermId(String concentUID,String termUID, int dimmingValue) throws Exception ;

	
public int dimmingByGroupId(String concentUID,int GroupId, int dimmingValue) throws Exception;
	
	
public int dimmingByConcentId(String concentUID, int dimmingValue) throws Exception ;
	
	
public int queryLightByTermId(String concentUID, String termUID) throws Exception;
	
	
public int queryLightByGroupId(String concentUID, int GroupID) throws Exception;
	
	
public int queryLightByConcentId(String concentUID) throws Exception;
	
	

3.切面类ControllorStrategyAspect.java
@Aspect
public class ControllorStrategyAspect {

	private ConcentratorDomainService concentratorDomainService;
	private static final Logger logger = Logger.getLogger(ControllorStrategyAspect.class);
	public ConcentratorDomainService getConcentratorDomainService() {
		return concentratorDomainService;
	}

	public void setConcentratorDomainService(ConcentratorDomainService concentratorDomainService) {
		this.concentratorDomainService = concentratorDomainService;
	}

	@Pointcut("execution(* cn.iscas.ac.gz.sems.domain.internal.MonitorServerDomainServiceImpl.turnOnOffLight*(..))")
	void turnOnOffLightCommandExecution() {
           // ...............................................
	}
	
	@Pointcut("execution(* cn.iscas.ac.gz.sems.domain.internal.MonitorServerDomainServiceImpl.dimming*(..))")
	void dimmingCommandExecution() {
           //..............................................
	}
	
	@Pointcut("execution(* cn.iscas.ac.gz.sems.domain.internal.MonitorServerDomainServiceImpl.circuitPowerOnOff(..))")
	void circuitPowerOnOffCommandExecution() {
          //.........................................
	}

	@Around(" turnOnOffLightCommandExecution() || dimmingCommandExecution() || circuitPowerOnOffCommandExecution()")
	public Object commandExecutionPointCut(ProceedingJoinPoint joinPoint) throws Exception {
            ................................
	    return null;
	}
}

解释:
@Pointcut :里头写的就是要切入的类中方法位置,使用*表示正则表达式匹配。匹配里面的方法。
@Around: 里头是一些方法的组合,当这些方法发生时,会触发commandExecutionPointCut方法的执行。它只要是打印一下控制台日志。
concentratorDomainService对象使用手动注入的方式,在配置文件中申明注入的。

学习链接 :http://www.mkyong.com/spring3/spring-aop-aspectj-annotation-example/

(完,待续...........................................)
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics