解决方案 »
- 连接数据库
- faultCode: {http://schemas.xmlsoap.or
- 用java如何把postgresql数据库中的表数据数据读取到XML文件中
- 讨论,SSH架构中,如何记录操作日志?
- org.apache.catalina.core.StandardWrapperValve invoke
- jdom取元素下所有数据
- 求助!!帮我做一个基于stryts2的个人图书管理系统
- 请教在Struts中如何将Excel表中的内容导入数据库
- java怎么抓取网站上的数据(比如新闻的标题和内容)还要保存到数据库中,请大神指教,最好有个小demo
- [紧急]resin下Mastering Jakarta Struts得employee的例子!
- 这两句代码是神马意思?
- 菜鸟求助!!自己调了大半天了还是没有解决!
这里有问题吧 execution(* test.*.work(..))修改成 execution(* test..*.*(..)) 试试
public class Main {
public static void main(String[] args) throws Exception{
// BeanFactory factory = new XmlBeanFactory(new FileSystemResource("bin/applicationContext.xml"));
// MyBean bean = (MyBean)factory.getBean("myBean");
// bean.work();
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
MyBean bean = (MyBean)context.getBean("myBean");
bean.work();
System.out.println(bean.getClass().getName());
}
}被注释掉的是你的代码。
你的MyBean是一个类,而不是一个接口的实现类。所以spring不得不用cglib帮你生成了MyBean的子类,来构成代理。我想原因可能是BeanFactory不会用cglib。但是,但是ApplicationContext的实现类会这么干。得到的结论是:没有特殊原因,不要用BeanFactory,用ApplicationContext最好!
/* (non-Javadoc)
* @see test.BeanInterface#work()
*/
public void work(){
System.out.println("Do something");
}
}
package test;import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;public class Main {
public static void main(String[] args) throws Exception{
BeanFactory factory = new XmlBeanFactory(new FileSystemResource("./src/applicationContext.xml"));
BeanInterface bean = (BeanInterface)factory.getBean("myBean");
//ApplicationContext context = new FileSystemXmlApplicationContext("./src/applicationContext.xml");
//BeanInterface bean = (BeanInterface)context.getBean("myBean");
bean.work();
}
}
程序运行不报错,只是AOP切点前后的方法没有被调用
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
log before bean method
Do something
log after bean method
test.MyBean$$EnhancerByCGLIB$$9f83794e
你要的AOP切点前后的方法调用都出来了啊。
难道是spring版本问题? 我用的是spring2.5.6。 你的呢?
改为:BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
2. <aop:config>
<aop:aspect ref="myLogger">
<aop:pointcut id="cut" expression="execution(* test.*.work(..))" />
<aop:before method="logBefore" pointcut-ref="cut" />
<aop:after-returning method="logAfter" pointcut-ref="cut" />
</aop:aspect>
改为:<aop:config>
<aop:aspect ref="myLogger">
<aop:pointcut id="cut" expression="execution(* test.*.work(..))" />
<aop:before method="logBefore" pointcut-ref="cut" />
<aop:after-returning method="logAfter" pointcut-ref="cut" />
</aop:aspect>
</aop:config>
在我电脑上能运行!!!LZ试下!!