springmvc-servlet.xml         <mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/images/**" location="/images/" />

<!-- 自动搜索@Controller标注的类 -->
<context:component-scan base-package="com.huaxin.action" />
<!-- 启用基于注解的处理器映射,添加拦截器,类级别的处理器映射 -->  
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- Default ViewResolver -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"></property>
</bean>
applicationContext.xml<bean id="systemMenuMapper" parent="txProxyTemplate">
<property name="target" ref="systemMenuMapperTarget" />
</bean>SystemMenuController.java@Controller
@RequestMapping(value="/menu")
public class SystemMenuController {
//@Autowired
private SystemMenuMapper systemMenuMapper;

public SystemMenuMapper getSystemMenuMapper() {
return systemMenuMapper;
}
         //@Resources
public void setSystemMenuMapper(SystemMenuMapper systemMenuMapper) {
this.systemMenuMapper = systemMenuMapper;
}
@RequestMapping(method=RequestMethod.GET)
public @ResponseBody String getMenuTree() {
System.out.println("null is " + systemMenuMapper);
return "123";
}
}
问题,我该怎么为SystemMenuController注入systemMenuMapper属性呢,无论用
//@Resources还是@Autowired
都不行,都提示空指针错,但是我直接用ClassPathXmlApplicationContext获取systemMenuMapper
是可以的。
在线等!!!

解决方案 »

  1.   

    再加个<context:annotation-config/>
      

  2.   

    applicationContext.xml里也加  <context:component-scan base-package="com.huaxin.action" />
      

  3.   

    <context:annotation-config></context:annotation-config>
    加一两个配置是不够的
    我记得spring的mvc需要一个****-servlet.xml配置
      

  4.   


        <!-- 自动搜索@Controller标注的类 -->
        <context:component-scan base-package="com.huaxin.action" />
     base-package="com.huaxin.action"给包含mvc的package
    比如 base-package="com.huaxin"