You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Chris <cr...@gmx.us> on 2015/09/05 14:47:26 UTC

Spring Injection Strange behaviour

I'm trying to use the Spring plugin to inject a dependency into my action at runtime. This is the first time I've tried this with Struts2 so I may have missed something but I'm stumped because I can see the dependency is injected but then the object is later null when I need to use it. Here is my action class code:

public class UserRegisterAction extends ActionSupport {
    private User user = new User();

    @Override
    public String execute(){
        getUserService().saveUser(user);
        return "success";
    }

    public UserServiceImpl getUserService(){
        return userService;
    }

    UserServiceImpl userService;
    public void setUserService(UserServiceImpl us){
        this.userService = us;
    }
}

Using the debugger I can see that upon startup "setUserService is called and this.userService is set, so the injection appears to be working ok, but when the execute method is called when the page is submitted userService is null. I'm quite new to Struts but I don't understand how my userService is being null.

Can anyone see what I am missing?


My applicationContext:


<?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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" default-autowire="byName">

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
                <property name="database" value="MYSQL" />
                <property name="showSql" value="true" />
            </bean>
        </property>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.gjt.mm.mysql.Driver" />
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/baseapp" />
        <property name="username" value="xxxxxx" />
        <property name="password" value="xxxxxx" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="userService" class="com.xxxxx.service.UserServiceImpl" scope="prototype"/>

    <bean id="userAction" class="com.xxxxxx.action.UserRegisterAction">
        <property name="userService" ref="userService"/>
    </bean>

</beans>


Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

   <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Spring Injection Strange behaviour

Posted by Dave Newton <da...@gmail.com>.
Not sure, but if nothing else the action should be set to `prototype`
scope, as they're instantiated per-request.

Are you using the Struts 2 Spring plugin?

On Sat, Sep 5, 2015 at 8:47 AM, Chris <cr...@gmx.us> wrote:

