问题描述
spring配置文件<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="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/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--注入配置文件--><!--<context:property-placeholderlocation="classpath:*.properties"/>--><!--使用annotation自动注册bean,并保证@Required,@Autowired的属性被注入--><context:component-scanbase-package="com.sun.oa"/><beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><propertyname="configLocation"value="classpath:hibernate.cfg.xml"></property></bean><beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><propertyname="sessionFactory"ref="sessionFactory"></property></bean><tx:adviceid="advice"transaction-manager="transactionManager"><tx:attributes><tx:methodname="save*"isolation="DEFAULT"propagation="REQUIRED"read-only="false"/><tx:methodname="update*"isolation="DEFAULT"propagation="REQUIRED"read-only="false"/><tx:methodname="delete*"isolation="DEFAULT"propagation="REQUIRED"read-only="false"/><tx:methodname="*"read-only="true"/></tx:attributes></tx:advice><aop:config><aop:pointcutexpression="execution(*com.sun.oa.service..*.*(..))"id="pointcut"/><aop:advisoradvice-ref="advice"pointcut-ref="pointcut"/></aop:config><!--加了下面后注入失败--><aop:configproxy-target-class="true"><aop:aspectref="operactionInterceptor"id="operactionInterceptor"><aop:pointcutexpression="execution(*com.sun.oa.action.*.*())"id="logpointcut"/><aop:aroundpointcut-ref="logpointcut"method="interceptor"/></aop:aspect></aop:config></beans>下面是interceptorpackagecom.sun.oa.aop;importjava.text.DateFormat;importjava.text.SimpleDateFormat;importjava.util.Date;importorg.apache.log4j.Logger;importorg.aspectj.lang.ProceedingJoinPoint;importorg.springframework.stereotype.Component;@ComponentpublicclassOperactionInterceptor{privatestaticLoggerlogger=Logger.getLogger(OperactionInterceptor.class);publicvoidinterceptor(ProceedingJoinPointjp){Stringmethod=jp.getSignature().getClass().getName();Stringaction=jp.getTarget().getClass().getName();longbefore=System.currentTimeMillis();try{jp.proceed();}catch(Throwablee){//TODOAuto-generatedcatchblocke.printStackTrace();}longafter=System.currentTimeMillis();DateFormatdf=newSimpleDateFormat("yyyy-MM-dd");logger.info(df.format(newDate())+"----Operaction:耗时["+(after-before)+"],action["+action+"],method["+method+"]");}}////////////////////////下面是actionpackagecom.sun.oa.action;importorg.apache.log4j.Logger;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.context.annotation.Scope;importorg.springframework.stereotype.Component;importcom.opensymphony.xwork2.ActionSupport;importcom.sun.oa.domain.TestUser;importcom.sun.oa.service.inter.ITestUserService;@Component@Scope("prototype")publicclassTestUserActionextendsActionSupport{privatestaticfinallongserialVersionUID=1L;//////////下面注入失败null@AutowiredprivateITestUserServicetestUserService;privatestaticLoggerlogger=Logger.getLogger(TestUserAction.class);publicStringsave(){logger.info("--------------------------------------------");TestUsertu=newTestUser();tu.setAddress("北苑路北");tu.setName("泰岳大厦");testUserService.save(tu);return"save";}}
解决方案
解决方案二:
struts.xml没有配置<constantname="struts.objectFactory.spring.autoWire.alwaysRespect"value="true"/>