目标类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;
}}
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;
}}
解决方案 »
- 怎么用axis2设置soap header 发送请求
- el表达式如何判断时间大小
- 谁能给详细介绍下LDAP的基本知识和用法
- [求助大家]关于Web Service调用时出现"no jbi part element"的问题
- jdbc插入oracle数据库clob类型的字段中文乱码
- javamail相关问题
- 用java 对base64中文utf-8进行 base64 解码后最后一个字 变为 ? 问题 help
- struts的tiles框架与html的frame框架都可用于网页布局,但两者相比有什么优势?
- 快进来看看:有没有把obj和xml string互相的方法或现成类库
- 请问用URL类连接到一个bbs网站后怎么注销呀?
- 求java打印预览核心代码
- 数据库层和jsp层测试都正确但是db层在servlet交互时出错
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));
}}
<!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>
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);
}}
log4j:WARN No appenders could be found for logger (org.springframework.core.CollectionFactory).
log4j:WARN Please initialize the log4j system properly.
使用#ffc800颜色打印Spring....
...框架!
问题:为什么没有调前置通知?
这句获得的Bean应该是printer的代理而不是printer,将此句改为:
Printer printer=(Printer)context.getBean("printerSerivce");
应该就可以了
at com.di.DIText.main(DIText.java:10)
数据类型转换报错???
我又从新建了个项目,按照思路重新做了一个,前置通知正确调用。
回想起刚才那个项目,我当时没是没把spring包拷到lib下,而且是1X版本又加入了2X版本,总之很乱 不知道是不是这个原因。我是新手,希望能得到前辈指点。