-1

I'm going to start with my project structure.

enter image description here

SpecialClaimsCaseManager.java - Where the error occurs:

package com.redacted.sch.web.mvc.model;

//Some imports that don't matter

@Component
public class SpecialClaimsCaseManager {

    @Autowired
    private SpecialClaimsCaseRepositoryService<SpecialClaimsCaseDto> specialClaimsCaseRepositoryService;

    public Collection<SpecialClaimsCase> findAll() {
        return convertToSpecialClaimsCase(specialClaimsCaseRepositoryService.findAll()); //Error happens here. specialClaimsCaseRepositoryService is null
    }

//Some irrelevant code finishing up the class

SpecialClaimsCaseRespositoryService - The interface of the class which needs be injected:

package com.redacted.sch.service;

public interface SpecialClaimsCaseRepositoryService<C extends SpecialClaimsCaseDto> {
    //Some irrelevant interface code
}

SpecialClaimsCaseRepositoryServiceImpl - The class which implements the interface. This is what should be injected.

package com.redacted.sch.service.impl;

@Service 
public class SpecialClaimsCaseRepositoryServiceImpl implements SpecialClaimsCaseRepositoryService<SpecialClaimsCaseDto> {

    @Autowired
    private SpecialClaimsCaseRepository repository; //This autowire actually works. Not relevant to the problem, though.

    //Some code...
}

ApplicationController - Controller obviously

package com.redacted.web.mvc.controllers;

//Some imports

@Controller
public class ApplicationController {

        @RequestMapping(value="/test", method=RequestMethod.GET)
        public @ResponseBody Collection<SpecialClaimsCase> getCaseInJSON(@RequestParam Map<String, String> requestParams, ModelMap model) {
            SpecialClaimsCaseManager caseManager = new SpecialClaimsCaseManager();

            return new ArrayList<SpecialClaimsCase>(caseManager.findAll());
        }
}

Now to get to the configuration, we'll start with web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>SpecialClaimsHandling</display-name>

    <!-- Spring Configuration Files -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            WEB-INF/applicationContext.xml
            WEB-INF/application-security.xml
            classpath*:sch_model_spring.xml
        </param-value>
    </context-param>

    <!-- Spring Security Filters -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>
             org.springframework.web.filter.DelegatingFilterProxy
        </filter-class>
    </filter>

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

    <!-- Spring Listeners -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- MVC Filter -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <!-- Session Configuration -->
    <session-config>
        <session-timeout>5</session-timeout>
    </session-config>

</web-app>

And dispatcher-servlet.xml:

<?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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.redacted.sch.web.mvc.controllers"/>

    <mvc:resources mapping="/resources/**" location="/resources/" />

    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" value="/WEB-INF/views/" />
       <property name="suffix" value=".jsp" />
    </bean>

</beans>

applicationContext.xml:

<?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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.redacted.sch.web.mvc.controllers"/>

    <mvc:resources mapping="/resources/**" location="/resources/" />

    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" value="/WEB-INF/views/" />
       <property name="suffix" value=".jsp" />
    </bean>

</beans>

And finally sch_model_spring.xml:

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        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">

        <context:component-scan base-package="com.redacted.repository.jpa,
              com.redacted.sch.domain.model,
              com.redacted.sch.repository.jpa,
              com.redacted.sch.service,
              com.redacted.sch.service.impl"/>

        <tx:annotation-driven />

        <tx:jta-transaction-manager />

        <!-- Data source used for testing -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver" />
            <property name="url" value="redacted.doesntmatter.com" />
            <property name="username" value="redacted" />
            <property name="password" value="redacted" />
        </bean>

        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
              <property name="persistenceUnitName" value="schManager" />
              <property name="dataSource" ref="dataSource" />
        </bean>

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

OK, so after the wall of configuration files, classes, and structure, let's get to the problem. In SpecialClaimsCaseManager I have the @Autowired SpecialClaimsCaseRepositoryService<SpecialClaimsCaseDto> object, which is not actually getting wired. When it comes to be used, an NPE is thrown is the following stack trace:

(Short here, full thing supplied via fpaste)

[7/10/14 15:20:13:343 CDT] 000000fe webapp        E com.ibm.ws.webcontainer.webapp.WebApp logServletError SRVE0293E: [Servlet Error]-[dispatcher]: java.lang.NullPointerException
    at com.redacted.sch.web.mvc.model.SpecialClaimsCaseManager.findAll(SpecialClaimsCaseManager.java:26)
    at com.redacted.sch.web.mvc.controllers.ApplicationController.getCaseInJSON(ApplicationController.java:43)
    ........

From what I've gathered from previous help, this is a context problem. The context that SpecialClaimsCaseManager is in does not have access to SpecialClaimsCaseRepositoryService. I do not know why this is so, or how to resolve it. I have already gone through and refactored the project to what I thought would work, and still no luck. I am really stuck here and have no idea how to resolve the problem.

Much thanks for any assistance. If anything else is needed, please ask.

jaredready
  • 2,398
  • 4
  • 23
  • 50

2 Answers2

4

The moment you used new operator its not Spring managed.

Change your controller to:

    @Controller
    public class ApplicationController {
            @Autowired
            SpecialClaimsCaseManager caseManager;

            @RequestMapping(value="/test", method=RequestMethod.GET)
            public @ResponseBody Collection<SpecialClaimsCase> getCaseInJSON(@RequestParam Map<String, String> requestParams, ModelMap model) {
                ....
                return new ArrayList<SpecialClaimsCase>(caseManager.findAll());
            }
    }
Prasad
  • 3,785
  • 2
  • 14
  • 23
0

could it be that your classpath def is wrong in web.xml?

classpath*:sch_model_spring.xml

I normall would use something like:

classpath:/spring/beans-datasource.xml
dr jerry
  • 9,768
  • 24
  • 79
  • 122