• Spring框架总结(五)


    自动装配(了解)

    根据名称自动装配:autowire="byName"

    自动去IOC容器中找与属性名同名的引用的对象,并自动注入

    延续使用user、dao、service、action

    一、局部改变自动化注入方法,更改bean.xml(根据名字自动加载)

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xsi:schemaLocation="
     6         http://www.springframework.org/schema/beans
     7         http://www.springframework.org/schema/beans/spring-beans.xsd
     8         http://www.springframework.org/schema/context
     9         http://www.springframework.org/schema/context/spring-context.xsd">
    10 
    11 
    12     <!-- 对象属性赋值 通过构造函数 -->
    13     <bean id="user" class="com.liuyang.auto.User">
    14     </bean>
    15     <!-- dao注入 -->
    16     <bean id="dao" class="com.liuyang.auto.UserDAO">
    17     </bean>
    18 
    19     <!-- service注入 -->
    20     <bean id="us" class="com.liuyang.auto.UserService" autowire="byType">
    21     </bean>
    22 
    23     <!-- action注入 ,,根据名称自动装配,userAction注入的属性,会去IOC容器中自动查找与属性同名的对象 -->
    24     <bean id="userAction" class="com.liuyang.auto.UserAction"
    25         autowire="byType">
    26     </bean>
    27 </beans>

    全局更改,加了一个标签default-autowire="byName"

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xsi:schemaLocation="
     6         http://www.springframework.org/schema/beans
     7         http://www.springframework.org/schema/beans/spring-beans.xsd
     8         http://www.springframework.org/schema/context
     9         http://www.springframework.org/schema/context/spring-context.xsd" default-autowire="byName"> 
    10 
    11 
    12     <!-- 对象属性赋值 通过构造函数 -->
    13     <bean id="user" class="com.liuyang.auto.User">
    14     </bean>
    15     <!-- dao注入 -->
    16     <bean id="dao" class="com.liuyang.auto.UserDAO">
    17     </bean>
    18 
    19     <!-- service注入 -->
    20     <bean id="us" class="com.liuyang.auto.UserService">
    21     </bean>
    22 
    23     <!-- action注入 ,,根据名称自动装配,userAction注入的属性,会去IOC容器中自动查找与属性同名的对象 -->
    24 
    25     <bean id="userAction" class="com.liuyang.auto.UserAction"
    26         autowire="byType">
    27     </bean>
    28 </beans>

    二、根据类型自动加载

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xsi:schemaLocation="
     6         http://www.springframework.org/schema/beans
     7         http://www.springframework.org/schema/beans/spring-beans.xsd
     8         http://www.springframework.org/schema/context
     9         http://www.springframework.org/schema/context/spring-context.xsd">
    10 
    11 
    12     <!-- 对象属性赋值 通过构造函数 -->
    13     <bean id="user" class="com.liuyang.auto.User">
    14     </bean>
    15     <!-- dao注入 -->
    16     <bean id="dao" class="com.liuyang.auto.UserDAO">
    17     </bean>
    18 
    19     <!-- service注入 -->
    20     <bean id="us" class="com.liuyang.auto.UserService" autowire="byType">
    21     </bean>
    22 
    23     <!-- action注入 ,,根据名称自动装配,userAction注入的属性,会去IOC容器中自动查找与属性同名的对象 -->
    24     <bean id="userAction" class="com.liuyang.auto.UserAction"
    25         autowire="byType">
    26     </bean>
    27 </beans>

    全局加default-autowire="byType"

    必须确保改类型在IOC容器中只有一个对象;否则报错

    总结:

            Spring提供的自动装配主要是为了简化配置; 但是不利于后期的维护。(一般不推荐使用)

  • 相关阅读:
    aliyun服务器安装AMH面板
    自动化测试框架Appium的安装和使用
    Java中的replace()函数
    Spring框架中的控制反转和依赖注入
    Spring MVC中ModelAndView
    Java中 VO、 PO、DO、DTO、 BO、 QO、DAO、POJO的概念
    bootstrap中常用的元素类名详解
    处理网页上的字符溢出的方法
    前端开发中用过的比较好用的框架
    php laravel框架学习笔记 (一) 基本工作原理
  • 原文地址:https://www.cnblogs.com/liuyangfirst/p/6535317.html
Copyright © 2020-2023  润新知