【问题标题】:SpringMVC config applicationContext.xml should import some-servlet.xmlSpringMVC 配置 applicationContext.xml 应该导入 some-servlet.xml
【发布时间】:2017-12-19 13:40:40
【问题描述】:
  • SpringMVC applicationContext.xml(ApplicationContext) 应该导入 user-servlet.xml(WebApplicationContext) 吗?

我正在使用ContextLoaderListener 配置web.xml 加载applicationContext.xml

DispatcherServlet load user-servlet.xml 发现控制器 bean 不能引用服务 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: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="/users/"/>
        <property name="suffix" value=".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>

更新1

上面运行在 UserController 上找到 NoPointException 因为没有注入服务 bean,下面是控制器的一部分

@RequestMapping(value = "/users")
@Controller
public class UserController {
    private Logger logger = LogManager.getLogger(UserController.class);

    @Autowired
    @Qualifier(value = "userService")
    private UserService userService;
}

如果添加&lt;import resource="applicationContext.xml"/&gt;user-servlet.xml 服务器运行正确。

但是在web.xml ContextLoaderListener 已经加载了applicationContext.xml 并再次在user-servlet.xml 加载可能有一些冗余

更新2

我在applicationContext.xml 有配置componentScan,在重命名SpringMVC-servlet.xml 并删除init-param 中的web.xml 在WEB-INF 中移动文件后,控制器找不到服务bean,注入错误

我想应该在SpringMVC-serlve.xml 中导入applicationContext.xml

用户服务

@Service(value = "userService")
public class UserServiceImpl implements UserService {
    @Qualifier(value = "userDao")
    @Autowired
    private UserMapper mapper;
}

解决

因为我在user-servlet.xml 中使用了&lt;bean id="userController" class="com.f.controller.UserController"/&gt;,所以无法注入UserService bean。

修改后 &lt;context:component-scan base-package="com.f.controller"/&gt;成功

【问题讨论】:

  • 你的问题不是很清楚。您到底在寻找什么?
  • 好的,我想要导入 applicationContext.xmlservlet.xml :)
  • 不,不需要。正如我所看到的,您已经在 SpringMVC servlet 定义中指定了您的 user-servlet.xml 位置。所以,这应该可以正常工作。
  • 我确实发现了错误,java.lang.NullPointerException com.f.controller.UserController.index(UserController.java:34),service bean 没有被注入
  • 如果在user-servlet.xml中添加``应该是正确的

标签: java spring spring-mvc servlets


【解决方案1】:

请将您的 spring bean 配置文件 user-servlet.xml 名称更改为 SpringMVC-servlet.xml 并删除 init-param 条目。并将其移动到 WEB-INF 或将资源文件夹映射为部署程序集中的根文件夹(用于 Eclipse IDE)。它应该可以正常工作。

对于 UserService 问题 - 检查 UserService 类上的注释。它应该使用 @Service 或任何其他原型注解进行注解,并且类必须驻留在 componentScan 声明的包中。此外,用户服务必须有一个无参数默认构造函数,否则您需要实例化 UserService 类的依赖构造函数 arg。

【讨论】:

  • 我确实将user-servlet.xml重命名为SpringMVC-servlet.xml并删除了init参数条目,在WEB-INF中移动文件,服务器运行是正确的
  • applicationContex.xml有扫描服务包,bean配置正确。
  • SpringMVC-servlet.xml 需要导入applicationContext.xml?如果我这样做,控制器会得到userService bean
  • 好的,弄清楚:)
猜你喜欢
  • 2014-04-27
  • 2011-03-30
  • 2014-04-11
  • 2011-04-08
  • 1970-01-01
  • 2011-05-04
  • 1970-01-01
  • 2016-11-04
相关资源
最近更新 更多