ssh2使用aop后属性注入失败

秦歌666 2015-02-06 09:05:35
spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注入配置文件 -->
<!-- <context:property-placeholder location="classpath:*.properties"/> -->

<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
<context:component-scan base-package="com.sun.oa" />

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>

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

<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut expression="execution(* com.sun.oa.service..*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
</aop:config>
<!--加了下面后 注入失败-->
<aop:config proxy-target-class="true">
<aop:aspect ref="operactionInterceptor" id="operactionInterceptor">
<aop:pointcut expression="execution(* com.sun.oa.action.*.*())" id="logpointcut"/>
<aop:around pointcut-ref="logpointcut" method="interceptor"/>
</aop:aspect>
</aop:config>
</beans>
下面是interceptor
package com.sun.oa.aop;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

@Component
public class OperactionInterceptor {

private static Logger logger = Logger.getLogger(OperactionInterceptor.class);

public void interceptor(ProceedingJoinPoint jp) {
String method = jp.getSignature().getClass().getName();
String action = jp.getTarget().getClass().getName();
long before = System.currentTimeMillis();
try {
jp.proceed();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long after = System.currentTimeMillis();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
logger.info(df.format(new Date())+"----Operaction:耗时["+(after-before)+"],action["+action+"],method["+method+"]");
}
}
////////////////////////下面是action
package com.sun.oa.action;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.opensymphony.xwork2.ActionSupport;

import com.sun.oa.domain.TestUser;
import com.sun.oa.service.inter.ITestUserService;
@Component
@Scope("prototype")
public class TestUserAction extends ActionSupport{

private static final long serialVersionUID = 1L;
//////////下面注入失败 null
@Autowired
private ITestUserService testUserService;

private static Logger logger = Logger.getLogger(TestUserAction.class);

public String save() {
logger.info("--------------------------------------------");
TestUser tu = new TestUser();
tu.setAddress("北苑路北");
tu.setName("泰岳大厦");
testUserService.save(tu);
return "save";
}
}
...全文
95 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
秦歌666 2015-02-06
  • 打赏
  • 举报
回复
struts.xml 没有配置<constant name="struts.objectFactory.spring.autoWire.alwaysRespect" value="true" />

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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