Spring 在Controller中注入一个service时输出是null
web.xml的配置如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<context:component-scan base-package="com.SpringMVC.service.dao,com.SpringMVC.controller1"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="loginCheckManager" class="com.SpringMVC.service.dao.impl.LoginCheckManagerImpl"></bean>
<bean id="loginCheckController" class="com.SpringMVC.controller1.LoginCheckController">
<property name="loginCheckManager" ref="loginCheckManager"></property>
</bean>
</beans>
mvc-context.xml配置如下:
<context:component-scan base-package="com.SpringMVC.controller1">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--<property name="viewClass" value="org.springframework.web.servlet.view.jstlView"/>-->
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
controller代码如下:
@Controller
public class LoginCheckController {
private LoginCheckManager loginCheckManager;
public LoginCheckManager getLoginCheckManager() {
return loginCheckManager;
}
public void setLoginCheckManager(LoginCheckManager loginCheckManager) {
this.loginCheckManager = loginCheckManager;
}
@RequestMapping(value="/loginCheck")
public String loginCheck(HttpServletRequest request,HttpServletResponse response){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println(ctx.getBean("loginCheckManager"));
System.out.println(loginCheckManager);
String username=request.getParameter("username");
//loginCheckManager.loginCheck(username);
return "success";
}
}
问题是第一个输出正常取到了对象
但是第二个输出为null不知道为什么,调了很长时间不知道问题所在?