天天看點

Spring aop 日志執行個體

spring AOP實作的記錄方法調用的日志功能的應用執行個體:目的是記錄 系統登入功能 的執行情況,技術架構簡單采用Spring+Struts。

1.登入頁面:

<formaction="login.do"method="post">

username:<inputtype="text"name="username"/><br>

password:<inputtype="password"name="password"><br>

<inputtype="submit"value="login">

</form>
           

2.表單類:

publicclassLoginActionForm extends ActionForm {

privateString username;

privateString password;

//setter,getter方法

}
           

3.Action類:

publicclassLoginAction extends Action {

privateILoginService loginService;

@Override

publicActionForward execute (ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)

throws Exception {

LoginActionForm laf = (LoginActionForm) form;

Stringusername = laf.getUsername();

Stringpassword = laf.getPassword();

boolean ret = loginService.login(username, password);

if(ret) {

returnmapping.findForward("success");

}

returnmapping.findForward("fail");

}

//loginService的setter,getter方法

}
           

4.service層接口:

publicinterfaceILoginService {

publicboolean login(StringuserName, Stringpassword);

}

publicinterfaceILogService {

publicvoidlog();

publicvoidlogArg(JoinPoint point);

publicvoidlogArgAndReturn(JoinPoint point, ObjectreturnObj);

}

           

5.service層實作類:

publicclassLoginServiceImpl implements ILoginService {

/**

*

* <p>功能實作描述:登入處理邏輯(這裡是假實作)</p>

*

* @see com.bruceyang.login.service.ILoginService#login(java.lang.String, java.lang.String)


*/

publicbooleanlogin(String userName, String password) {

StringBuffer sb=newStringBuffer();

sb.append("Target:").append("login:").append(userName).append(",").append(password);

System.out.println(sb.toString());

returntrue;

}

}
           
/**

*

* <p>[描述資訊:記錄檔記錄Service]</p>



*/

publicclassLogServiceImpl implementsILogService {

/**

*

* <p>功能實作描述:最簡單的情況</p>

*

* @see com.bruceyang.login.service.ILogService#log()

* @author: bruce.yang

* @date: Created on 2013-8-13 下午1:46:17

*/

publicvoidlog() {

System.out.println(newSimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(newDate())+" *******Log*********");

}
           
/**

*

* <p>功能實作描述:有參無傳回值的方法</p>

*

* @see com.bruceyang.login.service.ILogService#logArg(org.aspectj.lang.JoinPoint)



*/

publicvoidlogArg(JoinPoint point) {

StringBuffer sb=newStringBuffer();

//擷取連接配接點所在的目标對象

Object obj=point.getTarget();

//擷取連接配接點的方法簽名對象

String method=point.getSignature().getName();

//擷取連接配接點方法運作時的入參清單

Object[] args = point.getArgs();

sb.append(newSimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(newDate())).append(" ");

sb.append(obj.toString().substring(,obj.toString().indexOf('@')));

sb.append(".").append(method).append(" ");

sb.append("Args:[");

if(args != null) {

for(inti=;i<args.length;i++){

Object o=args[i];

sb.append(o);

if(i<args.length-){

sb.append(",");

}

}

}

sb.append("]");

System.out.println(sb.toString());

}
           
/**

*

* <p>功能實作描述:有參并有傳回值的方法</p>

*

* @see com.bruceyang.login.service.ILogService#logArgAndReturn(org.aspectj.lang.JoinPoint, java.lang.Object)

* @author: bruce.yang

* @date: Created on 2013-8-13 下午1:43:17

*/

publicvoidlogArgAndReturn(JoinPoint point, Object returnObj) {

StringBuffer sb=newStringBuffer();

//擷取連接配接點所在的目标對象

Object obj=point.getTarget();

//擷取連接配接點的方法簽名對象

String method=point.getSignature().getName();

//擷取連接配接點方法運作時的入參清單

Object[] args = point.getArgs();

sb.append(newSimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(newDate())).append(" ");

sb.append(obj.toString().substring(,obj.toString().indexOf('@')));

sb.append(".").append(method).append(" ");

sb.append("Args:[");

if(args != null) {

for(inti=;i<args.length;i++){

Object o=args[i];

sb.append(o);

if(i<args.length-){

sb.append(",");

}

}

}

sb.append("]").append(" ");

sb.append("Ret:[").append(returnObj).append("]");

System.out.println(sb.toString());

}

}
           

6.web.xml:

<!-- spring config -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:config/spring/app*.xml</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!-- struts config -->

<servlet>

<servlet-name>action</servlet-name>

<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

<init-param>

<param-name>config</param-name>

<param-value>/config/struts/struts-config.xml</param-value>

</init-param>

<init-param>

<param-name>debug</param-name>

<param-value>2</param-value>

</init-param>

<init-param>

<param-name>detail</param-name>

<param-value>2</param-value>

</init-param>

<load-on-startup>2</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>
           

7.spring配置資訊:

applicationContext.xml:

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"

xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx">

<beanid="logService"class="com.bruceyang.login.service.impl.LogServiceImpl"></bean>

<beanid="loginService"class="com.bruceyang.login.service.impl.LoginServiceImpl"></bean>

<aop:config>

<!-- 切入點 -->

<aop:pointcut

expression="execution(* com.bruceyang.login.service.impl.Login*.*(..))"

id="myPointcut"/>

<!-- 切面: 将哪個對象中的哪個方法,織入到哪個切入點 -->

<aop:aspectid="dd"ref="logService">

<!-- 前置通知

<aop:before method="log" pointcut-ref="myPointcut" />

<aop:after method="logArg" pointcut-ref="myPointcut"/>

<aop:after-returning method="logArgAndReturn" returning="returnObj" pointcut-ref="myPointcut"/>

-->

<aop:beforemethod="log"pointcut-ref="myPointcut"/>

<aop:aftermethod="logArg"pointcut-ref="myPointcut"/>

<aop:after-returningmethod="logArgAndReturn"returning="returnObj"pointcut-ref="myPointcut"/>

</aop:aspect>

</aop:config>

</beans>


7.Struts配置資訊:

  struts-config.xml:

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"

"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>

<form-beans>

<form-beanname="loginActionForm"type="com.bruceyang.login.web.form.LoginActionForm"/>

</form-beans>

<action-mappings>

<actionpath="/tologin"

forward="/login.jsp"

/>

<action

path="/login"

type="org.springframework.web.struts.DelegatingActionProxy"

name="loginActionForm"

scope="request"

input="/login.jsp">

<forwardname="success"path="/success.jsp"/>

<forwardname="fail"path="/login.jsp"/>

</action>

</action-mappings>

<plug-in

className="org.springframework.web.struts.ContextLoaderPlugIn">

<set-propertyproperty="contextConfigLocation"

value="/WEB-INF/classes/config/struts/action-servlet.xml"/>

</plug-in>

</struts-config>


action-servlet.xml:


<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<importresource="login-action.xml"/>

</beans>


login-action.xml:


<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<beanname="/login"

class="com.bruceyang.login.web.action.LoginAction">

<propertyname="loginService"ref="loginService"/>

</bean>

</beans>
           

繼續閱讀