轉載地址:http://blog.csdn.net/sd0902/article/details/8393770
在Java開發中日誌的管理有很多種。我一般會使用過濾器,或者是spring的攔截器進行日誌的處理。如果是用過濾器比較簡單,只要對所有的.do提交進行攔截,然後獲取action的提交路徑就可以獲取對每個方法的調用。然後進行日誌記錄。使用過濾器的好處是可以自己選擇性的對某一些方法進行過濾,記錄日誌。但是實現起來有點麻煩。
另外一種就是使用Spring的AOP了。這種方式實現起來非常簡單,只要配置一下配置文件就可以了。可是這種方式會攔截下所有的對action的每個操作。使得效率比較低。不過想做詳細日誌這個方法還是非常好的。下面我就介紹一下使用Spring AOP進行日誌記錄的方式。
第一種。Spring AOP對普通類的攔截操作
首先我們要寫一個普通類,此類作爲日誌記錄類。 比如
- package chen.hui.log
- public classs MyLog{
- //在類裏面寫方法,方法名詩可以任意的。此處我用標準的before和after來表示
- public void before(){
- System.out.println("被攔截方法調用之前調用此方法,輸出此語句");
- }
- public void after(){
- System.out.println("被攔截方法調用之後調用此方法,輸出此語句");
- }
- }
- 其次我們在寫一個類作爲被攔截類(Spring的AOP就是攔截這個類裏面的方法)
- package chen.hui.log
- public class Test{//此類中方法可以寫任意多個。我只寫一個
- public void test(){
- Sytem.out.println("測試類的test方法被調用");
- }
- }
最後進行配置文件的編寫。在Spring的配置文件中我們需要進行幾句話的配置
- <bean id="testLog" class="chen.hui.log.MyLog"></bean> <!--將日誌類注入到bean中。-->
- <aop:config>
- <aop:aspect id="b" ref="testLog"><!--調用日誌類-->
- <aop:pointcut id="log" expression="execution(* chen.hui.log.*.*(..))"/><!--配置在log包下所有的類在調用之前都會被攔截-->
- <aop:before pointcut-ref="log" method="before"/><!--在log包下面所有的類的所有方法被調用之前都調用MyLog中的before方法-->
- <aop:after pointcut-ref="log" method="after"/>><!--在log包下面所有的類的所有方法被調用之前都調用MyLog中的after方法-->
- </aop:aspect>
- </aop:config>
到此處整個程序完成,在MyLog類裏面的before和after方法添加日誌邏輯代碼就可以完成日誌的管理。以上是對普通類的管理,如果只想攔截某一個類。只要把倒數第二個 * 改成類名就可以了。
第二:使用Spring AOP對action做日誌管理
如果是想攔截action對action做日誌管理,基本和上面差不多,但是要注意。以下幾點
首先還是要寫一個普通類,不過此類中的方法需要傳入參數。 比如
- package chen.hui.log
- import org.aspectj.lang.JoinPoint;
- public classs MyLog{
- //在類裏面寫方法,方法名詩可以任意的。此處我用標準的before和after來表示
- //此處的JoinPoint類可以獲取,action所有的相關配置信息和request等內置對象。
- public void before(JoinPoint joinpoint){
- joinpoint.getArgs();//此方法返回的是一個數組,數組中包括request以及ActionCofig等類對象
- System.out.println("被攔截方法調用之前調用此方法,輸出此語句");
- }
- public void after(JoinPoint joinpoint){
- System.out.println("被攔截方法調用之後調用此方法,輸出此語句");
- }
- }
其次我們在寫一個action類作爲被攔截類(Spring的AOP就是攔截這個類裏面的方法)
- package chen.hui.log
- public class LoginAction{//此類中方法可以寫任意多個。我只寫一個
- public void test(){
- Sytem.out.println("測試類的test方法被調用");
- }
- }
最後進行配置文件的編寫。在Spring的配置文件中我們需要進行幾句話的配置
- <bean id="testLog" class="chen.hui.log.MyLog"></bean> <!--將日誌類注入到bean中。-->
- <aop:config>
- <aop:aspect id="b" ref="testLog"><!--調用日誌類-->
- <aop:pointcut id="log" expression="execution(* chen.hui.log.*.*(..))"/><!--配置在log包下所有的類在調用之前都會被攔截-->
- <aop:before pointcut-ref="log" method="before"/><!--在log包下面所有的類的所有方法被調用之前都調用MyLog中的before方法-->
- <aop:after pointcut-ref="log" method="after"/>><!--在log包下面所有的類的所有方法被調用之前都調用MyLog中的after方法-->
- </aop:aspect>
- </aop:config>
除了參數外其他地方基本和普通類相似。
需要注意的是:普通類可以監控單一的類,而action在配置文件中只能到包名而不能到action的類名。不然會報錯。就是說如果要記錄日誌就要記錄所有的action而不能記錄其中一個,這是我試了好久得出的結果。
.實現登陸和日誌管理(使用Spring AOP
1)LoginService LogService TestMain
2)用Spring 管理 LoginService 和 LogService 的對象
3)確定哪些連接點是切入點,在配置文件中
4)將LogService封裝爲通知
5)將通知植入到切入點
6)客戶端調用目標
-
<aop:config>
-
<aop:pointcut expression="execution(* cn.com.spring.service.impl.*.*(..))" id="myPointcut"/>
-
<!--將哪個-->
-
<aop:aspect id="dd" ref="logService">
-
<aop:before method="log" pointcut-ref="myPointcut"/>
-
</aop:aspect>
- </aop:config>
execution(* * cn.com.spring.service.impl.*.*(..))
1)* 所有的修飾符
2)* 所有的返回類型
3)* 所有的類名
4)* 所有的方法名
5)* ..所有的參數名
1.ILoginService.java
-
package cn.com.spring.service;
-
-
public interface ILoginService {
-
public boolean login(String userName, String password);
- }
-
package cn.com.spring.service.impl;
-
-
import cn.com.spring.service.ILoginService;
-
-
public class LoginServiceImpl implements ILoginService {
-
-
public boolean login(String userName, String password) {
-
System.out.println("login:" + userName + "," + password);
-
return true;
-
}
-
- }
3.ILogService.java
-
package cn.com.spring.service;
-
-
import org.aspectj.lang.JoinPoint;
-
-
public interface ILogService {
-
//無參的日誌方法
-
public void log();
-
//有參的日誌方法
-
public void logArg(JoinPoint point);
-
//有參有返回值的方法
-
public void logArgAndReturn(JoinPoint point,Object returnObj);
- }
4.LogServiceImpl.java
-
package cn.com.spring.service.impl;
-
-
import org.aspectj.lang.JoinPoint;
-
-
import cn.com.spring.service.ILogService;
-
-
public class LogServiceImpl implements ILogService {
-
-
@Override
-
public void log() {
-
System.out.println("*************Log*******************");
-
}
-
-
//有參無返回值的方法
-
public void logArg(JoinPoint point) {
-
//此方法返回的是一個數組,數組中包括request以及ActionCofig等類對象
-
Object[] args = point.getArgs();
-
System.out.println("目標參數列表:");
-
if (args != null) {
-
for (Object obj : args) {
-
System.out.println(obj + ",");
-
}
-
System.out.println();
-
}
-
}
-
-
//有參並有返回值的方法
-
public void logArgAndReturn(JoinPoint point, Object returnObj) {
-
//此方法返回的是一個數組,數組中包括request以及ActionCofig等類對象
-
Object[] args = point.getArgs();
-
System.out.println("目標參數列表:");
-
if (args != null) {
-
for (Object obj : args) {
-
System.out.println(obj + ",");
-
}
-
System.out.println();
-
System.out.println("執行結果是:" + returnObj);
-
}
-
}
- }
-
<?xml version="1.0" encoding="UTF-8"?>
-
<beans xmlns="http://www.springframework.org/schema/beans"
-
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xmlns:p="http://www.springframework.org/schema/p"
-
xsi:schemaLocation="http://www.springframework.org/schema/beans
-
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
-
http://www.springframework.org/schema/aop
-
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
-
-
<bean id="logService" class="cn.com.spring.service.impl.LogServiceImpl"></bean>
-
<bean id="loginService" class="cn.com.spring.service.impl.LoginServiceImpl"></bean>
-
-
<aop:config>
-
<!-- 切入點 -->
-
<aop:pointcut
-
expression="execution(* cn.com.spring.service.impl.LoginServiceImpl.*(..))"
-
id="myPointcut" />
-
<!-- 切面: 將哪個對象中的哪個方法,織入到哪個切入點 -->
-
<aop:aspect id="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:aspect>
-
</aop:config>
- </beans>
- public class TestMain {
-
public static void testSpringAOP(){
-
ApplicationContext ctx = new ClassPathXmlApplicationContext("app*.xml");
-
-
ILoginService loginService = (ILoginService)ctx.getBean("loginService");
- loginService.login("zhangsan", "12344");
- }
- public static void main(String[] args) {
- testSpringAOP();
- }
- }
-
login:zhangsan,12344
-
目標參數列表:
-
zhangsan,
-
12344,
-
- 執行結果是:true
2.再調用了logArgAndReturn()方法輸出了日誌,並且返回了login()方法是否成功
-
System.out.println("目標參數列表:");
-
if (args != null) {
-
for (Object obj : args) {
-
System.out.println(obj + ",");
-
}
-
System.out.println();
-
System.out.println("執行結果是:" + returnObj);
- }