我正在使用Spring 3.0.6 Release
创建一个Web应用程序我的问题是在启动服务器时,bean正在成功注入.....但在使用时给出Null指针异常。
以下是我的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>
Controller 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对象为null?