目标类package com.di;import com.di.ink.Ink;
import com.di.paper.Paper;public class Printer implements Iprint{
private Ink ink=null;
private Paper paper=null;
public void print(String str) {
System.out.println("使用"+ink.getColor(255, 200, 0)+"颜色打印\n");
for (int i = 0; i < str.length(); i++) {
paper.InputChar(str.charAt(i));
}
System.out.println(paper.getContent());
}
public Ink getInk() {
return ink;
}
public void setInk(Ink ink) {
this.ink = ink;
}
public Paper getPaper() {
return paper;
}
public void setPaper(Paper paper) {
this.paper = paper;
}}

解决方案 »

  1.   

    拦截器package com.aop;import java.lang.reflect.Method;
    import java.util.Arrays;import org.springframework.aop.MethodBeforeAdvice;
    public class LogAdvice implements MethodBeforeAdvice{ public void before(Method m, Object[] args, Object target)
    throws Throwable {


    System.out.println("调用LogAdvice!!"+m.getName()+Arrays.toString(args));


    }}
      

  2.   

    配置文件<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans>
    <bean id="logAdvice" class="com.aop.LogAdvice"></bean>
    <bean id="colorInk" class="com.di.ink.ColorInk"></bean>
    <bean id="greyInk" class="com.di.ink.GreyInk"></bean>
    <bean id="a4Paper" class="com.di.paper.TextPaper">
    <property name="charPerLine" value="10"></property>
    <property name="linePerPage" value="8"></property></bean>
    <bean id="b5Paper" class="com.di.paper.TextPaper">
    <property name="charPerLine" value="6"></property>
    <property name="linePerPage" value="5"></property></bean>
    <bean id="printer" class="com.di.Printer">
    <property name="ink" ref="colorInk"></property>
    <property name="paper" ref="a4Paper"></property></bean>

    <bean id="printerSerivce" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="proxyInterfaces">
    <value>com.di.Iprint</value>
    </property>
    <property name="interceptorNames">
    <list>
    <idref local="logAdvice"/>
    </list>
    </property>
    <property name="target" ref="printer"></property></bean>
    </beans>
      

  3.   

    测试类:package com.di;import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class DIText {
    public static void main(String[] ages) {
    ApplicationContext context=new ClassPathXmlApplicationContext("di.xml");
    Printer printer=(Printer)context.getBean("printer");
    String string="Spring.......框架!";
    printer.print(string);
    }}
      

  4.   

    运行结果:
    log4j:WARN No appenders could be found for logger (org.springframework.core.CollectionFactory).
    log4j:WARN Please initialize the log4j system properly.
    使用#ffc800颜色打印Spring....
    ...框架!
    问题:为什么没有调前置通知?
      

  5.   

    Printer printer=(Printer)context.getBean("printer");
    这句获得的Bean应该是printer的代理而不是printer,将此句改为:
    Printer printer=(Printer)context.getBean("printerSerivce");
    应该就可以了
      

  6.   

    楼上说的很对,我也是一时糊涂。按照上面的做法还是报错:Exception in thread "main" java.lang.ClassCastException: $Proxy0 cannot be cast to com.di.Printer
    at com.di.DIText.main(DIText.java:10)
    数据类型转换报错???
    我又从新建了个项目,按照思路重新做了一个,前置通知正确调用。
    回想起刚才那个项目,我当时没是没把spring包拷到lib下,而且是1X版本又加入了2X版本,总之很乱 不知道是不是这个原因。我是新手,希望能得到前辈指点。