zoukankan      html  css  js  c++  java
  • spring之自动装配bean

    1、通过xml设置自动装配:

    实体类:

    xml文件:

    autowire可改为:

     autowire="byType"

    2、通过注解方式实现自动装配:

    先加入约束:

    xmlns:context="http://www.springframework.org/schema/context"

    加上标签开启:

    <context:annotation-config/>

    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
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:component-scan base-package="pojo"/>
        <context:annotation-config/>
    </beans>

    解析:

    实体类上的操作:

     此外:

    dao层使用:@Repository

    service层使用:@Service

    controller层使用:@Controller

    进行自动托管到spring,功能和@Component一致。

    3、纯java类进行配置:

    核心配置类写法:

     获取数据测试:

  • 相关阅读:
    tornado web 框架的认识
    JavaScript 作用域知识点梳理
    服务器
    git——学习
    webservice——和API接口
    celery——任务调度模块
    supervisor——进程管理工具
    Python常用的语句
    数据类型比较总结
    字符集和字符编码问题
  • 原文地址:https://www.cnblogs.com/wmskywm/p/13619855.html
Copyright © 2011-2022 走看看