spring(代码片段)

小企鹅推雪球! 小企鹅推雪球!     2023-01-14     300

关键词:

Spring Beans 自动装配

  1. 可以使用·bean元素来声明 bean 和通过使用 XML 配置文件中的<constructor-arg>和<property>元素来注入 。
  2. Spring容器可以在不使用和 元素的情况下自动装配相互协作的 bean 之间的关系,这会减少XML配置的数量
  3. 自动装配模式 Spring 容器为来使用自动装配进行依赖注入
  4. 可以使用bean元素的autowire属性为一个bean定义指定自动装配模式
  5. bean的装配模式有以下几种
模式描述
no默认的设置,没有自动装配
byName由属性名自动装配,Spring容器在看到XML配置文件中bean的自动装配的属性设置为byName,然后尝试匹配,并且将它的属性与在配置文件中被定义为相同名称的 beans 的属性进行连接。
byType由属性数据类型自动装配。Spring 容器看到在 XML 配置文件中 bean 的自动装配的属性设置为 byType。然后如果它的类型匹配配置文件中的一个确切的 bean 名称,它将尝试匹配和连接属性的类型。如果存在不止一个这样的 bean,则一个致命的异常将会被抛出。
constructor类似于 byType,但该类型适用于构造函数参数类型。如果在容器中没有一个构造函数参数类型的 bean,则一个致命错误将会发生。
autodetectSpring首先尝试通过 constructor 使用自动装配来连接,如果它不执行,Spring 尝试通过 byType 来自动装配。

Spring自动装配的局限性

  1. 当自动装配始终在同一个项目中使用时,效果是最好的
  2. 如果不是使用自动装配,可能会使我们使用它来连接只有一个或者两个bean定义
  3. 自动装配可以显著减少需要指定的属性或者构造器参数
限制描述
重写的可能性可以使用总是重写自动装配的 和 设置来指定依赖关系。
原始数据类型不能自动装配所谓的简单类型包括基本类型,字符串和类。
混乱的本质自动装配不如显式装配精确,所以如果可能的话尽可能使用显式装配。

Spring 自动装配byName

  1. byName模式由属性名称指定自动装配。Spring容器看作beans,在 XML 配置文件中 beans 的 auto-wire 属性设置为 byName,然后,它尝试将它发属性和配置文件定义为相同命名称的beans进行匹配和连接。如果找到匹配项,它将注入这些 beans,否则,它将抛出异常。
  2. 在配置文件中,如果一个Bean定义设置为自动装配byName,并且它包含 spellChecker 属性(即,它有一个 setSpellChecker(…) 方法),那么 Spring 就会查找定义名为 spellChecker 的 bean,并且用它来设置这个属性。而且可以使用·<property> 标签连接其余的属性

Spring 自动装配byName样例

  1. 创建一个名称为 SpringExample 的项目,并且在已创建的项目的 src 文件夹中创建一个包 com.tutorialspoint。
  2. 使用 Add External JARs 选项,添加所需的 Spring 库,
  3. 在 com.tutorialspoint 包中创建 Java 类 TextEditor,SpellChecker 和 MainApp
  4. 在 src 文件夹中创建 Beans 的配置文件 Beans.xml。

TextEditor.java 文件的内容:

package com.tutorialspoint;
public class TextEditor 
   private SpellChecker spellChecker;
   private String name;
   public void setSpellChecker( SpellChecker spellChecker )
      this.spellChecker = spellChecker;
   
   public SpellChecker getSpellChecker() 
      return spellChecker;
   
   public void setName(String name) 
      this.name = name;
   
   public String getName() 
      return name;
   
   public void spellCheck() 
      spellChecker.checkSpelling();
   
  

依赖类文件 SpellChecker.java 的内容:

package com.tutorialspoint;
public class SpellChecker 
   public SpellChecker() 
      System.out.println("Inside SpellChecker constructor." );
   
   public void checkSpelling() 
      System.out.println("Inside checkSpelling." );
      

MainApp.java 文件的内容:

package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp 
   public static void main(String[] args) 
      ApplicationContext context = 
             new ClassPathXmlApplicationContext("Beans.xml");
      TextEditor te = (TextEditor) context.getBean("textEditor");
      te.spellCheck();
   

