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 <crisp...@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-unsubscr...@struts.apache.org > For additional commands, e-mail: user-h...@struts.apache.org > > -- e: davelnew...@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>