> I'm trying to use the Spring plugin to inject a dependency into my action
> at runtime. This is the first time I've tried this with Struts2 so I may
> have missed something but I'm stumped because I can see the dependency is
> injected but then the object is later null when I need to use it. Here is
> my action class code:
>
> public class UserRegisterAction extends ActionSupport {
>     private User user = new User();
>
>     @Override
>     public String execute(){
>         getUserService().saveUser(user);
>         return "success";
>     }
>
>     public UserServiceImpl getUserService(){
>         return userService;
>     }
>
>     UserServiceImpl userService;
>     public void setUserService(UserServiceImpl us){
>         this.userService = us;
>     }
> }
>
> Using the debugger I can see that upon startup "setUserService is called
> and this.userService is set, so the injection appears to be working ok, but
> when the execute method is called when the page is submitted userService is
> null. I'm quite new to Struts but I don't understand how my userService is
> being null.
>
> Can anyone see what I am missing?
>
>
> My applicationContext:
>
>
> <?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:tx="
> http://www.springframework.org/schema/tx"
>        xsi:schemaLocation="http://www.springframework.org/schema/beans
> http://www.springframework.org/schema/beans/spring-beans.xsd
>        http://www.springframework.org/schema/tx
> http://www.springframework.org/schema/tx/spring-tx.xsd"
> default-autowire="byName">
>
>     <bean
> class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"
> />
>
>     <bean
> class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
>
>     <bean id="entityManagerFactory"
> class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
>         <property name="dataSource" ref="dataSource" />
>         <property name="jpaVendorAdapter">
>             <bean
> class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
>                 <property name="databasePlatform"
> value="org.hibernate.dialect.MySQL5Dialect" />
>                 <property name="database" value="MYSQL" />
>                 <property name="showSql" value="true" />
>             </bean>
>         </property>
>     </bean>
>
>     <bean id="dataSource"
> class="org.springframework.jdbc.datasource.DriverManagerDataSource">
>         <property name="driverClassName" value="org.gjt.mm.mysql.Driver" />
>         <property name="url" value="jdbc:mysql://127.0.0.1:3306/baseapp"
> />
>         <property name="username" value="xxxxxx" />
>         <property name="password" value="xxxxxx" />
>     </bean>
>
>     <bean id="transactionManager"
> class="org.springframework.orm.jpa.JpaTransactionManager">
>         <property name="entityManagerFactory" ref="entityManagerFactory" />
>     </bean>
>
>     <tx:annotation-driven transaction-manager="transactionManager" />
>
>     <bean id="userService" class="com.xxxxx.service.UserServiceImpl"
> scope="prototype"/>
>
>     <bean id="userAction" class="com.xxxxxx.action.UserRegisterAction">
>         <property name="userService" ref="userService"/>
>     </bean>
>
> </beans>
>
>
> Web.xml:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
>          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
> http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
>          version="3.1">
>
>    <filter>
>         <filter-name>struts2</filter-name>
>
> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
>     </filter>
>
>     <filter-mapping>
>         <filter-name>struts2</filter-name>
>         <url-pattern>/*</url-pattern>
>     </filter-mapping>
>
>     <listener>
>
> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
>     </listener>
> </web-app>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


-- 
e: davelnewton@gmail.com
m: 908-380-8699
s: davelnewton_skype
t: @dave_newton <https://twitter.com/dave_newton>
b: Bucky Bits <http://buckybits.blogspot.com/>
g: davelnewton <https://github.com/davelnewton>
so: Dave Newton <http://stackoverflow.com/users/438992/dave-newton>

RE: Spring Injection Strange behaviour

Posted by C N Davies <cn...@xcogia.com>.
Hi Lukasz,

Thank you for your reply.

I defined my action like so:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <include file="struts-default.xml"/>
  <constant name="struts.devMode" value="true" />

  <package name="com.xxxx.action" extends="struts-default" namespace="/">
      <action name="userAction" class="UserRegisterAction" >
          <result name="success">confirmRegister.jsp</result>
          <result name="input">/register.jsp</result>
          <result name="error">/error.jsp</result>
      </action>
  </package>
</struts>

However this will throw an exception:

SEVERE: Dispatcher initialization failed
Unable to load configuration. - action - file:/D:/projects/BaseApp/out/artifacts/BaseApp_war_exploded/WEB-INF/classes/struts.xml:22:60
	at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:70)
	at org.apache.struts2.dispatcher.Dispatcher.getContainer(Dispatcher.java:967)
	at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:435)
	at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:479)
	at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:74)
	at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:57)
	at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)
	at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
	at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105)
	at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4841)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5535)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
	at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1809)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

IntelliJ Idea reports that "class="UserRegisterAction" cannot be resolved and the only way to fix it is use the full class name.

I'm stumped!

Chris

-----Original Message-----
From: Lukasz Lenart [mailto:lukaszlenart@apache.org] 
Sent: Sunday, September 6, 2015 12:37 AM
To: Struts Users Mailing List
Subject: Re: Spring Injection Strange behaviour

How do you declare the action in struts.xml? Because instead full class name you must use the id from applicationContext.xml

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="secure" namespace="/secure" extends="default">
        <action name="user" class="userAction">
            <result>success.jsp</result>
        </action>
    </package>
</struts>

https://struts.apache.org/docs/spring-plugin.html#SpringPlugin-InitializingActionsfromSpring


Regards
--
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

2015-09-05 14:47 GMT+02:00 Chris <cr...@gmx.us>:
> I'm trying to use the Spring plugin to inject a dependency into my action at runtime. This is the first time I've tried this with Struts2 so I may have missed something but I'm stumped because I can see the dependency is injected but then the object is later null when I need to use it. Here is my action class code:
>
> public class UserRegisterAction extends ActionSupport {
>     private User user = new User();
>
>     @Override
>     public String execute(){
>         getUserService().saveUser(user);
>         return "success";
>     }
>
>     public UserServiceImpl getUserService(){
>         return userService;
>     }
>
>     UserServiceImpl userService;
>     public void setUserService(UserServiceImpl us){
>         this.userService = us;
>     }
> }
>
> Using the debugger I can see that upon startup "setUserService is called and this.userService is set, so the injection appears to be working ok, but when the execute method is called when the page is submitted userService is null. I'm quite new to Struts but I don't understand how my userService is being null.
>
> Can anyone see what I am missing?
>
>
> My applicationContext:
>
>
> <?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:tx="http://www.springframework.org/schema/tx"
>        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
>        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" default-autowire="byName">
>
>     <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
>
>     <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
>
>     <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
>         <property name="dataSource" ref="dataSource" />
>         <property name="jpaVendorAdapter">
>             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
>                 <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
>                 <property name="database" value="MYSQL" />
>                 <property name="showSql" value="true" />
>             </bean>
>         </property>
>     </bean>
>
>     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
>         <property name="driverClassName" value="org.gjt.mm.mysql.Driver" />
>         <property name="url" value="jdbc:mysql://127.0.0.1:3306/baseapp" />
>         <property name="username" value="xxxxxx" />
>         <property name="password" value="xxxxxx" />
>     </bean>
>
>     <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
>         <property name="entityManagerFactory" ref="entityManagerFactory" />
>     </bean>
>
>     <tx:annotation-driven transaction-manager="transactionManager" />
>
>     <bean id="userService" class="com.xxxxx.service.UserServiceImpl" scope="prototype"/>
>
>     <bean id="userAction" class="com.xxxxxx.action.UserRegisterAction">
>         <property name="userService" ref="userService"/>
>     </bean>
>
> </beans>
>
>
> Web.xml:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
>          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
>          version="3.1">
>
>    <filter>
>         <filter-name>struts2</filter-name>
>         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
>     </filter>
>
>     <filter-mapping>
>         <filter-name>struts2</filter-name>
>         <url-pattern>/*</url-pattern>
>     </filter-mapping>
>
>     <listener>
>         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
>     </listener>
> </web-app>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Spring Injection Strange behaviour

Posted by Lukasz Lenart <lu...@apache.org>.
How do you declare the action in struts.xml? Because instead full
class name you must use the id from applicationContext.xml

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="secure" namespace="/secure" extends="default">
        <action name="user" class="userAction">
            <result>success.jsp</result>
        </action>
    </package>
</struts>

https://struts.apache.org/docs/spring-plugin.html#SpringPlugin-InitializingActionsfromSpring


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

2015-09-05 14:47 GMT+02:00 Chris <cr...@gmx.us>:
> I'm trying to use the Spring plugin to inject a dependency into my action at runtime. This is the first time I've tried this with Struts2 so I may have missed something but I'm stumped because I can see the dependency is injected but then the object is later null when I need to use it. Here is my action class code:
>
> public class UserRegisterAction extends ActionSupport {
>     private User user = new User();
>
>     @Override
>     public String execute(){
>         getUserService().saveUser(user);
>         return "success";
>     }
>
>     public UserServiceImpl getUserService(){
>         return userService;
>     }
>
>     UserServiceImpl userService;
>     public void setUserService(UserServiceImpl us){
>         this.userService = us;
>     }
> }
>
> Using the debugger I can see that upon startup "setUserService is called and this.userService is set, so the injection appears to be working ok, but when the execute method is called when the page is submitted userService is null. I'm quite new to Struts but I don't understand how my userService is being null.
>
> Can anyone see what I am missing?
>
>
> My applicationContext:
>
>
> <?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:tx="http://www.springframework.org/schema/tx"
>        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
>        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" default-autowire="byName">
>
>     <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
>
>     <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
>
>     <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
>         <property name="dataSource" ref="dataSource" />
>         <property name="jpaVendorAdapter">
>             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
>                 <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
>                 <property name="database" value="MYSQL" />
>                 <property name="showSql" value="true" />
>             </bean>
>         </property>
>     </bean>
>
>     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
>         <property name="driverClassName" value="org.gjt.mm.mysql.Driver" />
>         <property name="url" value="jdbc:mysql://127.0.0.1:3306/baseapp" />
>         <property name="username" value="xxxxxx" />
>         <property name="password" value="xxxxxx" />
>     </bean>
>
>     <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
>         <property name="entityManagerFactory" ref="entityManagerFactory" />
>     </bean>
>
>     <tx:annotation-driven transaction-manager="transactionManager" />
>
>     <bean id="userService" class="com.xxxxx.service.UserServiceImpl" scope="prototype"/>
>
>     <bean id="userAction" class="com.xxxxxx.action.UserRegisterAction">
>         <property name="userService" ref="userService"/>
>     </bean>
>
> </beans>
>
>
> Web.xml:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
>          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
>          version="3.1">
>
>    <filter>
>         <filter-name>struts2</filter-name>
>         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
>     </filter>
>
>     <filter-mapping>
>         <filter-name>struts2</filter-name>
>         <url-pattern>/*</url-pattern>
>     </filter-mapping>
>
>     <listener>
>         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
>     </listener>
> </web-app>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org