0

我正在使用 Spring 3.0.6 Release 创建一个网络应用程序

我的问题是在启动服务器时,bean 注入成功.....但是当使用给出空指针异常时。

下面是我的 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:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:jd="http://www.springframework.org/schema/jdbc"
    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
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
   http://www.springframework.org/schema/jdbc
   http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">

   <context:annotation-config />
   <context:component-scan base-package="com.icis.sample.web" />
   <mvc:annotation-driven />
   <bean id="dataSource"   class =   "org.springframework.jdbc.datasource.DriverManagerDataSource">
   <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />        
   <property name="url" value="jdbc:sqlserver://localhost:3306;DatabaseName=SAMPLESPRING"     />
    <property name="username" value="user1" />        
    <property name="password" value="user1" />
    </bean>

    <bean id="sessionFactory"   class=  "org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource" />
    </property>

    <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>

            </props>
        </property>

    </bean>
  <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="modelDAO" class="com.sample.dao.ModelDAO">
         <property name="hibernateTemplate" ref="hibernateTemplate" /> 
    </bean>

    <bean id="modelService" class="com.sample.service.ModelService">
         <property name="modelDAO" ref="modelDAO" /> 
    </bean>
        <bean id="modelController" class="com.sample.GetAllDataController.ModelService">
         <property name="modelService" ref="modelService" /> 
    </bean>

</beans>

控制器 GetAllDataController.java

package com.sample.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.icis.sample.model.FormModel;
import com.icis.sample.service.ModelService;

@Controller
public class GetAllDataController{
private ModelService modelService;
public void setModelService(){
   this.modelService =modelService ;
}
    @RequestMapping("/home")
    public ModelAndView home(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception {

        ModelAndView mav = new ModelAndView("home");
        FormModel model = new FormModel();

        modelService.getPort();


         return mav;
    }

}

请帮助我,为什么在调用 getPort() 方法时注入的 bean 对象为空?

4

1 回答 1

0

你需要autowire在豆子里

@Autowired
private ModelService modelService;
于 2013-12-30T07:18:03.447 回答