Spring学习之旅(四)Spring工作原理再探
上篇博文对Spring的工作原理做了个大概的介绍,想看的同学请出门左转。今天详细说几点。
(一)Spring IoC容器及其实例化与使用
Spring IoC容器负责Bean的实例化、配置和组装工作有两个接口:BeanFactory和ApplicationContext。其中ApplicationContext继承于BeanFactory,对企业级应用开发提供了更多的支持。在实际应用中都是用该接口。
1)实例化Spring容器(主要有四种)
1.ClassPathXmlApplicationContext: 在类路径下寻找配置XML文件实例化容器。
ApplicationContext act = new ClassPathXmlApplicationContext("hellobean.xml");
2.FileSystemXmlApplicationContext:在文件系统路径下寻找配置文件来实例化容器。
ApplicationContext act=new FileSystemXmlApplicationContext("d:/beans.xml");
3.XmlWebApplicationContext:从Web应用目录WEB-INF中的XML配置文件实例化容器。(小编未能实现成功,请实现成功的同学指教)
WebApplicationContext wctx = new XmlWebApplicationContext();
4.在web.xml配置文件中,通过配置监听器实例化容器。(假定已经配置了Spring的配置文件)
在web.xml中注册Spring提供的Servlet监视器,它会在当前Web应用被加载时将Spring的ApplicationContext保存到ServletContext对象中。
Spring配置文件可指定多个,之间用逗号隔开。
//在网页中通过request对象或其他方式,获取Web服务器容器
ServletContext sc=request.getServletContext();
//利用spring框架提供的静态方法,从Web服务器中获取Spring容器
WebApplicationContext wact=WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
2)生成Bean实例
Spring容器通过getBean()方法,从容器中获取所管理的对象。
例如:
HelloBeans student=(HelloBeans)wctx.getBean("stu1");
(二)基于XML文件方式的Bean配置
在java容器中形成Bean称为装配。
Bean的装配形式有两种:基于XML文件的方式和基于注解的方式。
基于XML文件的方式就是用一个XML文件对Bean信息实施配置。主要有两部分:命名空间、Bean及有关信息的配置。
4种配置Bean的方法:
例子:定义两个实体类
public class Address {
private String city;
private String school;
//无参构造器
public Address(){
this.city="taian";
this.school="nongda";
}
//有参构造器
public Address(String city,String school){
this.city=city;
this.school=school;
}
//省略了setter/getter方法
}
public class Student {
private String name;
private int age;
Address address;
//默认构造器
public Student(){}
//有参构造器
public Address(String name,int age,Address address){
this.name=name;
this.age=age;
this.address=address;
}
//省略了setter/getter方法
}
第1种配置方法,利用带参数的构造器注入:
<bean name="a1" class="com.edu.bean.Address">
<constructor-arg index="0" type="java.lang.String" value="北京"/>
<constructor-arg index="1" type="java.lang.String" value="清华"/>
</bean>
第2种配置方法,利用无参构造器注入:
<bean name="a2" class="com.edu.bean.Address"/>
第3种配置方法,利用属性的setter方法注入:
<bean name="a3" class="com.edu.bean.Address">
<property name="city" value="北京"></property>
<property name="school" value="清华"></property>
</bean>
第4种配置方法,利用属性的setter方法注入引用属性:
<bean name="addr" class="com.edu.bean.Address">
<property name="city" value="北京"></property>
<property name="school" value="清华"></property>
</bean> <bean name="ss" class="com.edu.bean.Student">
<property name="name" value="张三"></property>
<property name="age" value="20"></property>
<property name="address" ref="addr"></property>
</bean>
(三)Spring表达式——SpEL(Spring Expression Lanuage)
使用“#{...}”作为定界符。所有在大括号中的字符都将被认为是SpEL。SpEL为Bean的属性动态赋值提供了便利。
以下示例每一组两条语句均为等价表示:
<property name="count" value="#{5}"></property>
<property name="count" value="5"></property> <property name="address" value="#{addr}"></property>
<property name="address" ref="addr"></property> <property name="address" value="#{addr.city}"></property>
(四)基于注解方式的Bean配置
先说个例子:
打开eclipse,建立java web工程如下:
注意:除常规web项目导入包外,另需导入aop方面的jar包(放在lib目录下)
其中,UserDao类:
package com.edu.annotation; import org.springframework.stereotype.Service; @Service(value="abc")
public class UserDao { public void save(){
System.out.println("保存数据完成!");
}
}
beans-annotation.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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.edu.annotation"/> </beans>
用于测试的主类Main:
package com.edu.annotation; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext act = new ClassPathXmlApplicationContext("beans-annotation.xml");
UserDao userDao=(UserDao)act.getBean("abc");
userDao.save(); } }
运行结果为:保存数据完成!
从例子中我们可以看到,基于注解方式和基于XML方式Bean配置作用相同。
在java的实现类中,Spring提供了在类内进行Bean定义的标注(写在类名上一行),从而标识该类创建Bean,让Spring管理。
为了便于分类管理,基于注解方式的Bean配置分为4类:
1.@Component:基本注解(通用Bean标注)
2.@Respository:标识持久层组件
3.@Service:标识服务层(业务层)组件
4.@Controller:标识表现层组件
当一个类(组件被标注为Bean后),每一个Bean都有一个标识名称(给Bean命名)。分两种:
1.若没有在注解中指定,则实行Spring的默认命名策略:使用非限定类名,将类名的第一个字母小写;
2.若在注解中指定(使用value属性),就像例子中一样,则使用指定名称。
使用基于注解的Bean装配时的几点注意:
1.beans-annotation.xml中命名空间和不使用时的不同;
2.需导入aop的jar包。
更多详细的基于注解的Bean装配,请各位移步官网自行查看,这里不再赘述。
本篇参考书籍《Java EE框架开发技术与案例教程》
Spring学习之旅(四)Spring工作原理再探的更多相关文章
- Spring学习之旅(四)--高级装配Bean
条件化 bean 有时候我们要满足某种情况才将bean 初始化放入容器中. 基于环境初始化不同的 bean 1.申明接口并创建两个实现类 public interface Teacher { void ...
- Spring学习之旅(八)Spring 基于AspectJ注解配置的AOP编程工作原理初探
由小编的上篇博文可以一窥基于AspectJ注解配置的AOP编程实现. 本文一下未贴出的相关代码示例请关注小编的上篇博文<Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AO ...
- Spring学习之旅(十)--MockMvc
在之前的 Spring学习之旅(八)--SpringMVC请求参数 我们是通过在控制台输出来验证参数是否正确,但是这样做实在是太耗时间了,我们今天来学习下 MockMvc,它可以让我们不需要启动项目就 ...
- Spring学习(三)——Spring中的依赖注入的方式
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...
- Spring学习(二)——Spring中的AOP的初步理解[转]
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...
- Spring学习(二)——Spring中的AOP的初步理解
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...
- Spring学习之——手写Spring源码V2.0(实现IOC、D、MVC、AOP)
前言 在上一篇<Spring学习之——手写Spring源码(V1.0)>中,我实现了一个Mini版本的Spring框架,在这几天,博主又看了不少关于Spring源码解析的视频,受益匪浅,也 ...
- Spring学习之旅(三)Spring工作原理初探
详细的废话相信很多书籍视频资料都已经很多了,这里说几个小编个人认为对于理解Spring框架很重要的点.欢迎批评指正. 1)Spring的控制反转 先说说“依赖”,在面向对象程序设计中,类A中用到了类B ...
- 我的Spring学习记录(四)
虽然Spring管理这我们的Bean很方便,但是,我们需要使用xml配置大量的Bean信息,告诉Spring我们要干嘛,这还是挺烦的,毕竟当我们的Bean随之增多的话,xml的各种配置会让人很头疼. ...
随机推荐
- Python 22端口发邮件
#!/usr/bin/python#-*-coding:UTF-8-*- import smtplibimport timeimport os from email.mime.text import ...
- Python super() 函数的概念和例子
概念: super() 函数是用于调用父类(超类)的一个方法. super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO).重 ...
- C语言中assert()断言函数的概念及用法
断言函数的格式如下所示: void assert (int expression);如果参数expression等于零,一个错误消息将会写入到设备的标准错误集并且会调用abort函数,就会结束程序的执 ...
- Python xlrd xlwt 读取写入Excel.
import xlrd import xlwt #读取 xlrd.Book.encoding = "gbk" wb = xlrd.open_workbook(filename='s ...
- 我对开源C++网络库简单应用总结
网上有篇文章<开源免费的C/C++网络库(c/c++ sockets library) 七剑下天山>,看了之后觉得每个库都不错,这里我具体下载这些库看一下,简单总结一下: 顺便添加一些我找 ...
- C++版 - 剑指offer 面试题31:连续子数组的最大和 题解
剑指offer:连续子数组的最大和 提交网址: http://www.nowcoder.com/practice/459bd355da1549fa8a49e350bf3df484?tpId=13&am ...
- kibana从入门到精通-Kibana安装
作者其他ELK快速入门系列文章 Elasticsearch从入门到精通 logstash快速入门实战指南 简介 Kibana 是一款开源的数据分析和可视化平台,它是 Elastic Stack 成员之 ...
- man statd(rpc.statd中文手册)
本人译作集合:http://www.cnblogs.com/f-ck-need-u/p/7048359.html rpc.statd程序主要实现NFS锁相关内容,如普通的文件锁(NLM.NSM).文件 ...
- python布尔类型和逻辑运算
布尔类型 python中True表示真,False表示假,它们是布尔类型: >>> type(True) <class 'bool'> 在python中,bool的Tru ...
- vue_drf之实现短信验证码
一.需求 1,需求 我们在做网站开发时,登录页面很多情况下是可以用手机号接收短信验证码,然后实现登录的,那我们今天就来做一做这一功能. 伪代码: 进入登录页面,点击短信登录 输入手机号码,点击获取验证 ...