spring AOP的困惑,使用AOP后原来注入的bean丢失

liuheworld 2011-10-12 07:42:26
spring3 struts2 hibernate3.5

Action 中注入Bean
@Resource
private HibernateTemplate ht;
构造方法执行一次

可以使用,但是当我写了一个切入程序切入 Action 后,构造方法执行两次,ht=null ,怀疑是切入造成的。切入后构造方法执行两次,可能的原因是ht第一次注入,执行第二次的时候ht没有被注入。
问题是:
既然是切入,Action也不应该构造两次啊,愿听各位指教!
...全文
600 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuheworld 2011-10-13
  • 打赏
  • 举报
回复
难道 Action 不能切入吗?或者说是不能进行事务管理?
liuheworld 2011-10-13
  • 打赏
  • 举报
回复
修改了,不起作用,现在只有一种方式了,代码如下:

<aop:config>
<aop:pointcut id="allServiceMethod"
expression="execution(* com.test.erp.test.action.*.*(..))" />

<aop:advisor advice-ref="txAdvice" pointcut-ref="allServiceMethod" />
</aop:config>

<bean class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

@Resource
private HibernateTemplate ht;


[Quote=引用 4 楼 wjacketcn 的回复:]

@Aspect
@Component
public class BeforeAdvice{

@Pointcut("execution(* com.test.erp.test.action.*.*(..))")
public void rolePoint(){}

@Before("rolePoint()")
public void bef……
[/Quote]
wjacketcn 2011-10-13
  • 打赏
  • 举报
回复
@Aspect
@Component
public class BeforeAdvice{

@Pointcut("execution(* com.test.erp.test.action.*.*(..))")
public void rolePoint(){}

@Before("rolePoint()")
public void beforeAdvice() {
System.out.println("before advice is executed!");
}


}

用这种注解的方式就可以了,不需要在配置文件里再写:

<aop:config>

<aop:pointcut id="allServiceMethod"
expression="(execution(* com.test.erp.core.service.impl.*.*(..)) or
execution(* com.test.erp.test.service.impl.*.*(..)) or
execution(* com.test.erp.test.action.*.*(..))
)" />

<aop:advisor advice-ref="txAdvice" pointcut-ref="allServiceMethod" />
</aop:config>

或只写配置文件,不用:

@Aspect
@Component
public class BeforeAdvice{

@Pointcut("execution(* com.test.erp.test.action.*.*(..))")
public void rolePoint(){}

@Before("rolePoint()")
public void beforeAdvice() {
System.out.println("before advice is executed!");
}


}


这两种方式是一样的,只用一种方式就行,不然就重复了。
liuheworld 2011-10-13
  • 打赏
  • 举报
回复
<aop:config>

<aop:pointcut id="allServiceMethod"
expression="(execution(* com.test.erp.core.service.impl.*.*(..)) or
execution(* com.test.erp.test.service.impl.*.*(..)) or
execution(* com.test.erp.test.action.*.*(..))
)" />

<aop:advisor advice-ref="txAdvice" pointcut-ref="allServiceMethod" />
</aop:config>


<bean id="ht" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>


现象是我在没加入 红色部分之前是可以用
@Resource
private HibernateTemplate ht;
加入红色部分(
execution(* com.test.erp.test.action.*.*(..))
)后, ht 注入不进去,求高手解答
liuheworld 2011-10-13
  • 打赏
  • 举报
回复
MemberAction!
2011-10-13 08:55:04,108 [http-8080-3] DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0'
2011-10-13 08:55:04,108 [http-8080-3] DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0'
2011-10-13 08:55:04,109 [http-8080-3] DEBUG [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator] - Creating implicit proxy for bean 'com.test.erp.test.action.MemberAction' with 0 common interceptors and 2 specific interceptors
2011-10-13 08:55:04,109 [http-8080-3] DEBUG [org.springframework.aop.framework.Cglib2AopProxy] - Creating CGLIB2 proxy: target source is SingletonTargetSource for target object [com.test.erp.test.action.MemberAction@1307957]
MemberAction!
2011-10-13 08:55:04,893 [http-8080-3] DEBUG [org.springframework.beans.factory.annotation.InjectionMetadata] - Processing injected method of bean 'com.test.erp.test.action.MemberAction': ResourceElement for private org.springframework.orm.hibernate3.HibernateTemplate com.test.erp.test.action.MemberAction.ht
2011-10-13 08:55:04,893 [http-8080-3] DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'ht'
before advice is executed!
2011-10-13 08:55:04,917 [http-8080-3] DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0'
2011-10-13 08:55:04,918 [http-8080-3] DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0'
before advice is executed!
before advice is executed!
before advice is executed!
before advice is executed!
before advice is executed!
before advice is executed!
请仁兄帮我分析一下日志
@Aspect
@Component
public class BeforeAdvice{

@Pointcut("execution(* com.test.erp.test.action.*.*(..))")
public void rolePoint(){}

@Before("rolePoint()")
public void beforeAdvice() {
System.out.println("before advice is executed!");
}


}

[Quote=引用 1 楼 fly_m 的回复:]

aop和依赖注入没有关系,你只需要保证 切入时所操作的对象,就是注入之后的对象即可。

难道你切入时所操作的对象是另外new出来的?
[/Quote]
liuheworld 2011-10-13
  • 打赏
  • 举报
回复
问题进一步确认,当标记有@Action 的方法做切入的话,这个Action里面注入的bean 都不能注入,什么原因呢
Fly_m 2011-10-12
  • 打赏
  • 举报
回复
aop和依赖注入没有关系,你只需要保证 切入时所操作的对象,就是注入之后的对象即可。

难道你切入时所操作的对象是另外new出来的?

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