大憨豆先生 2017-12-11 04:31 采纳率: 40%
浏览 6999
已采纳

ssm 整合 service层使用Autowired注入dao 报红,可以使用,为什么?

图片说明

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:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">



    <!-- 读取数据库配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>


    <!-- 启动上下文的注解配置 -->
    <context:annotation-config/>

    <!-- 扫描含有注解的包 -->
    <context:component-scan base-package="niit.dxs.lehuSSM.service"/>


    <!-- 配置数据源,使用阿里巴巴的druid数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"  init-method="init" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>

        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <!--初始化大小--><property name="initialSize" value="20"/>
        <!--连接池最大使用连接数量-->
        <property name="maxActive" value="20"/>
        <!--连接池最小空闲-->
        <property name="minIdle" value="0"/>
        <!--配置获取连接等待超时的时间-->
        <property name="maxWait" value="60000"/>
        <!--配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒-->
        <property name="timeBetweenEvictionRunsMillis" value="60000"/>
        <!--配置一个连接在池中最小生存的时间,单位是毫秒-->
        <property name="minEvictableIdleTimeMillis" value="300000"/>
        <!--连接空闲时测试是否有效-->
        <property name="testWhileIdle" value="false"/>
        <!--获取连接时测试是否有效-->
        <property name="testOnBorrow" value="false"/>
        <!--归还连接时是否测试有效-->
        <property name="testOnReturn" value="false"/>
        <!--打开PSCache缓存,并且指定每个连接上PSCache的大小-->
        <property name="poolPreparedStatements" value="true"/>
        <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>

    </bean>

    <!-- 配置 mybatis 的SessionFactory 实例 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 指定数据源 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 指定映射文件位置 -->
        <property name="mapperLocations" value="classpath*:/niit/dxs/lehuSSM/mapper/*.xml"/>
        <!-- 指定别名 -->
        <property name="typeAliasesPackage" value="niit.dxs.lehuSSM.domain"/>
    </bean>


    <!--配置DAO接口所在的包-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="niit.dxs.lehuSSM.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

    <!--配置一个事务管理器的bean -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
          p:dataSource-ref="dataSource"/>

    <!--配置声明式事务,在使用处加注解@Transactional-->
    <tx:annotation-driven transaction-manager="transactionManager"  proxy-target-class="true"/>

    <!--用于文件上传-->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
          p:defaultEncoding="UTF-8" p:maxUploadSize="5242880"/>

</beans>

spring_mvc-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"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--启动MVC注解-->
    <mvc:annotation-driven/>

    <!--扫描含有注解的包,(控制器所在的包)-->
    <context:component-scan base-package="niit.dxs.lehuSSM.controller"/>

    <!--配置视图解析器,指定视图所在路径,视图文件后缀名-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:viewClass="org.springframework.web.servlet.view.JstlView" p:prefix="/"
          p:suffix=".jsp"/>

    <!--保证静态资源不被拦截-->
    <mvc:default-servlet-handler/>

    <!-- 拦截器 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <!-- 表示拦截所有 -->
            <mvc:mapping path="/**"/>
            <!--<mvc:exclude-mapping path="/css/*.css"/>-->
            <!-- 指定拦截器 -->
            <bean class="niit.dxs.lehuSSM.interceptor.LoginInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>

</beans>

web.xml 配置

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

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

  <display-name>lehuSSM</display-name>

  <!--默认启动页-->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <!-- 加载spring容器 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml,classpath:spring_mvc-servlet.xml</param-value>
  </context-param>

  <!-- 过滤器统一编码 -->
  <filter>
    <filter-name>Spring character encoding</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>
    <init-param><!-- 过滤响应字符集 -->
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>Spring character encoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!-- spring容器配置 ,当服务器启动,加载web.xml配置文件-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>


  <!-- 控制器 当服务器启动,加载spring_mvc-servlet.xml配置文件-->
  <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:spring_mvc-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- mvc控制器映射 -->
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

  • 写回答

7条回答 默认 最新

  • 关注

    这个应该是一个警告,可能是你的变量命名有问题,
    idea设置中是不是警告用的就是红色下标,在eclipse中这种警告是黄色下标。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理