写了一个Interceptorpackage com.spring.aop;import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;@Aspect
@Component
public class LogInterceptor {

@Pointcut("execution(public * com.spring.aop.AopService.say(..))")
public void myMethod(){

};
@Before("myMethod()")
public void before(){
System.out.println("method before");
}
@After("myMethod()")
public void after(){
System.out.println("method end");
}
}
一个需要被拦截的类:AopServicepackage com.spring.aop;import org.springframework.stereotype.Component;@Component("aopservice")
public class AopService {
String name = "aa";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void say(){
System.out.println("hello!");
}
}applicationContext.xml文件里的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config />
 <context:component-scan base-package="com.spring"></context:component-scan>
 <aop:aspectj-autoproxy/>
</beans>Test类:
package com.spring.aop;import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;public class Test {
@org.junit.Test
public void test(){
XmlBeanFactory f = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
AopService service = (AopService) f.getBean("aopservice");
service.say();
}
}
后台打印:
log4j:WARN No appenders could be found for logger (org.springframework.beans.factory.xml.XmlBeanDefinitionReader).
log4j:WARN Please initialize the log4j system properly.
hello!不知道为什么并没有打印拦截器所要输出的内容,不知道问题出在哪儿了。因为是初学spring,还不是很懂,希望各位能帮帮忙!谢谢啦!

解决方案 »

  1.   

    要引近来Spring 的log jar包和log.properties在Spring下载下来的文件里面好好找找,学一遍,这辈子就那样了
      

  2.   

    问题解决了!
    刚开始的是用Myeclipse添加的spring的属性,怎么都打印不出来;后来我重新自己手动加入下载的jar后,后台就能够打印出Inteceptor要打印的内容了!
      

  3.   

    而且我发现在Test类里,XmlBeanFactory f = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
    这样是不会打印出拦截器要求输出的内容,而换成ClassPathXmlApplicationContext就可以。
    这是为什么呢?
      

  4.   

    package com.sxzlc.test.rc4;public class RC4 {
    public  static byte[] RC42(byte[] aInput, byte[] aKey) {
    int[] iS = new int[256];
    byte[] iK = new byte[256]; for (int i = 0; i < 256; i++)
    iS[i] = i; int j = 1; for (short i = 0; i < 256; i++) {
    iK[i] = (byte) aKey[i%aKey.length];
    } j = 0; for (int i = 0; i < 256; i++) {
    j = (j + iS[i] + iK[i]) &0xff;
    int temp = iS[i]&0xff;
    iS[i] = iS[j]&0xff;
    iS[j] = temp;
    } int i = 0;
    j = 0; for (int x = 0; x < aInput.length; x++) {
    i = (i + 1) &0xff;
    j = (j + iS[i]) &0xff;
    int temp = iS[i]&0xff;
    iS[i] = iS[j]&0xff;
    iS[j] = temp;
    int t = ((iS[i] + iS[j] ))&0xff;
    int iY = iS[t];
    byte iCY = (byte) (iY&0xff);
    aInput[x] = (byte) ((aInput[x]^iCY)&0xff);
    } return aInput; }
        

    public static void main(String[] args) 

    String inputStr = "你们好啊"; 
    String key = "961633"; 
    byte[] bytes = inputStr.getBytes();
    //打印加密后的字符串 
    bytes = RC4.RC42(inputStr.getBytes(), key.getBytes());
    inputStr = new String(bytes);
    System.out.println( inputStr ); 
    //打印加密后的字符串 
    bytes = RC4.RC42(inputStr.getBytes(), key.getBytes());
    inputStr = new String(bytes);
    System.out.println( inputStr ); 
    }}