首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

代码复建 spring自动装配 struts动态方法

2014-04-23 
代码重构 spring自动装配 struts动态方法然后新的action配置如下

代码重构 spring自动装配 struts动态方法

然后新的action配置如下

<package name="admin" extends="json-default" namespace="/admin/dep">
??? ??? <action name="*" method="{1}">
??? ??? ??? <result name="abc">/12.jsp</result>
??? ??? </action>
?</package>

2:修改原来spring的使用方式

遇到的问题1

在applicationContext.xml文件中新增

<context:component-scan base-package="*.*" />

?

出现问题

Caused by: org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.
??? at org.springframework.aop.framework.DefaultAopProxyFactory.createAopProxy(DefaultAopProxyFactory.java:67)
??? at org.springframework.aop.framework.ProxyCreatorSupport.createAopProxy(ProxyCreatorSupport.java:104)
??? at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:112)
??? at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:476)
??? at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:362)
??? at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:322)
??? at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:407)
??? at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1426)
??? at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
??? ... 24 more

加入jar包 cglib-nodep-2.2.jar 问题解决

然后spring还是没有装配好

然后在寻找问题,发现还少配置

新增配置 <aop:aspectj-autoproxy proxy-target-/>

然后发现有报错

Caused by: java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Aspect
??? at org.springframework.aop.aspectj.annotation.AbstractAspectJAdvisorFactory.hasAspectAnnotation(AbstractAspectJAdvisorFactory.java:119)
??? at org.springframework.aop.aspectj.annotation.AbstractAspectJAdvisorFactory.isAspect(AbstractAspectJAdvisorFactory.java:115)
??? at org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator.isInfrastructureClass(AnnotationAwareAspectJAutoProxyCreator.java:100)
??? at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessBeforeInstantiation(AbstractAutoProxyCreator.java:278)
??? at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:848)
??? at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:820)
??? at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:446)
??? ... 23 more

加入jar包 aspectjrt-1.5.4.jar
aspectjweaver-1.6.11.jar 后不报错了,但是service还是空。

继续寻找问题。

网上搜索可能是需要struts和spring的自动装配的配置,然后在struts配置文件中增加

?<bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" />
??
??? <!--? Make the Spring object factory the automatic default -->
??? <constant name="struts.objectFactory" value="spring" />

?

?然后报错

Caused by: Unable to load bean: type:com.opensymphony.xwork2.ObjectFactory class:org.apache.struts2.spring.StrutsSpringObjectFactory - bean - file:/D:/tomcat/tomcat-7/webapps/admin/WEB-INF/classes/struts.xml:10:129
??? at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:232)
??? at org.apache.struts2.config.StrutsXmlConfigurationProvider.register(StrutsXmlConfigurationProvider.java:101)
??? at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:180)
??? at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
??? ... 20 more
Caused by: java.lang.ClassNotFoundException: org.apache.struts2.spring.StrutsSpringObjectFactory
??? at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
??? at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
??? at com.opensymphony.xwork2.util.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:157)
??? at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:203)
??? ... 23 more

网上搜索后发现是少jar包

加上jar包 struts2-spring-plugin-2.1.8.1.jar 后

报错

Caused by: Unable to load bean: type:com.opensymphony.xwork2.ObjectFactory class:org.apache.struts2.spring.StrutsSpringObjectFactory - bean - file:/D:/tomcat/tomcat-7/webapps/admin/WEB-INF/classes/struts.xml:10:129
??? at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:232)
??? at org.apache.struts2.config.StrutsXmlConfigurationProvider.register(StrutsXmlConfigurationProvider.java:101)
??? at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:180)
??? at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
??? ... 20 more
Caused by: Bean type class com.opensymphony.xwork2.ObjectFactory with the name spring has already been loaded by [unknown location] - bean - file:/D:/tomcat/tomcat-7/webapps/admin/WEB-INF/classes/struts.xml:10:129
??? at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:216)
??? ... 23 more

是bean重复创建的问题

删除? <bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" />
问题解决。

久违的自动装配bean成功装配好

?

?

?

?

热点排行