Tobias Kuess
Tobias Kuess

Reputation: 749

Spring MVC: Controller does not run in an ApplicationContext

I am trying to implement a WebApp using Spring MVC Framework. So far, HelloWorld wasn't a problem. Now I wanted to read in some Data from a database. To do so, I implemente a class called DataProvider which handles the database access.

Now I added this DataProvider class to my HelloWorld class, which is my Controller here. As soon as I do that, i get the following exception:

java.lang.IllegalStateException: ApplicationObjectSupport instance [de.bpm.keza.ui.srv.kennzahlen.controller.HelloController@7361b599] does not run in an ApplicationContext

Here is my Dispatcher-Servlet:

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="

       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="de.bpm.keza.ui.srv.kennzahlen" />

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

    <!-- Externe Konfigurationsdateien -->
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName"  value="jdbc/BPM_KORE_ALIAS"/>
    </bean>

    <!-- DataProvider -->
    <bean id="dataProvider" class="de.bpm.keza.ui.srv.kennzahlen.data.DataProvider">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>

    <!-- KEZA Dashboard -->
    <property name="koreVorgaengeGesamtDataSql">
            <value>
                select 
                    *
                from DE_BPM_KORE_DBRD
            </value>
    </property>
    </bean>

</beans>

here is my HelloController.java

@Controller
public class HelloController extends WebContentGenerator {


    DataProvider daPro = ((DataProvider) getWebApplicationContext().getBean("dataProvider", DataProvider.class));

   @RequestMapping("/hello")
    public ModelAndView helloWorld() {
        String message = "Hello World, Spring 3.0!";
        return new ModelAndView("hello", "message", message);
    }

   @RequestMapping("/bye")
    public ModelAndView byeWorld() {
        String message = "Goodbye World, Spring 3.0!";

//          daPro.getVorgaengeGesamtByArkNr();

        return new ModelAndView("hello", "message", message);
    }

}

What am i doing wrong here?

Upvotes: 0

Views: 2228

Answers (1)

Ranjith Kumar
Ranjith Kumar

Reputation: 75

You have two ways for accessing that bean.

DataProvider daPro = ((DataProvider) getWebApplicationContext().getBean("dataProvider", DataProvider.class));

Replace the Above Code with

DataProvider dataProvider;

Or

@Autowired
DataProvider dataProvider;

Upvotes: 1

Related Questions