Java 使用SpringMVC,DaoImpl不是';t注入
我正在使用SpringMVC4.3.2-RELEASE构建一个简单的结构。我将配置写入了几个spring-*.xml,当我调试应用程序时,它显示控制器中的serviceimpl有值,而serviceimpl中的daoimpl没有值 角色控制器Java 使用SpringMVC,DaoImpl不是';t注入,java,spring,spring-mvc,Java,Spring,Spring Mvc,我正在使用SpringMVC4.3.2-RELEASE构建一个简单的结构。我将配置写入了几个spring-*.xml,当我调试应用程序时,它显示控制器中的serviceimpl有值,而serviceimpl中的daoimpl没有值 角色控制器 我仍然不知道是什么导致了我的问题,我试着用JavaConfig重建我的SpringDemo,但效果很好——我没有像使用xml那样使用@ComponentScan或@Service@Repository。有人能解释一下吗?请将keyy.service添加
我仍然不知道是什么导致了我的问题,我试着用JavaConfig重建我的SpringDemo,但效果很好——我没有像使用xml那样使用@ComponentScan或@Service@Repository。有人能解释一下吗?请将
keyy.service
添加到您的
中,或者将基本包更改为justkeyy
,因为它会重复出现。这样做将允许Spring选择带注释的类。服务
@Service
public class RoleInfoServiceImpl implements IRoleInfoService {
Dao
@Repository
public class RoleInfoDaoImpl implements IRoleInfoDao {
注意:如果依赖注释,最好只使用注释。现在您的服务正在使用xml配置。我认为您应该在spring service.xml中添加
请参阅此链接以了解您是否缺少spring-dao.xml中
roleinfodaompl
的bean定义?!!配置文件有问题,你能把它们全部发布并完成吗?spring-*.xml文件放在哪里?您似乎还没有在spring-service.xml中定义roleinfodaoimplbean,您正在DispatcherServlet
的上下文中使用组件扫描,这意味着基于注释的配置。但是,对于ContextLoaderLIstener
加载的上下文,您不会这样做,因此您的注释将被忽略。也可以在其中启用组件扫描,或者添加
以仅对该上下文启用注释处理。我刚刚将springmvc.xml的名称重构为spring-mvc.xml,我认为这使contextLoaderListener和DispatcherServlet都包含了它们。但在这种情况下,应用程序可以正常工作——RoleInfoDaoImpl现在有了价值。有人知道原因吗?但问题是,RoleInfo ServiceImpl成功注入控制器,而DaoImpl则没有。我认为扫描过程是使用contextLoaderListener执行的,而不是因为我的服务和dao的声明在配置文件中,而不是直接在使用注释的源代码中。我不确定我是否理解。Spring如何知道如何读取服务层中的Autowire注释?如果包含这个,会发生什么呢?我想会通知上下文来检测被标记为“@Controller@servicevce”的实例。。。由容器生成和管理。在我看来,contextLoaderListener的作用是将单独配置文件中bean的定义合并到位于DispatcherServlet的contextConfigLocation的原始applicationContext.xml中。
public class RoleInfoDaoImpl implements IRoleInfoDao {
@Override
public List<RoleInfo> getAll() throws SQLException {
return null;
}
}
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>>
</listener>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="keyy.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix" value=".jsp" />
<property name="prefix" value="/WEB-INF/jsp/"/>
</bean>
<bean class="keyy.service.serviceimpl.RoleInfoServiceImpl"></bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="driverClass" value="${jdbc.driver}"/>
</bean>
<bean class="keyy.dao.datasourcefactory.datasourcefactoryimpl.DataSourceFactory">
</bean>
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="roleInfoDao" class="keyy.dao.daoimpl.RoleInfoDaoImpl"></bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="driverClass" value="${jdbc.driver}"/>
</bean>
<bean class="keyy.dao.datasourcefactory.datasourcefactoryimpl.DataSourceFactory">
</bean>
</beans>
-|springmvc
-|resources
-|spring
* spring-dao.xml
* spring-service.xml
* springmvc.xml
* jdbc.properties
-|src
-|keyy
-|controller
* RoleController
* HomeController
-|dao
* IRoleInfoDao
-|daoimpl
* RoleInfoDaoImpl
-|entity
* RoleInfo
-|service
* IRoleInfoService
-|serviceimpl
* RoleInfoServceImpl
@Service
public class RoleInfoServiceImpl implements IRoleInfoService {
@Repository
public class RoleInfoDaoImpl implements IRoleInfoDao {