2007-12-18
Spring 2 AOP编程问题
spring 的aop功能怎么不起作用。
大家帮我看看:
java 代码
- /**
- *
- */
- package com.dongyun.platform.test;
- /**
- * @author Administrator
- *
- */
- public class AppKwikEMart implements KwikEMart {
- /* (non-Javadoc)
- * @see com.dongyun.platform.test.KwikEMart#buySquishee(com.dongyun.platform.test.Customer)
- */
- public Squishee buySquishee(Customer customer) {
- System.out.println("give you");
- return new Squishee();
- }
- }
java 代码
- /**
- *
- */
- package com.dongyun.platform.test;
- import java.lang.reflect.Method;
- /**
- * @author Administrator
- *
- */
- public class WelcomeAdvice {
- /* (non-Javadoc)
- * @see org.springframework.aop.MethodBeforeAdvice#before(java.lang.reflect.Method, java.lang.Object[], java.lang.Object)
- */
- public void before(Method arg0, Object[] arg1, Object arg2)
- throws Throwable {
- 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:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
- <aop:aspectj-autoproxy />
- <bean id="kwikEMartTarget"
- class="com.dongyun.platform.test.AppKwikEMart">
- </bean>
- <bean id="welcomeAdvice"
- class="com.dongyun.platform.test.WelcomeAdvice">
- </bean>
- <aop:config>
- <!-- 声明一个切入点。 -->
- <aop:aspect id="myAspect" ref="welcomeAdvice">
- <aop:pointcut id="apointcut"
- expression="execution(* com.dongyun.platform.test.AppKwikEMart.*(..))" />
- <aop:before method="before" pointcut-ref="apointcut" />
- </aop:aspect>
- </aop:config>
- </beans>
然后我在main函数里测试怎么不起作用。
java 代码
- package com.dongyun.platform.test;
- public class Main {
- public static void main(String[] args){
- ClassPathResource resource = new ClassPathResource("applicationContext.xml");
- BeanFactory factory = new XmlBeanFactory(resource);
- // AppKwikEMart app = (AppKwikEMart) factory.getBean("kwikEMartTarget");
- AppKwikEMart app = new AppKwikEMart();
- Customer cus = new Customer("chxkyy");
- app.buySquishee(cus);
- System.out.println("hello world!");
- }
- }
出来的结果只是:
give you
hello world!
为什么不打印Hello !呢?
评论
chxkyy
2008-01-04
name 写道
也刚碰到这个问题.谢谢啊...但这是什么原因啊?为什么 factory.getBean("myAspect"); 这样写就是可以呢?
KwikEMart app = (KwikEMart) factory.getBean("kwikEMartTarget");
这样写。stone7已经解释清楚。
east_java
2008-01-02
也刚碰到这个问题.谢谢啊...但这是什么原因啊?为什么 factory.getBean("myAspect"); 这样写就是可以呢?
chxkyy
2007-12-25
谢谢,问题解决!
stone7
2007-12-21
1.使用ApplicationContext,而不是BeanFactory
2.使用接口作为getBean返回的引用类型,因为该引用此时是代理。除非你的bean没有实现接口,返回的是子类。
2.使用接口作为getBean返回的引用类型,因为该引用此时是代理。除非你的bean没有实现接口,返回的是子类。
引用
ApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");
KwikEMart app = (KwikEMart) factory.getBean("kwikEMartTarget");
KwikEMart app = (KwikEMart) factory.getBean("kwikEMartTarget");
地方疙瘩人
2007-12-19
我也在做这个同样的问题找了半天找到了
main 中的
AppKwikEMart app = (AppKwikEMart) factory.getBean("kwikEMartTarget"); --错了应当是 factory.getBean("myAspect");
main 中的
AppKwikEMart app = (AppKwikEMart) factory.getBean("kwikEMartTarget"); --错了应当是 factory.getBean("myAspect");
chxkyy
2007-12-19
[quote="liusong1220"]WelcomeAdvice 要 impletments MethodBeforeAdvice[/quote]
我加了也还是不出来:
我加了也还是不出来:
java 代码
- /**
- *
- */
- package com.dongyun.platform.test;
- import java.lang.reflect.Method;
- import org.springframework.aop.MethodBeforeAdvice;
- /**
- * @author Administrator
- *
- */
- public class WelcomeAdvice implements MethodBeforeAdvice {
- public void before(Method arg0, Object[] arg1, Object arg2)
- throws Throwable {
- System.out.println("Hello !");
- }
- }
liusong1220
2007-12-19
WelcomeAdvice 要 impletments MethodBeforeAdvice
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 17972 次
- 性别:

- 来自: 上海

- 详细资料
搜索本博客
我的相册
ie7
共 2 张
共 2 张
最近加入圈子
链接
最新评论
-
Ajax4jsf 和 <h:message> ...
给<h:message> 加个ID标识 <a4j:commandBu ...
-- by zuzuzuu -
response.sendRedirect(lo ...
说的很详细,谢了啊
-- by ungshow -
Ajax4jsf 和 <h:message> ...
能把你的页面的代码贴上来看一下吗?包括<h:message>
-- by chxkyy -
Ajax4jsf 和 <h:message> ...
<a4j:commandButton value="Add" style ...
-- by rainingrose -
Firefox 3没办法用QZone?
stevenwang 写道ff3对中文支持的不好。访问个别网站乱码。查看->字符 ...
-- by qubic






评论排行榜