不适用自动装配byName的配置文件 Beans.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <!-- Definition for textEditor bean -->
   <bean id="textEditor" class="com.tutorialspoint.TextEditor">
       <property name="spellChecker" ref="spellChecker" />
       <property name="name" value="Generic Text Editor" />
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
   </bean>

</beans>

使用自动装配 “byName”的 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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <!-- Definition for textEditor bean -->
   <bean id="textEditor" class="com.tutorialspoint.TextEditor" 
      autowire="byName">
      <property name="name" value="Generic Text Editor" />
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
   </bean>

</beans>

运行该应用程序,输出结果为

Inside SpellChecker constructor.
Inside checkSpelling.

Spring自动装配byType

  1. byType模式由属性类型指定自动装配,Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 byType,如果它的 type 恰好与配置文件中 beans 名称中的一个相匹配,它将尝试匹配和连接它的属性,如果找到匹配项,它将注入这些 beans,否则,它将抛出异常
  2. 在配置文件中,如果一个 bean 定义设置为自动装配 byType,并且它包含 SpellChecker 类型的 spellChecker 属性,那么 Spring 就会查找类型为 SpellChecker 的 bean并且用它来设置这个属性,仍然可以使用 标签连接其余属性。

Spring自动装配byType样例

  1. 创建一个名称为 SpringExample 的项目,并且在已创建的项目的 src 文件夹中创建一个包 com.tutorialspoint。
  2. 使用 Add External JARs 选项,添加所需的 Spring 库,
  3. 在 com.tutorialspoint 包中创建 Java 类 TextEditor,SpellChecker 和 MainApp。
  4. 在 src 文件夹中创建 Beans 的配置文件 Beans.xml

TextEditor.java 文件的内容:

package com.tutorialspoint;
public class TextEditor 
   private SpellChecker spellChecker;
   private String name;
   public void setSpellChecker( SpellChecker spellChecker ) 
      this.spellChecker = spellChecker;
   
   public SpellChecker getSpellChecker() 
      return spellChecker;
   
   public void setName(String name) 
      this.name = name;
   
   public String getName() 
      return name;
   
   public void spellCheck() 
      spellChecker.checkSpelling();
   

``
**SpellChecker.java 的内容:**

