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";
}
}