先来阐述我的问题。我按照教程把Action作为spring的bean来管理。然后通过spring的IOC 获得service的IMPl没有问题
后来我发现如果不把Aciton交给spring管理 只要service的IMPl交给spring管理。struts的Action同样可以获得service的IMPL(我找了很多文章。大家都说获得的是空,但是我不知道为什么我可以获得)
我贴出我的部分代码
//@Component
//@Scope("prototype")
public class RegisterAction extends ActionSupport {
private UserModel user;
private UserRegister userRegister;

@Override
public String execute() throws Exception {
System.out.print(user.getUser_name() + user.getUser_password()
+ user.getUser_email());
if(userRegister.userRegister(user)){
return SUCCESS;
}else{
return ERROR;
}

}
public UserModel getUser() {
return user;
}
public void setUser(UserModel user) {
this.user = user;
}
public UserRegister getUserRegister() {
return userRegister;
}
//@Resource
public void setUserRegister(UserRegister userRegister) {
this.userRegister = userRegister;
}
}
这个是ACTION 注释掉的注解是我用来配饰SPRING的其中的UserRegister 已经被spring管理了<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
web。xml的内容
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
 
  <package name="default" extends="struts-default">
<action name="Register" class="registerAction">
<result name="success">interface/welcome.jsp</result>
<result name="error">interface/welcome.jsp</result>
</action>
</package>
struts。xml的内容我不是道是我配置隐藏了什么机制还是什么原因 我这个ACTION不交给spring管理就是可以获得UserRegister 这个实体类。请大侠们指教

解决方案 »

  1.   

    <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
     
      <package name="default" extends="struts-default">
    <action name="Register" class="xxx.xxx.xxx.RegisterAction">
    <result name="success">interface/welcome.jsp</result>
    <result name="error">interface/welcome.jsp</result>
    </action>
    </package>
    struts。xml的内容更正一下这里的CLASS 应该是这个。。就是说是全路径名
      

  2.   

    我以前用的一直都可以的,我认为是加载了struts-spring-plug.jar的缘故吧,bean可以直接诸如action吧!
      

  3.   

    classpath*:applicationContext*.xml
    贴下啊
      

  4.   

    你得先搞清楚Spring是什么你这个问的就像是发工资时为什么给你银行卡,而不给现金一样
    Spring是一种对象管理容器,使用spring管理对象就可以使框架之间解耦,因为所有对象是通过IOC插入的,而不是自己new出来的
    这东西一句两句说不清楚,自己去看Spring的文档把
      

  5.   

    spring有个默认的装备模式,是通过BYType的方式。就是该类的第一字母小写。而是用struts2-spring-plugin-2.1.8.1.jar会的action会自动交给spring处理。
      

  6.   

    spring呢...他主要功能就是IOC跟一个事务的管理..你把action交给spring管理的话...你struts2就不用这么麻烦,加管理事务的代码....也可以增加不同的逻辑
      

  7.   

    applicationContext-email
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <context:annotation-config />
    <context:component-scan base-package="com.DanielsViki" /> <bean id="mailSender"
    class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="smtp.gmail.com" />
    <property name="username" value="[email protected]" />
    <property name="password" value="zwjun_01" />
    <property name="javaMailProperties">
    <props>
    <prop key="mail.smtp.auth">true</prop>
    <prop key="mail.smtp.timeout">25000</prop>
    <prop key="mail.smtp.port">465</prop>
    <prop key="mail.smtp.socketFactory.port">465</prop>
    <prop key="mail.smtp.socketFactory.fallback">
    false
    </prop>
    <prop key="mail.smtp.socketFactory.class">
    javax.net.ssl.SSLSocketFactory
    </prop>
    </props>
    </property>
    </bean> <bean id="templateMessage"
    class="org.springframework.mail.SimpleMailMessage">
    <property name="from" value="www.DanielsViki.com" />
    <property name="subject" value="thank for your register" />
    </bean>
    </beans>
    applicationContext-hibernate
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <context:annotation-config />
    <context:component-scan base-package="com.DanielsViki" /> <bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
    <value>classpath:dbcp.properties</value>
    </property>
    </bean>
    <bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName">
    <value>${driverClass}</value>
    </property>
    <property name="url">
    <value>${url}</value>
    </property>
    <property name="username">
    <value>${username}</value>
    </property>
    <property name="password">
    <value>${password}</value>
    </property>
    </bean> <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mappingResources">
    <list>
    <value>com/DanielsViki/model/UserModel.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    ${hibernate.dialect}
    </prop>
    <prop key="hibernate.show_sql">true</prop>
    </props>
    </property>
    </bean> <bean id="hibernateTemplate"
    class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean> <bean id="txManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean> <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
    <tx:method name="get*" read-only="true" />
    <tx:method name="*" />
    </tx:attributes>
    </tx:advice> <aop:config>
    <aop:pointcut
    expression="execution(* com.DanielsViki.service.impl.*.*(..))"
    id="hibernateAOP" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="hibernateAOP" />
    </aop:config>

    </beans>
    这个就是applicationContext
      

  8.   


    各位是这样吗?使用了struts2-spring-plugin-2.1.8.1.jar 后 action会自动交给spring处理?
      

  9.   

    web.xml里的这条常量就是对spring对action的进行管理的支持。
    <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
      

  10.   

    使用spring管理对象就可以使框架之间解耦,因为所有对象是通过IOC插入的,而不是自己new出来的
      

  11.   

    <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
    应该就是这块起得作用吧。
      

  12.   

    The Spring Plugin works by overriding the Struts ObjectFactory to enhance the creation of core framework objects. When an object is to be created, it uses the class attribute in the Struts configuration to correspond to the id attribute in the Spring configuration. If not found, the class will try to be created as usual, then be autowired by Spring. In the case of Actions, Spring 2's bean scope feature can be used to scope an Action instance to the session, application, or a custom scope, providing advanced customization above the default per-request scoping.Spring插件的工作原理是通过重写Struts的ObjectFactory方法来加强创造核心框架对象。当一个对象被创建的时候,它利用Struts配置文件中的class属性和Spring配置文件中的id相对应。如果没有发现,那么类将被创建为一个普通的,然后被Spring自动装配。在action实例中,Spring2 bean 的范围可以被用在Action实例的范围,session,application,或者是个自定义的范围。提供高级的定制基于默认的每个请求范围。
      

  13.   

    如果你的struts.xml对应action的class属性不使用spring声明的action,而是直接写action的具体路径的话,这样也是可以的,只不过这样写你的action是由struts创建的。建议还是交给spring来管理,并且指明action的scope是prototype,这样可以避免一些问题
      

  14.   

    这个有理有据,讲得很清楚了。
    struts 中的action是可以不交给spring管理的(即不需要@Component和@Scope("prototype"))。
    这时spring-struts 的插件会自动转配上action依赖的那些@resource javabean。此时的action由strus来管理, 也不能使用spring的事务管理。