热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SpringIOC基于XML配置使用

一、springIOC简介ApplicationContext是SpringIoC容器实现的代表,它负责实例化,配置和组装Bean二、环境准备创建

一、springIOC简介

ApplicationContext是Spring IoC容器实现的代表,它负责实例化,配置和组装Bean

二、环境准备


  1. 创建maven项目
  2. 添加依赖

<dependency><groupId>org.springframeworkgroupId><artifactId>spring-contextartifactId><version>5.2.6.RELEASEversion>dependency>

  1. 创建实体类

public class Student{private String name;private int age;private String hobby;
}

public class Person {private int id;private String realName;private String name;private Student student;private Date birthday;private List<String> hobbies;private Map<String,String> course;
}

  1. 在resources文件下创建XML文件springTest.xml

三、实例解析


1.实现依赖注入的两种方式


  • 基于set方法进行依赖注入
    1.注入的属性需添加set方法
    2.name值时set方法名字来的

<bean class&#61;"com.tedu.liyu.entry.Student" id&#61;"student"><property name&#61;"age" value&#61;"12">property><property name&#61;"hobby" value&#61;"王者荣耀">property><property name&#61;"name" value&#61;"李白">property>bean>

创建测试类&#xff1a;

public class TestIOC {ClassPathXmlApplicationContext ioc;&#64;Beforepublic void before(){ioc &#61; new ClassPathXmlApplicationContext("springTest.xml");}&#64;Testpublic void test1(){Student student &#61; ioc.getBean("student",Student.class);System.out.println(student);}
}

  • 基于构造函数的依赖注入
    1.注入的实体类需添加有参的构造方法
    2.name值为构造方法的入参变量

public Student(String name, int age, String hobby) {this.name &#61; name;this.age &#61; age;this.hobby &#61; hobby;
}

<bean class&#61;"com.tedu.liyu.entry.Student" id&#61;"student"><constructor-arg name&#61;"name" value&#61;"十三中">constructor-arg><constructor-arg name&#61;"age" value&#61;"18">constructor-arg><constructor-arg name&#61;"hobby" value&#61;"数学">constructor-arg>bean>

2.复杂类型的注入

下面采用的是set注入&#xff0c;所以实体类中需要添加相关的set方法

<bean class&#61;"com.tedu.liyu.entry.Person" id&#61;"person"><property name&#61;"id" value&#61;"1">property><property name&#61;"name"><null>null>property><property name&#61;"realName" value&#61;"">property><property name&#61;"birthday" value&#61;"2020/05/20">property><property name&#61;"student"><bean class&#61;"com.tedu.liyu.entry.Student"><property name&#61;"name" value&#61;"张三">property><property name&#61;"hobby" value&#61;"打篮球">property><property name&#61;"age" value&#61;"18">property>bean>property><property name&#61;"hobbies"><list><value>唱歌value><value>跳舞value>list>property><property name&#61;"course"><map><entry key&#61;"1" value&#61;"Java">entry><entry key&#61;"2" value&#61;"Js">entry>map>property>
bean>

3.自动注入

<bean class&#61;"com.tedu.liyu.entry.Student" id&#61;"student"><constructor-arg name&#61;"name" value&#61;"十三中">constructor-arg><constructor-arg name&#61;"age" value&#61;"18">constructor-arg><constructor-arg name&#61;"hobby" value&#61;"数学">constructor-arg>
bean>
<bean class&#61;"com.tedu.liyu.entry.Person" id&#61;"person2" autowire&#61;"byName"><property name&#61;"id" value&#61;"999">property><property name&#61;"name" value&#61;"666">property>
bean>

当person对象中需要引用另一个student对象时&#xff0c;在自动注入时&#xff0c;spring会在IOC容器中自动找到匹配的进行注入。
byName&#xff1a;按照名字进行装配&#xff0c;以属性名作为id去容器中查找组件&#xff0c;进行赋值&#xff0c;如果找不到则装配null
byType&#xff1a;按照类型进行装配,以属性的类型作为查找依据去容器中找到这个组件&#xff0c;如果有多个类型相同的bean对象&#xff0c;那么会报异常&#xff0c;如果找不到则装配null

4.Bean的作用域