```c
package com.tutorialspoint;
public class SpellChecker 
   public SpellChecker()
      System.out.println("Inside SpellChecker constructor." );
   
   public void checkSpelling() 
      System.out.println("Inside checkSpelling." );
      

MainApp.java 文件的内容:

package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp 
   public static void main(String[] args) 
      ApplicationContext context = 
             new ClassPathXmlApplicationContext("Beans.xml");
      TextEditor te = (TextEditor) context.getBean("textEditor");
      te.spellCheck();
   

正常情况下的配置文件 Beans.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <!-- Definition for textEditor bean -->
   <bean id="textEditor" class="com.tutorialspoint.TextEditor">
      <property name="spellChecker" ref="spellChecker" />
      <property name="name" value="Generic Text Editor" />
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
   </bean>

</beans>

使用自动装配 “byType”,那么你的 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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <!-- Definition for textEditor bean -->
   <bean id="textEditor" class="com.tutorialspoint.TextEditor" 
      autowire="byType">
      <property name="name" value="Generic Text Editor" />
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="SpellChecker" class="com.tutorialspoint.SpellChecker">
   </bean>

</beans>

Spring 由构造函数自动装配

  1. 由构造函数自动装配模式与 byType 非常相似,但它应用于构造器参数。Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 constructor。然后,它尝试把它的构造函数的参数与配置文件中 beans 名称中的一个进行匹配和连线。如果找到匹配项,它会注入这些 bean,否则,它会抛出异常
  2. 在配置文件中,如果一个 bean 定义设置为通过构造函数自动装配,而且它有一个带有 SpellChecker 类型的参数之一的构造函数,那么 Spring 就会查找定义名为 SpellChecker 的 bean,并用它来设置构造函数的参数。但是仍然可以使用 <constructor-arg>标签连接其余属性。

Spring 由构造函数自动装配

  1. 创建一个名称为 SpringExample 的项目,并且在已创建的项目的 src 文件夹中创建一个包 com.tutorialspoint。
  2. 使用 Add External JARs 选项,添加所需的 Spring 库,
  3. 在 com.tutorialspoint 包中创建 Java 类 TextEditor,SpellChecker 和 MainApp。
  4. 在 src 文件夹中创建 Beans 的配置文件 Beans.xml

TextEditor.java 文件的内容:

package com.tutorialspoint;
public class TextEditor 
   private SpellChecker spellChecker;
   private String name;
   public TextEditor( SpellChecker spellChecker, String name ) 
      this.spellChecker = spellChecker;
      this.name = name;
   
   public SpellChecker getSpellChecker() 
      return spellChecker;
   
   public String getName() 
      return name;
   
   public void spellCheck() 
      spellChecker.checkSpelling();
   

依赖类文件 SpellChecker.java 的内容:

package com.tutorialspoint;
public class SpellChecker 
   public SpellChecker()
      System.out.println("Inside SpellChecker constructor." );
   
   public void checkSpelling()
   
      System.out.println("Inside checkSpelling." );
     

MainApp.java 文件的内容

package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp 
   public static void main(String[] args) 
      ApplicationContext context = 
             new ClassPathXmlApplicationContext("Beans.xml");
      TextEditor te = (TextEditor) context.getBean("textEditor");
      te.spellCheck();
   

正常情况下的配置文件 Beans.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <!-- Definition for textEditor bean -->
   <bean id="textEditor" class="com.tutorialspoint.TextEditor">
      <constructor-arg  ref="spellChecker" />
      <constructor-arg  value="Generic Text Editor"/>
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
   </bean>

</beans>

使用自动装配 “by constructor”,的 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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <!-- Definition for textEditor bean -->
   <bean id="textEditor" class="com.tutorialspoint.TextEditor" 
      autowire="constructor">
      <constructor-arg value="Generic Text Editor"/>
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="SpellChecker" class="com.tutorialspoint.SpellChecker">
   </bean>

</beans>

spring介绍(代码片段)

Spring介绍介绍IOC介绍Spring容器管理对象1、添加依赖2、配置spring配置文件(applicationCintext.xml)3、创建实体类4、spring来管理容器5、通过容器获取对象Spring中DI介绍Spring中bean实例和依赖注入基于注解形式使用spring和mybatis的整合1、引... 查看详情

spring框架spring入门(代码片段)

Spring入门spring快速入门1、导入spring依赖maven仓库地址:https://mvnrepository.com/<!--https://mvnrepository.com/artifact/org.springframework/spring-context--><dependency><groupId>org.springframework 查看详情

java有用的spring代码(代码片段)

查看详情

spring(代码片段)

文章目录Spring框架概述Spring框架特点Spring-IOC容器详解IOC接口IOC操作-Bean管理Spring框架概述spring是轻量级的开源JavaEE框架Spring可以解决企业应用开发的复杂性Spring有两个核心部分:IOC和AOPIOC:控制反转,把创建对象过程... 查看详情

spring--spring数据绑定(代码片段)

Spring数据绑定使用场景SpringBeanDefinition到Bean实例创建Spring数据绑定SpringWeb参数绑定Spring数据绑定组件标准组件:org.springframework.validation.DataBinderWeb组件1.org.springframework.web.bind.WebDataBinder2.org.springframework.web.b 查看详情

spring--spring数据绑定(代码片段)

Spring数据绑定使用场景SpringBeanDefinition到Bean实例创建Spring数据绑定SpringWeb参数绑定Spring数据绑定组件标准组件:org.springframework.validation.DataBinderWeb组件1.org.springframework.web.bind.WebDataBinder2.org.springframework.web.b 查看详情

spring入门(代码片段)

Spring相关知识点整理Spring体系结构Spring程序开发步骤Spring配置文件Bean标签的基本配置Bean标签的范围配置默认情况下演示:`ApplicationContextapp=newClassPathXmlApplicationContext("applicationContext.xml");`内部Bean----匿名,外 查看详情

spring(代码片段)

文章目录SpringBeans自动装配Spring自动装配的局限性Spring自动装配byNameSpring自动装配byName样例Spring自动装配byTypeSpring自动装配byType样例Spring由构造函数自动装配Spring由构造函数自动装配SpringBeans自动装配可以使用·bean元素来声明bean... 查看详情

spring(代码片段)

文章目录Spring-IOC容器详解Spring容器类型SpringBeanFactory容器Spring应用程序示例SpringBean的定义Spring配置元数据IOC接口IOC操作-Bean管理注入案例Spring-IOC容器详解第一:什么是IOC?Spring容器是Spring框架的核心IOC是控制反转,... 查看详情

spring--spring校验(代码片段)

Spring校验使用场景Spring常规校验Spring数据绑定SpringWeb参数绑定SpringWebMVC/SpringWebFlux处理方法参数校验Validator接口设计接口的职责:Spring内部校验器接口,通过编程的方式校验目标对象核心方法supports(Class):校验目标类能否校验val... 查看详情

spring--spring校验(代码片段)

Spring校验使用场景Spring常规校验Spring数据绑定SpringWeb参数绑定SpringWebMVC/SpringWebFlux处理方法参数校验Validator接口设计接口的职责:Spring内部校验器接口,通过编程的方式校验目标对象核心方法supports(Class):校验目标类能否校验val... 查看详情

spring框架--springjdbc(代码片段)

Spring框架往期文章Spring框架(一)–spring搭建和IOCSpring框架(二)–SpringBean管理Spring框架(三)–SpringJDBCSpring框架(四)–AOP面向切面编程Spring框架(五)–Spring事务管理和Spring事务传播行为Spring是个一站式框架:Spring自身也提供了控制... 查看详情

spring笔记(代码片段)

目录狂神视频地址1、Spring1-1.Spring简介1-2.Spring的优点1-3.Spring组成1-4.Spring拓展2、IOC理论推导2-1.UserDao接口2-2.UserDaoImpl实现类2-3.UserService业务接口2-4.UserServiceImpl业务实现类2-5.测试3、IOC本质4、Spring入门程序4-1.实体类4-2.配置文件4-3.... 查看详情

spring笔记(代码片段)

目录狂神视频地址1、Spring1-1.Spring简介1-2.Spring的优点1-3.Spring组成1-4.Spring拓展2、IOC理论推导2-1.UserDao接口2-2.UserDaoImpl实现类2-3.UserService业务接口2-4.UserServiceImpl业务实现类2-5.测试3、IOC本质4、Spring入门程序4-1.实体类4-2.配置文件4-3.... 查看详情

spring(代码片段)

文章目录Spring基于Java的配置Spring基于Java的配置样例Spring基于Java的配置-注入Bean的依赖性Spring基于Java的配置-注入Bean的依赖性样例Spring@Import注解Spring生命周期回调Spring基于Java的配置基于Java的配置选项,可以在不用配置XML... 查看详情

spring学习笔记(代码片段)

目录1.Spring简介1.1Spring是什么1.2IOC和AOP简介1.3Spring的优势1.4Spring的体系结构2.Spring快速入门2.1Spring程序开发步骤2.2使用IDEA创建第一个Spring项目2.2.1导入Spring开发的基本包坐标2.2.2编写Dao接口和实现类2.2.3创建Spring核心配置文件2.2.4... 查看详情

初识-spring(代码片段)

1.Spring是什么?1.1什么是容器?1.2什么是IoC?1.2.1传统程序开发1.2.2解决办法1.2.3对比总结1.3什么是DI?2.总结2.1Spring是什么?如何理解Spring?2.2IoC和DI是啥?有什么区别?2.3Spring最核心的功能是啥?1.Spring是什么?我们通常所说的Spring指的是SpringF... 查看详情

spring入门(代码片段)

文章目录SpringBeans自动装配Spring自动装配的局限性Spring自动装配byNameSpring自动装配byName样例Spring自动装配byTypeSpring自动装配byType样例Spring由构造函数自动装配Spring由构造函数自动装配SpringBeans自动装配可以使用·bean元素来声明bean... 查看详情