美文网首页
【spring】spring ioc

【spring】spring ioc

作者: giraffecode9668 | 来源:发表于2019-05-22 17:49 被阅读0次

2019-05-22
将配置扫描进IOC容器中时,可以使用XML扫描或者是用@configure注解java类。
项目中用的是XML扫描
XML扫描:

   <context:component-scan base-package="com.atguigu.crud.controller" />

当然,扫描多次,分情况的扫描,在spring-frame(context)配置文件中,扫描除@controller;在spring-mvc配置文件中,只扫描@controller。

//在spring配置文件中,扫描除@controller
    <context:component-scan base-package="com.atguigu.crud.controller" >
        <!--扫描除controller-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
//在spring-mvc配置文件中,只扫描@controller
    <context:component-scan base-package="com.atguigu.crud.controller" use-default-filters="false">// use-default-filters="false" 默认为true,扫描@service与其他
        <!--只扫描controller-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

另:在其他项目中看到只扫描一次,在spring中扫描,在spring-mvc不用扫描

   <context:component-scan base-package="com.atguigu.crud.controller" />

<import resource="classpath:spring-context-druid-stat.xml"/>能够将本配置文件分为多个文件集合。



@Reposity注解在Dao中不用写,在xml配置如下。
 <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage"
            value="com.maiyajf.loan.*.dao,com.maiyajf.base.utils.sequence" />
         <!-- 存在多个sqlSessionFactory或sqlSessionTemplates模板时,property可以指定 ,可省略-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <!-- 指定@Repositoty注释-->
        <!-- <property name = "annotationClass" value = "org.springframework.stereotype.Repository"/>-->
    </bean>

又有:

    <!--配置扫描器,将mybatis接口的实现加入到IOC容器中-->
    <mybatis:scan base-package="com.hunter.ssm_crud.dao"/>

是否用处一样,有待考验

Spring ioc.png

一篇关于注解的博客:https://blog.csdn.net/wee616/article/details/78596360

相关文章

网友评论

      本文标题:【spring】spring ioc

      本文链接:https://www.haomeiwen.com/subject/xlduzqtx.html