zoukankan      html  css  js  c++  java
  • Spring 的autowired大坑

    ---恢复内容开始---

    @autowired

    自动注入大坑

    这几天在spring的配置上花了许多时间....

    首先appcontextconfig.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:tx="http://www.springframework.org/schema/tx"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
    http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    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">

    <!--配置spring自动扫描的包-->

    <context:component-scan base-package="z.talent"/>

    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 配置数据源 ,dbcp -->

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${mysql.driver}"/>
    <property name="url" value="${mysql.userurl}"/>
    <property name="username" value="${mysql.username}"/>
    <property name="password" value="${mysql.password}"/>
    <property name="maxActive" value="30"/>
    <property name="maxIdle" value="5"/>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 注入数据库连接池 -->
    <property name="dataSource" ref="dataSource" />
    <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
    <property name="configLocation" value="classpath:mybatisconfig.xml" />
    <!-- 扫描entity包 使用别名 -->

    <!-- 扫描sql配置文件:mapper需要的xml文件 -->
    <property name="mapperLocations" value="classpath:mapper/*.xml" />
    </bean>

    <!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- 注入sqlSessionFactory -->
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    <!-- 给出需要扫描Dao接口包 -->
    <property name="basePackage" value="z.talent.mapper" />
    </bean>
    <bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!-- 注入数据库连接池 -->
    <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 配置基于注解的声明式事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    <!-- 遍历所有的类 官方解释 写上这句会自动注册 指定包的class(@Component, @Repository, @Service, @Controller, @RestController,
    @ControllerAdvice, and @Configuration)为bean -->

    <!-- 加载框架mvc注释驱动 我自己说的 反正这两句少了就是不行,不信你试试-->
    <mvc:annotation-driven />
    <!-- 静态资源 image javascirpt css 不设置这个url访问image/*.png 肯定返回404 因为没写这个给controller处理里面并没有处理这个当然返回404-->
    <mvc:resources location="/WEB-INF/resource/" mapping="/resource/**"
    cache-period="864000" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
    </bean>
    <!-- 静态资源访问处理 -->
    <mvc:default-servlet-handler/>
    </beans>

     >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    >>>>>>>>>>>>>>>>>>>>>

    >>>>>>>>>>>>>

    >>>>>>>>
    >>>>>>..

    >>>>>

    >>>

    >>

    >

    紧接着呢

    》》》

    开始写dao层

    首先我要说明下其实交给spring管理了,就没有必要自己实现dao接口了

    mybatis的mapper就相当于dao层了,,,

    接着都是spring的事了

    然后嘞就是啥呢

    然后开始注入service层

    @autowired的mapper

    service层自动注入了mapper

    那么使用service的地方也要@autowired 这个service

    autowired 不能断,要一直应用下去

    不然会爆空指针错误  这里指的是 mapper

    哈哈哈哈哈哈哈

    期间呢还发生了需要故事

    如果报了 logger 啥的错误  type different class类似字眼记得

    把tomcat下bin的commed log 。jar删了

    xxiixixixixixihahahaha

    哈哈哈哈

  • 相关阅读:
    ASP.NET SignalR HubPipelineModule
    MongoDB新版本特性
    Xamarin向iOS和Android引入C# Async支持
    Redis开源文档《Redis设计与实现》[转]
    WCF的追踪分析工具——SvcPerf
    Windows Azure移动服务更新,支持Android、活动目录和更多语言,并支持在东亚地区部署服务
    社区网站系统 jsGen
    单元测试同时支持 NUnit/MSTest
    .NET的微型Web框架 Nancy
    通过二维码登录(CSC模式)
  • 原文地址:https://www.cnblogs.com/zhangtalent/p/8450085.html
Copyright © 2011-2022 走看看