applicationContext.xml
(ApplicationContext)应该导入user-servlet.xml
(WebApplicationContext)?我使用web.xml
加载ContextLoaderListener
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>
上面运行在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
可能会有一些冗余
我在applicationContext.xml
有componentScan
的配置,重命名SpringMVC-servlet.xml
后删除init-param
移动文件在WEB-INF中,控制器可以'找到服务bean,注入假
我想应该在web.xml
中导入applicationContext.xml
?
UserService
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
bean。
UserService
成功
答案 0 :(得分:1)
请将spring bean配置文件user-servlet.xml名称更改为SpringMVC-servlet.xml并删除init-param条目。并将其移动到WEB-INF或映射资源文件夹中作为部署程序集中的根文件夹(用于eclipse ide)。它应该工作正常。
用于UserService问题 - 检查UserService类的注释。它应该使用@Service或任何其他类型注释注释,并且类必须驻留在componentScan声明的包中。此外,用户服务必须具有无arg默认构造函数,否则您需要实例化UserService类的依赖构造函数arg。