【发布时间】: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;
}
如果添加<import resource="applicationContext.xml"/> 到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 中使用了<bean id="userController" class="com.f.controller.UserController"/>,所以无法注入UserService bean。
修改后
<context:component-scan base-package="com.f.controller"/>成功
【问题讨论】:
-
你的问题不是很清楚。您到底在寻找什么?
-
好的,我想要导入
applicationContext.xml和servlet.xml:) -
不,不需要。正如我所看到的,您已经在 SpringMVC servlet 定义中指定了您的 user-servlet.xml 位置。所以,这应该可以正常工作。
-
我确实发现了错误,java.lang.NullPointerException com.f.controller.UserController.index(UserController.java:34),
servicebean 没有被注入 -
如果在
user-servlet.xml中添加``应该是正确的
标签: java spring spring-mvc servlets