singleton&#xff1a;单例&#xff0c;只会在IOC容器中创建一次
prototype:多例&#xff0c;每次获取IOC容器都会new一个bean

<bean class&#61;"com.tedu.liyu.entry.Student" id&#61;"student3" scope&#61;"singleton">
bean>

5.生命周期回调

两种方式&#xff1a;
1.使用接口实现的方式实现生命周期的回调初始化方法&#xff1a;
实现接口InitializingBean&#xff0c;重afterPropertiesSet方法销毁方法&#xff1a;
实现接口DisposableBean&#xff0c;重写destroy方法

<bean class&#61;"com.tedu.liyu.entry.Student" id&#61;"student">bean>

public class Student implements InitializingBean , DisposableBean {public void afterPropertiesSet() throws Exception {System.out.println("生命周期回调---初始化");}public void destroy() throws Exception {System.out.println("生命周期回调---销毁");}
}

2.使用对应bean里创建的方法实现生命周期的回调

<bean class&#61;"com.tedu.liyu.entry.Student" id&#61;"student" init-method&#61;"init" destroy-method&#61;"destroy">bean>

public class Student{public void init(){System.out.println("生命周期回调---初始化");}public void destroy(){System.out.println("生命周期回调---销毁");}
}

示例如上&#xff1a;init-method的值为实体类中自定义初始化的方法名&#xff0c;destroy-method的值为实体类中自定义销毁的方法名

6.懒加载


<bean class&#61;"com.tedu.liyu.entry.Student" id&#61;"student" lazy-init&#61;"false">bean>

7.加载外部配置文件

1.先在resources下添加springContext.properties配置文件

student.name&#61;123
student.age&#61;10
student.hobby&#61;中国

<context:property-placeholder location&#61;"springContext.properties" file-encoding&#61;"UTF-8"/>
<bean class&#61;"com.tedu.liyu.entry.Student" id&#61;"student"><property name&#61;"name" value&#61;"${student.name}">property><property name&#61;"age" value&#61;"${student.age}">property><property name&#61;"hobby" value&#61;"${student.hobby}">property>
bean>

示例如上&#xff1a;需要注意的是&#xff0c;需要添加file-encoding&#61;“UTF-8”&#xff0c;不然配置文件中有中文时&#xff0c;引用可能会乱码。

8.SpEL的使用

<bean class&#61;"com.tedu.liyu.entry.Student" id&#61;"student"><constructor-arg name&#61;"name" value&#61;"十三中">constructor-arg><constructor-arg name&#61;"age" value&#61;"18">constructor-arg><constructor-arg name&#61;"hobby" value&#61;"数学">constructor-arg>
bean>
<bean class&#61;"com.tedu.liyu.entry.Person" id&#61;"person"><property name&#61;"name" value&#61;"#{student.name}">property><property name&#61;"student" value&#61;"#{student}">property><property name&#61;"birthday" value&#61;"2020/05/20">property>
bean>

SpEL:Spring Expression Language,spring的表达式语言&#xff0c;支持运行时查询操作对象


推荐阅读
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • FeatureRequestIsyourfeaturerequestrelatedtoaproblem?Please ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • 浏览器中的异常检测算法及其在深度学习中的应用
    本文介绍了在浏览器中进行异常检测的算法,包括统计学方法和机器学习方法,并探讨了异常检测在深度学习中的应用。异常检测在金融领域的信用卡欺诈、企业安全领域的非法入侵、IT运维中的设备维护时间点预测等方面具有广泛的应用。通过使用TensorFlow.js进行异常检测,可以实现对单变量和多变量异常的检测。统计学方法通过估计数据的分布概率来计算数据点的异常概率,而机器学习方法则通过训练数据来建立异常检测模型。 ... [详细]
  • Spring学习(4):Spring管理对象之间的关联关系
    本文是关于Spring学习的第四篇文章,讲述了Spring框架中管理对象之间的关联关系。文章介绍了MessageService类和MessagePrinter类的实现,并解释了它们之间的关联关系。通过学习本文,读者可以了解Spring框架中对象之间的关联关系的概念和实现方式。 ... [详细]
author-avatar
wei5xiao5zou6bian6tian
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有