SpringMVC配置applicationContext.xml应该导入some-servlet.xml

识字粗堪供赋役,不须辛苦慕公卿。这篇文章主要讲述SpringMVC配置applicationContext.xml应该导入some-servlet.xml相关的知识,希望能为你提供帮助。

  • SpringMVC applicationContext.xml(ApplicationContext)应该导入user-servlet.xml(WebApplicationContext)?
我使用web.xml加载ContextLoaderListener配置applicationContext.xml
DispatcherServlet加载user-servlet.xml发现控制器bean无法引用服务bean
目录结构
SpringMVC配置applicationContext.xml应该导入some-servlet.xml

文章图片

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:aop="http://www.springframework.org/schema/aop" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> < context:component-scan base-package="com.f.dao, com.f.service, com.f.advice"/> < aop:aspectj-autoproxy/> < /beans>

user-servlet.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" 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> < bean id="userController" class="com.f.controller.UserController"/> < mvc:annotation-driven/> < mvc:resources mapping="/statics/**" location="/statics/"/> < bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> < property name="prefix" value="https://www.songbingjia.com/users/"/> < property name="suffix" value="https://www.songbingjia.com/android/.jsp"/> < /bean> < /beans>

web.xml
< ?xml version="1.0" encoding="UTF-8"?> < web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> < 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> SpringMVC< /servlet-name> < servlet-class> org.springframework.web.servlet.DispatcherServlet< /servlet-class> < init-param> < param-name> contextConfigLocation< /param-name> < param-value> classpath:user-servlet.xml< /param-value> < /init-param> < load-on-startup> 1< /load-on-startup> < /servlet> < servlet-mapping> < servlet-name> SpringMVC< /servlet-name> < url-pattern> /< /url-pattern> < /servlet-mapping> < /web-app>

UPDATE1以上运行在UserController上找到了NoPointException,因为没有注入服务bean,下面是控制器的一部分
@RequestMapping(value = "https://www.songbingjia.com/users") @Controller public class UserController { private Logger logger = LogManager.getLogger(UserController.class); @Autowired @Qualifier(value = "https://www.songbingjia.com/android/userService") private UserService userService; }

如果将< import resource="applicationContext.xml"/> 添加到user-servlet.xml服务器运行是正确的。
但在web.xml ContextLoaderListener已加载applicationContext.xml并再次加载user-servlet.xml可能有一些redundency
UPDATE2我在applicationContext.xml有配置componentScan,重命名SpringMVC-servlet.xml并删除init-param在WEB-INF中的web.xml移动文件,控制器找不到服务bean,注入false
【SpringMVC配置applicationContext.xml应该导入some-servlet.xml】
SpringMVC配置applicationContext.xml应该导入some-servlet.xml

文章图片

我想应该在applicationContext.xml中导入SpringMVC-serlve.xml
UserService
@Service(value = "https://www.songbingjia.com/android/userService") public class UserServiceImpl implements UserService { @Qualifier(value = "https://www.songbingjia.com/android/userDao") @Autowired private UserMapper mapper; }

解决
因为我在< bean id="userController" class="com.f.controller.UserController"/> 使用user-servlet.xml所以UserService bean不能注射。
改变后,这个< context:component-scan base-package="com.f.controller"/> 成功了
答案请将spring bean配置文件user-servlet.xml名称更改为SpringMVC-servlet.xml并删除init-param条目。并将其移动到WEB-INF或映射资源文件夹中作为部署程序集中的根文件夹(用于eclipse ide)。它应该工作正常。
对于UserService问题 - 检查UserService类的注释。它应该使用@Service或任何其他类型注释注释,并且类必须驻留在componentScan声明的包中。此外,用户服务必须具有无arg默认构造函数,否则您需要实例化UserService类的依赖构造函数arg。

    推荐阅读