Uploaded image for project: 'Struts 2'
  1. Struts 2
  2. WW-4758

@autowired does not work since Struts 2.3.28.1

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.3.28
    • Fix Version/s: 2.5.12
    • Component/s: Plugin - Spring
    • Labels:
      None

      Description

      I am trying to run on Tomcat a Spring 4 + Struts 2 + Hibernate web app, but it fails to start with NullPointerException for autowired field. This issue is happening after release of 2.3.28.1 So please help

      2017-03-15 15:28:35,308 [http-bio-8009-exec-1] ERROR au.com.presentation.struts.action.tiles.landing.LandingSummaryPreparer - Error Id:
      java.lang.NullPointerException
      at au.com.presentation.struts.action.tiles.landing.LandingSummaryPreparer.performPreparer(LandingSummaryPreparer.java:142

      and in my code I have autowired that always returning null
      @Autowired
      private Services services;

        Activity

        Hide
        lukaszlenart Lukasz Lenart added a comment -

        Do you use the Spring plugin?

        Show
        lukaszlenart Lukasz Lenart added a comment - Do you use the Spring plugin?
        Hide
        khurram.rashid Khurram added a comment - - edited

        Hi Luckasz,

        Yes we are using struts2-spring-plugin version 4.1.2.RELEASE and detail of other versions as follows
        <spring.version>4.1.2.RELEASE</spring.version>
        <struts.version>2.3.28.1</struts.version> or <struts.version>2.3.32</struts.version>
        <hibernate.version>4.3.7.Final</hibernate.version>
        <aspectj.version>1.8.5</aspectj.version>

        Show
        khurram.rashid Khurram added a comment - - edited Hi Luckasz, Yes we are using struts2-spring-plugin version 4.1.2.RELEASE and detail of other versions as follows <spring.version>4.1.2.RELEASE</spring.version> <struts.version>2.3.28.1</struts.version> or <struts.version>2.3.32</struts.version> <hibernate.version>4.3.7.Final</hibernate.version> <aspectj.version>1.8.5</aspectj.version>
        Hide
        lukaszlenart Lukasz Lenart added a comment -

        hm... but you put in the description Spring 3 but here you have Spring 4, are you sure you are using a proper jars? Did you cleaned up Tomcat's work and temp folders?

        Show
        lukaszlenart Lukasz Lenart added a comment - hm... but you put in the description Spring 3 but here you have Spring 4, are you sure you are using a proper jars? Did you cleaned up Tomcat's work and temp folders?
        Hide
        khurram.rashid Khurram added a comment - - edited
        <dependency>
        				<groupId>org.springframework</groupId>
        				<artifactId>spring-aop</artifactId>
        				<version>${spring.version}</version>
        			</dependency>
        			<dependency>
        				<groupId>org.springframework</groupId>
        				<artifactId>spring-context</artifactId>
        				<version>${spring.version}</version>
        			</dependency>
        			<dependency>
        				<groupId>org.springframework</groupId>
        				<artifactId>spring-context-support</artifactId>
        				<version>${spring.version}</version>
        			</dependency>
        			<dependency>
        				<groupId>org.springframework</groupId>
        				<artifactId>spring-orm</artifactId>
        				<version>${spring.version}</version>
        			</dependency>
        			<dependency>
        				<groupId>org.springframework</groupId>
        				<artifactId>spring-tx</artifactId>
        				<version>${spring.version}</version>
        			</dependency>
        			<dependency>
        				<groupId>org.springframework</groupId>
        				<artifactId>spring-web</artifactId>
        				<version>${spring.version}</version>
        			</dependency>
        			<dependency>
        				<groupId>org.springframework</groupId>
        				<artifactId>spring-webmvc</artifactId>
        				<version>${spring.version}</version>
        			</dependency>
        
        Show
        khurram.rashid Khurram added a comment - - edited <dependency> <groupId> org.springframework </groupId> <artifactId> spring-aop </artifactId> <version> ${spring.version} </version> </dependency> <dependency> <groupId> org.springframework </groupId> <artifactId> spring-context </artifactId> <version> ${spring.version} </version> </dependency> <dependency> <groupId> org.springframework </groupId> <artifactId> spring-context-support </artifactId> <version> ${spring.version} </version> </dependency> <dependency> <groupId> org.springframework </groupId> <artifactId> spring-orm </artifactId> <version> ${spring.version} </version> </dependency> <dependency> <groupId> org.springframework </groupId> <artifactId> spring-tx </artifactId> <version> ${spring.version} </version> </dependency> <dependency> <groupId> org.springframework </groupId> <artifactId> spring-web </artifactId> <version> ${spring.version} </version> </dependency> <dependency> <groupId> org.springframework </groupId> <artifactId> spring-webmvc </artifactId> <version> ${spring.version} </version> </dependency>
        Hide
        khurram.rashid Khurram added a comment -

        Sorry that was typo

        Show
        khurram.rashid Khurram added a comment - Sorry that was typo
        Hide
        khurram.rashid Khurram added a comment -

        Yes did tomcat cleanup. Verified war file under lib folder and all my jar files with same version exist in that folder. No cleanup issue

        Show
        khurram.rashid Khurram added a comment - Yes did tomcat cleanup. Verified war file under lib folder and all my jar files with same version exist in that folder. No cleanup issue
        Hide
        khurram.rashid Khurram added a comment -

        Also we have been using same @Autowired code since 2.3.15.1 struts version but now we wanted to upgrade to newer version and facing this issue from 2.3.28.1

        Show
        khurram.rashid Khurram added a comment - Also we have been using same @Autowired code since 2.3.15.1 struts version but now we wanted to upgrade to newer version and facing this issue from 2.3.28.1
        Hide
        lukaszlenart Lukasz Lenart added a comment -

        Maybe it is related to this WW-4110 and you must set struts.class.spring.enableAopSupport to true

        Show
        lukaszlenart Lukasz Lenart added a comment - Maybe it is related to this WW-4110 and you must set struts.class.spring.enableAopSupport to true
        Hide
        aleksandr-m Aleksandr Mashchenko added a comment - - edited

        How do create a bean of your LandingSummaryPreparer?

        Show
        aleksandr-m Aleksandr Mashchenko added a comment - - edited How do create a bean of your LandingSummaryPreparer ?
        Hide
        khurram.rashid Khurram added a comment -
        1)
        public abstract class BasePreparer implements ViewPreparer {
        	protected final Logger LOG = Logger.getLogger(this.getClass());
        	public void execute(TilesRequestContext tilesContext, AttributeContext attributeContext) {
        		boolean display = performPreparer(tilesContext, attributeContext);
        
        2)
        public class LandingSummaryPreparer extends BasePreparer {
        	@Autowired
        	private Services services;
        	@Override
        	protected boolean performPreparer(TilesRequestContext tilesContext, AttributeContext attributeContext) {
        		HttpServletRequest request = getRequest(tilesContext);
        
        3)Service.class
        import org.springframework.beans.factory.annotation.Autowired;
        public class Services {
        	@Autowired
        	private UserService userService;
        	public UserService getUserService() {
        		return userService;
        	}
        		
        4)
        In tiles.xml
         <definition name="v3.landing.summary" extends="v3.basepage" template="/WEB-INF/struts/v3/tiles/landingpages/summarywithfollow.jsp"
            			preparer="au.com.presentation.struts.action.v3.tiles.landing.LandingSummaryPreparer" />
        
        5) applicationContext.xml
        <context:annotation-config />
        <context:component-scan base-package="au.com" />
        <bean id="userService" class="au.com.business.service.impl.UserServiceImpl" />
        <bean id="services" class="au.com.business.service.Services" />
        <bean id="cacheLocator" class="au.com.support.cache.CacheLocator">
        		<constructor-arg ref="lasooServices" />
        		<constructor-arg ref="XMemCachedExternalCache" />
        		<constructor-arg ref="assetUpdateMonitor" />
        		<constructor-arg ref="elasticSearchFactory" />
        </bean>
        <bean class="au.com.support.site.ServiceLocator">
        	<constructor-arg ref="services" />
        </bean>				
        
        6) In web.xml
        <listener>
        		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        	</listener>
        	
        7) In struts.xml
        <constant name="struts.objectFactory" value="spring" />		
        
        Show
        khurram.rashid Khurram added a comment - 1) public abstract class BasePreparer implements ViewPreparer { protected final Logger LOG = Logger.getLogger( this .getClass()); public void execute(TilesRequestContext tilesContext, AttributeContext attributeContext) { boolean display = performPreparer(tilesContext, attributeContext); 2) public class LandingSummaryPreparer extends BasePreparer { @Autowired private Services services; @Override protected boolean performPreparer(TilesRequestContext tilesContext, AttributeContext attributeContext) { HttpServletRequest request = getRequest(tilesContext); 3)Service.class import org.springframework.beans.factory.annotation.Autowired; public class Services { @Autowired private UserService userService; public UserService getUserService() { return userService; } 4) In tiles.xml <definition name= "v3.landing.summary" extends = "v3.basepage" template= "/WEB-INF/struts/v3/tiles/landingpages/summarywithfollow.jsp" preparer= "au.com.presentation.struts.action.v3.tiles.landing.LandingSummaryPreparer" /> 5) applicationContext.xml <context:annotation-config /> <context:component-scan base- package = "au.com" /> <bean id= "userService" class= "au.com.business.service.impl.UserServiceImpl" /> <bean id= "services" class= "au.com.business.service.Services" /> <bean id= "cacheLocator" class= "au.com.support.cache.CacheLocator" > <constructor-arg ref= "lasooServices" /> <constructor-arg ref= "XMemCachedExternalCache" /> <constructor-arg ref= "assetUpdateMonitor" /> <constructor-arg ref= "elasticSearchFactory" /> </bean> <bean class= "au.com.support.site.ServiceLocator" > <constructor-arg ref= "services" /> </bean> 6) In web.xml <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 7) In struts.xml <constant name= "struts.objectFactory" value= "spring" />
        Hide
        aleksandr-m Aleksandr Mashchenko added a comment -

        In order to inject something into LandingSummaryPreparer you need to create a bean out of it.
        Not a Struts problem.

        Show
        aleksandr-m Aleksandr Mashchenko added a comment - In order to inject something into LandingSummaryPreparer you need to create a bean out of it. Not a Struts problem.
        Hide
        khurram.rashid Khurram added a comment -

        Hi Aleksandr Mashchenko,

        I was following this thread http://stackoverflow.com/questions/26905430/how-to-inject-springbean-to-a-struts-intercepter-is-it-possible and here solution is exactly same like us. Only difference is we have preparer but in that thread is related to interceptor. Can you please explain more that how can we fix this and why that happen in newer struts version?

        Show
        khurram.rashid Khurram added a comment - Hi Aleksandr Mashchenko , I was following this thread http://stackoverflow.com/questions/26905430/how-to-inject-springbean-to-a-struts-intercepter-is-it-possible and here solution is exactly same like us. Only difference is we have preparer but in that thread is related to interceptor. Can you please explain more that how can we fix this and why that happen in newer struts version?
        Hide
        aleksandr-m Aleksandr Mashchenko added a comment -

        You can inject into Struts actions and interceptors because they are created by Struts object factory. The preparer for Tiles is not.

        Show
        aleksandr-m Aleksandr Mashchenko added a comment - You can inject into Struts actions and interceptors because they are created by Struts object factory. The preparer for Tiles is not.
        Hide
        lukaszlenart Lukasz Lenart added a comment -

        This can be easily fixed by extending StrutsTilesContainerFactory and overriding createPreparerFactory method. On it

        Show
        lukaszlenart Lukasz Lenart added a comment - This can be easily fixed by extending StrutsTilesContainerFactory and overriding createPreparerFactory method. On it
        Hide
        jira-bot ASF subversion and git services added a comment -

        Commit 93dba5bce22e9e7e539e3e631166d6510efca023 in struts's branch refs/heads/master from Lukasz Lenart
        [ https://git-wip-us.apache.org/repos/asf?p=struts.git;h=93dba5b ]

        WW-4758 Uses ObjectFactory to create Tiles' ViewPrepare

        Show
        jira-bot ASF subversion and git services added a comment - Commit 93dba5bce22e9e7e539e3e631166d6510efca023 in struts's branch refs/heads/master from Lukasz Lenart [ https://git-wip-us.apache.org/repos/asf?p=struts.git;h=93dba5b ] WW-4758 Uses ObjectFactory to create Tiles' ViewPrepare
        Hide
        lukaszlenart Lukasz Lenart added a comment -

        StrutsTilesContainerFactory has been extended. Please test and report back

        Show
        lukaszlenart Lukasz Lenart added a comment - StrutsTilesContainerFactory has been extended. Please test and report back
        Hide
        hudson Hudson added a comment -

        SUCCESS: Integrated in Jenkins build Struts-JDK7-master #637 (See https://builds.apache.org/job/Struts-JDK7-master/637/)
        WW-4758 Uses ObjectFactory to create Tiles' ViewPrepare (lukaszlenart: rev 93dba5bce22e9e7e539e3e631166d6510efca023)

        • (add) plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsPreparerFactory.java
        • (edit) plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java
        Show
        hudson Hudson added a comment - SUCCESS: Integrated in Jenkins build Struts-JDK7-master #637 (See https://builds.apache.org/job/Struts-JDK7-master/637/ ) WW-4758 Uses ObjectFactory to create Tiles' ViewPrepare (lukaszlenart: rev 93dba5bce22e9e7e539e3e631166d6510efca023) (add) plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsPreparerFactory.java (edit) plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java

          People

          • Assignee:
            lukaszlenart Lukasz Lenart
            Reporter:
            khurram.rashid Khurram
          • Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development