java之使用 SpringMVC,未注入(inject) DaoImpl

rubylouvre 阅读:50 2023-09-06 19:40:43 评论:0

我正在使用 springmvc 4.3.2-RELEASE 构建一个简单的结构。我将我的配置写入几个 spring-*.xml 中,当我调试我的应用程序时,它显示我的 Controller 中的 serviceimpl 有值,而 serviceimpl 中的 daoimpl 没有。

角色 Controller

@Controller 
@RequestMapping("/role") 
public class RoleController { 
 
    @Autowired 
    IRoleInfoService roleInfoService; 
 
    @RequestMapping("index") 
    public String Index(){ 
 
 
        return "/role/index"; 
    } 
 
    @RequestMapping("getall") 
    public @ResponseBody List<RoleInfo> getAll() throws SQLException { 
        return roleInfoService.getAll(); 
    } 
} 

RoleInfoServiceImpl

public class RoleInfoServiceImpl implements IRoleInfoService { 
 
    @Autowired 
    RoleInfoDaoImpl roleInfoDao ; 
 
    @Override 
   public List<RoleInfo> getAll() throws SQLException { 
        return roleInfoDao.getAll(); 
    } 
} 

RoleInfoDaoImpl

public class RoleInfoDaoImpl implements IRoleInfoDao { 
 
    @Override 
    public List<RoleInfo> getAll() throws SQLException { 
        return null; 
 
    } 
} 

web.xml

<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> 

springmvc.xml

<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> 

Spring 服务.xml

<bean class="keyy.service.serviceimpl.RoleInfoServiceImpl"></bean> 

spring-dao.xml

<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> 

抱歉,我忘记将我的 RoleInfoDaoImpl 放入配置文件中,但它确实存在,这是我的 spring-dao.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" 
       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> 

我在我的 RoleInfoDalImpl 类中删除了 dataSourceFactory 实例以简化这里的问题,两种情况下问题仍然存在。


我的项目的层次结构:(我的声誉限制了图像的数量)

-|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 

RoleInfoServiceImpl has value RoleInfoDaoImpl is null

好吧,我仍然无法识别是什么导致了我的问题,我尝试使用 JavaConfig 重建我的 SpringDemo,它工作正常 ----- 我没有像使用 xml 那样使用 @ComponentScan 或 @Service @Repository。任何人都可以解释一下吗?请

请您参考如下方法:

服务

   @Service  
   public class RoleInfoServiceImpl implements IRoleInfoService { 

   @Repository 
   public class RoleInfoDaoImpl implements IRoleInfoDao {  

注意:如果您依赖注解,最好只使用注解。现在您的服务正在使用 xml 配置。


标签:springMVC
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号