0

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?

6
  • 3
    You're missing the whole point of Spring: it's a dependency injection framework. You should just have @Autowired private DataProvider dataProvider; in your controller. Spring will inject the DataProvider bean into the controller. I suggest you find a recent tutorial about Spring, which doesn't use the old XML way of defining beans, but uses annotations and Java configuration instead. Commented Jul 14, 2015 at 12:41
  • I think you are missing to define Controller in dispatcher-servlet.xml Commented Jul 14, 2015 at 12:42
  • You may consider checking out projects.spring.io/spring-data-jpa for some pretty nifty JPA interfaces that can make interacting with data sources in Spring MVC. spring.io/guides/gs/accessing-data-jpa has Maven and Gradle configuration instructions for Spring Data JPA that you may be able to utilize. Commented Jul 14, 2015 at 12:49
  • Most of the tutorials do the XML way. At least a dispatcherServlet which configures the viewresolver etc. I cant use Mavon or Gradle, which all other tutorials do use. Commented Jul 14, 2015 at 12:49
  • 1
    Remove extends WebContextGenerator and the ugly way to get the DataProvider just add @Autowired to the daPro field. Commented Jul 14, 2015 at 12:52

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.