上篇博文对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装配,请各位移步官网自行查看,这里不再赘述。

链接奉上:https://docs.spring.io/spring/docs/5.0.4.RELEASE/spring-framework-reference/core.html#beans-annotation-config

本篇参考书籍《Java EE框架开发技术与案例教程》

Spring学习之旅(四)Spring工作原理再探的更多相关文章

  1. Spring学习之旅(四)--高级装配Bean

    条件化 bean 有时候我们要满足某种情况才将bean 初始化放入容器中. 基于环境初始化不同的 bean 1.申明接口并创建两个实现类 public interface Teacher { void ...

  2. Spring学习之旅(八)Spring 基于AspectJ注解配置的AOP编程工作原理初探

    由小编的上篇博文可以一窥基于AspectJ注解配置的AOP编程实现. 本文一下未贴出的相关代码示例请关注小编的上篇博文<Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AO ...

  3. Spring学习之旅(十)--MockMvc

    在之前的 Spring学习之旅(八)--SpringMVC请求参数 我们是通过在控制台输出来验证参数是否正确,但是这样做实在是太耗时间了,我们今天来学习下 MockMvc,它可以让我们不需要启动项目就 ...

  4. Spring学习(三)——Spring中的依赖注入的方式

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  5. Spring学习(二)——Spring中的AOP的初步理解[转]

      [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...

  6. Spring学习(二)——Spring中的AOP的初步理解

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  7. Spring学习之——手写Spring源码V2.0(实现IOC、D、MVC、AOP)

    前言 在上一篇<Spring学习之——手写Spring源码(V1.0)>中,我实现了一个Mini版本的Spring框架,在这几天,博主又看了不少关于Spring源码解析的视频,受益匪浅,也 ...

  8. Spring学习之旅(三)Spring工作原理初探

    详细的废话相信很多书籍视频资料都已经很多了,这里说几个小编个人认为对于理解Spring框架很重要的点.欢迎批评指正. 1)Spring的控制反转 先说说“依赖”,在面向对象程序设计中,类A中用到了类B ...

  9. 我的Spring学习记录(四)

    虽然Spring管理这我们的Bean很方便,但是,我们需要使用xml配置大量的Bean信息,告诉Spring我们要干嘛,这还是挺烦的,毕竟当我们的Bean随之增多的话,xml的各种配置会让人很头疼. ...

随机推荐

  1. Spring理解IOC,DI,AOP作用,概念,理解。

    IOC控制反转:创建实例对象的控制权从代码转换到Spring容器.实际就是在xml中配置.配置对象 实例化对象时,进行强转为自定义类型.默认返回类型是Object强类型. ApplicationCon ...

  2. LINUX capability概念及配置

    写的不错的一片博客,可以参考熟悉相关概念,主要将了linux系统的系统调用,以及用法这里就不重新造轮子了 https://www.cnblogs.com/iamfy/archive/2012/09/2 ...

  3. 【新手向】使用nodejs抓取百度贴吧内容

    参考教程:https://github.com/alsotang/node-lessons 1~5节 1. 通过superagent抓取页面内容 superagent .get('http://www ...

  4. 五分钟了解node,cnpm和yarn

    1.静态网页和动态网页 动态网页:数据可以进行交互,动态改变数据 2.node node是基于chrome的V8引擎的javascript的运行环境,node中的事件机制以及非阻塞式的I/O式模式,使 ...

  5. C++常见笔试题

    1.实现字符串转整数的函数:int atoi(const char *nptr) 2.实现数组折半查找:int BinarySearch(int a[],int len, int key) 3.实现字 ...

  6. [Jenkins]JDK版本过高导致的java.io.IOException: Remote call on xxxx failed

    ------------------------------------------------------ 如需转载,请注明出处. 文章链接:https://www.cnblogs.com/dzbl ...

  7. Java I/O : Java中的进制详解

    作者:李强强 上一篇,泥瓦匠基础地讲了下Java I/O : Bit Operation 位运算.这一讲,泥瓦匠带你走进Java中的进制详解. 一.引子 在Java世界里,99%的工作都是处理这高层. ...

  8. [CXF REST标准实战系列] 二、Spring4.0 整合 CXF3.0,实现测试接口

    Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章Points: 1.介绍RESTful架构 ...

  9. linux 命令 — xargs

    xargs xargs能接收stdin并将其转化为特定命令的命令行参数,构建单行命令的重要工具 command | xargs 指定分隔符 echo "splitXsplitXsplitXs ...

  10. 【Flask-RESTPlus系列】Part1:快速入门

    0x00 内容概览 Flask-RESTPlus安装 快速入门 初始化 一个最简单的API示例 资源路由 端点 参数解析 数据格式化 顺序保留 完整例子 0x01 Flask-RESTPlus安装 1 ...