Spring4学习回顾之路03—XML配置Bean ,依赖注入的方式
配置Bean的形式可以基于XML文件的方式,也可以基于注解的方式,而Bean的配置方式可以通过全类名(反射),通过工厂方式和FactoryBean。
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.xsd"> <!-- 配置bean id:标识 class:全类名 name:属性名,注意是针对set方法的属性名,不是变量的属性名 value:属性值 -->
<bean id="student" class="com.lql.spring01.Student">
<property name="name" value="lql"></property>
<property name="age" value="18"></property>
</bean>
</beans>
这是第一个HelloWorld的applicationContext.xml的代码,基于xml形式的Bean配置,其中的class="com.lql.spring01.Student"就是通过反射的方式在IOC容器中创建Bean,所以要求Bean中必须有无参构造。而id="student'则是引用Bean,唯一标识,需要注意的是,如果id没有被指定,那么Spring自动将类名作为Bean的名字。
Spring容器
在第一个HelloWorld中有这么一段代码;
public static void main(String[] args) {
//创建IOC容器
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取bean
Student student = app.getBean("student", Student.class);
//调用方法
student.Hello();
}
其中 ApplicationContext就是IOC容器,实际上它是个接口,Spring中提供了两种类型的IOC容器的实现,一个是BeanFactory: IOC容器的基本实现,一个是ApplicationContext: 提供了更多的高级特性,也是BeanFactory的子接口 , BeanFactory是Spring框架的基础设施,是面向Spring本身;而ApplicationContext面向使用Spring框架的开发者,几乎所有的应用场合都直接使用了ApplicationContext而非底层的BeanFactory,
但是无论是哪一种方式,配置文件是相同的;这里给出ApplicationContext的继承机构:
ApplicationContext的主要实现类:
-| ClassPathXmlApplicationContext: 从类路径下加载配置文件,
-| FileSystemXmlApplicationContext: 从文件系统中加载配置文件
ConfigurableApplicationContext扩展于ApplicationContext,新增了两个主要方法:refresh() 和 close(),这让ApplicationContext具有启动,刷新和关闭上下文的能力。
ApplicationContext在初始化上下文的时候就实例化所有单例的Bean.(BeanFactory在启动的时候不会去实例化Bean,只有从容器中拿Bean的时候才会去实例化)
实际上还有个WebApplicationContext: 是专门为WEB应用而准备的,它允许从相对于WEB根目录的路径完成初始化工作(SpringMVC)。
依赖注入的方式
Spring支持三种依赖注入的方式:
①属性注入,②构造器注入 , ③工厂方法注入(不推荐),第三种我工作2年都没见过一次,很少使用,不推荐,所以只说前两者,后者知道就行了
属性注入(setter()注入):属性注入即通过setter()方法注入Bean的属性值或依赖的对象;属性注入使用<property>元素,name属性指定Bean的属性名,value属性(或者使用<value>子标签)指定属性值。实际上,属性注入是实际应用中最常用的注入方式。示例如下:
<bean id="student" class="com.lql.spring01.Student">
<property name="name" value="lql"></property>
<property name="age" value="18"></property>
</bean>
构造器注入:通过构造方法注入Bean的属性值或者依赖的对象,它保证了Bean的实例在实例化后就可以使用,构造器注入在<constructor-arg>元素里声明属性,<constructor-arg>里面没有name属性。示例如下:
<bean id="student1" class="com.lql.spring01.Student">
<constructor-arg value="tom" index="0"></constructor-arg>
<constructor-arg value="19" index="1"></constructor-arg>
</bean>
需要注意的是需要在类中提供对应的构造方法,index是从0开始的,当有多个构造器的时候,可以使用type来区分参数类型;如下
<bean id="student1" class="com.lql.spring01.Student">
<constructor-arg value="tom" index="0" type="java.lang.String"></constructor-arg>
<constructor-arg value="19" index="1" type="int"></constructor-arg>
</bean>
上述的value="19" 是字符串形式,而我们代码中的age是int类型的,那Spring是怎么处理的?首先介绍一个概念 "字面值",可用字符串表示的值,可以通过<value>元素标签或value属性进行注入,基本数据类型及其封装类、String 等类型都可以采取字面值注入的方式,若字面值中包含特殊字符,可以使用<![CDATA[]]>把字面值包裹起来。所以上述的注入可以这么写:
<bean id="student2" class="com.lql.spring01.Student">
<constructor-arg index="0" type="java.lang.String">
<value><![CDATA[tom^]]></value>
</constructor-arg>
<constructor-arg index="1" type="java.lang.Integer">
<value>20</value>
</constructor-arg>
</bean>
Spring4学习回顾之路03—XML配置Bean ,依赖注入的方式的更多相关文章
- Spring4学习回顾之路06- IOC容器中Bean的生命周期方法
SpringIOC容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行特定的任务! Spring IOC容器对Bean的生命周期进行管理的过程: -通过构造器或者工厂方法创建 ...
- Spring4学习回顾之路04—引用其他Bean,集合数据注入,内部Bean
引用其他Bean 组件应用程序的Bean经常需要相互协作以完成应用程序的功能,所以要求Bean能够相互访问,就必须在Bean配置文件中指定Bean的引用.在Bean的配置文件中可以用过<ref& ...
- Spring4学习回顾之路09-基于注解的方式配置bean
一:基于注解配置Bean 首先介绍下组件扫描(component scanning): Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 包括: -@Component ...
- Spring4学习回顾之路07- 通过工厂方法配置Bean
一:通过静态工厂配置Bean 建立Student.java package com.lql.srping04; /** * @author: lql * @date: 2019.10.28 * Des ...
- Spring4学习回顾之路01—HelloWorld
以前公司一直使用的是spring3.0,最近一段时间开始用了4.0,官网上都已经有了5.0,但是很多知识点已经忘了差不多了,趁现在项目不忙写写随笔,一来回顾自己的知识点,二来如果能帮助比我还小白的小白 ...
- Spring4学习回顾之路11-AOP
Srping的核心除了之前讲到的IOC/DI之外,还有一个AOP(Aspect Oriented Programming:面向切面编程):通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术 ...
- Spring4学习回顾之路05—自动装配,Bean的继承,依赖和作用域
自动装配 xml配置里的Bean的自动装配,Spring IOC容器可以自动装配Bean,仅仅需要做的是在<bean>标签里的autowire属性里指定自动装配的模式. ①byType(根 ...
- Spring4学习回顾之路10-Spring4.x新特性:泛型依赖注入
泛型依赖注入:Spring 4.x中可以为子类注入子类对应的泛型类型的成员变量的引用. 话语太过抽象,直接看代码案例,依次建立如下代码: User.java package com.lql.sprin ...
- Spring4学习回顾之路08- FactoryBean配置Bean
建立Student.java package com.lql.srping04; /** * @author: lql * @date: 2019.10.28 * Description: */ pu ...
随机推荐
- 2019.9.5 Balanced Lineup
题目传送门 板子*3 #include<iostream> #include<cstdio> #include<cstring> #include<cmath ...
- Selenium报错:StaleElementReferenceException
一个学生在操作页面跳转时遇到一个Selenium报错, 如下图所示: StaleElementReferenceException: Message: stale element reference: ...
- td中文字居中
<style> .table_style{width: 100%;height: auto;} .table_style tr td{text-align: center;vertical ...
- npm修改源
获取原来的镜像地址 npm get registry 修改源 npm config set registry http://registry.npm.taobao.org/
- PPT:很多文字如何排版?(PPT如何美化?)
1.加入图标.形状(或者加入图片和色块搭配) 2.提取关键词作为标题(这个很重要) 下面看下示例: 这个是没修改前 修改后: 相信大家都会喜欢第二张吧 再看一个: 最近在学做PPT,有点上瘾了,哈哈哈 ...
- 前端知识点回顾之重点篇——CORS
CORS(cross origin resource sharing)跨域资源共享 来源:http://www.ruanyifeng.com/blog/2016/04/cors.html 它允许浏览器 ...
- 使用pyinstaller 打包python程序
1.打开PyCharm的Terminal,使用命令pip install pyinstaller安装pyinstaller 2.打包命令:pyinstaller --console --onefile ...
- java ldap用户密码md5加密
在这里不过多介绍ldap,因为这样的文章特别多,这里就简单直接的记录这一个问题. 在springboot中通过引入spring-boot-starter-data-ldap,使用LdapTemplat ...
- o enclosing instance of type ArrayList_day02 is accessible. Must qualify the allocation with an enclosing instance of type ArrayList_day02
错误日志: 这个错误是因为我创建的一个类,内中又创建了一个内部类,为什么呢在new内部类的时候出现错误呢,因为类中方法(函数)是在是在public static void main(String [] ...
- python #!/usr/bin/python作用
#!/usr/bin/python指定用什么解释器运行脚本以及解释器所在的位置 # -*- coding: utf-8 -*-用来指定文件编码为utf-8的PEP 0263 -- Defining P ...