- SpringMVC
applicationContext.xml
(ApplicationContext) 应该导入user-servlet.xml
(Web应用程序上下文)?
我是配置web.xml
使用ContextLoaderListener
负载applicationContext.xml
DispatcherServlet
负载user-servlet.xml
发现 Controller 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
以上运行发现NoPointException
在 UserController 上,因为没有注入(inject)服务 bean,下面是 Controller 的一部分
@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 中移动文件, Controller 找不到服务 bean,注入(inject) false
我想应该导入applicationContext.xml
在SpringMVC-serlve.xml
?
用户服务
@Service(value = "userService")
public class UserServiceImpl implements UserService {
@Qualifier(value = "userDao")
@Autowired
private UserMapper mapper;
}
解决
因为我正在使用<bean id="userController" class="com.f.controller.UserController"/>
在user-servlet.xml
所以UserService
无法注入(inject)bean。
更改后
<context:component-scan base-package="com.f.controller"/>
成功
最佳答案
请将您的 spring bean 配置文件 user-servlet.xml 名称更改为 SpringMVC-servlet.xml 并删除 init-param 条目。并将其移动到 WEB-INF 或 map 资源文件夹中作为部署程序集的根文件夹(对于 eclipse ide)。应该可以正常工作。
对于 UserService 问题 - 检查 UserService 类上的注释。它应该用 @Service 或任何其他原型(prototype)注释进行注释,并且类必须驻留在 componentScan 声明的包中。另外,User 服务必须有一个无参数的默认构造函数,否则您需要实例化 UserService 类的依赖构造函数 arg。
关于java - SpringMVC 配置 applicationContext.xml 应该导入 some-servlet.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47888307/