spring深入学习(一)-----IOC容器
spring对于java程序员来说,重要性不可言喻,可以想象下如果没有他,我们要多做多少工作,下面一个系列来介绍下spring(5.x版本)。
spring模块
IOC概念
spring中最重要的两个部分就是ioc和aop,先来介绍ioc。ioc也叫控制反转,其实说白了,就是容器帮你去创建对象(前提是你交给对象管理),并且创造出的对象默认是单例的,也可以非单例,并且自动帮你实现注入功能,很屌。
项目demo
先来建个demo项目,本人使用的是idea,新建maven项目,
1、设置编码
为了防止项目中出现乱码,统一使用utf-8编码格式。
2、idea中设置maven
首先在maven的setting.xml配置maven镜像,在mirrors节点下配置阿里云镜像,国内的下载速度快
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
然后在idea中将maven的设置改成自己本地,不然会取默认的配置。
3、新建maven项目
4、添加依赖等。pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.ty</groupId>
<artifactId>SpringStudy</artifactId>
<version>1.0-SNAPSHOT</version>
<name>spring4.x study</name>
<description>it's only for studying</description> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> </dependencies>
</project>
5、新建两个类
package com.ty.beans; public class School { private String address; private Student student; public School(Student student) {
this.student = student;
}
}
package com.ty.beans; public class Student { private int age; private String name; public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}
applicationContext.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="student" class="com.ty.beans.Student">
<!-- property代表的是set方法注入-->
<property name="age" value="27"></property>
<property name="name" value="马云"></property>
</bean> <bean id="school" class="com.ty.beans.School">
<!-- constructor代表的是构造器注入-->
<constructor-arg ref="student"></constructor-arg>
</bean>
</beans>
测试类:
package com.ty.beans; import javafx.application.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @ContextConfiguration(locations = { "classpath:applicationContext.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class IOCTest { @Test
public void testIOC() {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
System.out.println(context.getBean("student"));
System.out.println(context.getBean("school"));
}
}
运行结果:
com.ty.beans.Student@65d6b83b
com.ty.beans.School@d706f19
那么为啥spring可以做到这点呢?往下看:
相关java基础回顾
本人在说jvm模块的类加载的时候,说到class文件流经过验证后进入方法区,并且会创建一个class对象,而java中的反射可以使用class对象进行操作,如构造函数、属性和方法等等。
1、反射
package com.ty.reflect; public class Car { private String brand; private String color; private int maxSpeed; public Car() { } public Car(String brand, String color, int maxSpeed) {
this.brand = brand;
this.color = color;
this.maxSpeed = maxSpeed;
} public void introduce() {
System.out.println("This is a test" + ";汽车品牌为" + this.getBrand());
} public String getBrand() {
return brand;
} public void setBrand(String brand) {
this.brand = brand;
} public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
} public int getMaxSpeed() {
return maxSpeed;
} public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
}
package com.ty.reflect; import java.lang.reflect.Constructor;
import java.lang.reflect.Method; public class ReflectTest { public static Car init() throws Throwable {
Class clazz = Car.class; //获取car的无参构造器
Constructor<?> cons = clazz.getDeclaredConstructor();
//通过无参构造器实例化对象
Car car = (Car) cons.newInstance(); Method setBrand = clazz.getMethod("setBrand", String.class);
//实例方法的第一个隐式参数就是当前对象this
setBrand.invoke(car, "大众");
return car;
} public static void main(String[] args) throws Throwable {
Car car = init();
car.introduce();
}
}
运行结果:
This is a test;汽车品牌为大众
注:如果某个类中存在private属性或是方法,反射可以直接访问,因此反射也从一定程度上打破了封装。
field.setAccessible(true);
method.setAccessible(true);
2、ClassLoader方法
BeanFactory
一般将BeanFactory称为ioc容器,将ApplicationContext称为应用上下文。
ApplicationContext
1、ApplicationContext
2、WebApplicationContext
专门为web应用所准备,允许从相对于web根目录的路径中装载配置文件完成初始化工作。并且从WebApplicationContext中可以获得ServletContext(web容器上下文)的引用,整个web应用上下文对象将作为属性放置到ServletContext中,保证web应用环境可以访问Spring应用上下文。
ServletContext:javaee标准规定了,servlet容器需要在应用项目启动时,给应用项目初始化一个ServletContext作为公共环境容器存放公共信息。ServletContext中的信息都是由容器提供的。
在非web应用的环境下,bean只有singleton和prototype两种作用域。WebApplicationContext为bean添加三个新的作用域:request、session和global session。
WebApplicationContext可以通过WebApplicationContextUtils.getWebApplicationContext(ServletContext sc)获取。
WebApplicationContextUtils.getWebApplicationContext(ServletContext sc)
WebApplicationContext需要ServletContext实例,也就是说依赖于web容器,所以我们在web.xml中需要配置web容器监听器(ServletContextListener)。例如:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"> <!-- 指定 spring 配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <!-- web 容器监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> </web-app>
ContextLoaderListener通过contextConfigLocation中配置的xml信息来获取Spring的相关信息。另外WebApplicationContext必须要使用日志功能,所以可以将log4j.properties文件配置在web.xml中,例如:
<!-- 指定 log4j 配置文件-->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<!-- Log4j 监听器-->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
spring深入学习(一)-----IOC容器的更多相关文章
- Spring框架学习之IOC(二)
Spring框架学习之IOC(二) 接着上一篇的内容,下面开始IOC基于注解装配相关的内容 在 classpath 中扫描组件 <context:component-scan> 特定组件包 ...
- Spring框架学习之IOC(一)
Spring框架学习之IOC(一) 先前粗浅地学过Spring框架,但当时忙于考试及后期实习未将其记录,于是趁着最近还有几天的空闲时间,将其稍微整理一下,以备后期查看. Spring相关知识 spri ...
- Spring核心原理之IoC容器初体验(2)
本文节选自<Spring 5核心原理> 1 IoC与DI基本概念 IoC(Inversion of Control,控制反转)就是把原来代码里需要实现的对象创建.依赖,反转给容器来帮忙实现 ...
- Spring系列14:IoC容器的扩展点
Spring系列14:IoC容器的扩展点 回顾 知识需要成体系地学习,本系列文章前后有关联,建议按照顺序阅读.上一篇我们详细介绍了Spring Bean的生命周期和丰富的扩展点,没有阅读的强烈建议先阅 ...
- spring之:XmlWebApplicationContext作为Spring Web应用的IoC容器,实例化和加载Bean的过程
它既是 DispatcherServlet 的 (WebApplicationContext)默认策略,又是 ContextLoaderListener 创建 root WebApplicationC ...
- Spring源码解析-ioc容器的设计
Spring源码解析-ioc容器的设计 1 IoC容器系列的设计:BeanFactory和ApplicatioContext 在Spring容器中,主要分为两个主要的容器系列,一个是实现BeanFac ...
- [原创]java WEB学习笔记101:Spring学习---Spring Bean配置:IOC容器中bean的声明周期,Bean 后置处理器
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring源码学习之IOC容器实现原理(一)-DefaultListableBeanFactory
从这个继承体系结构图来看,我们可以发现DefaultListableBeanFactory是第一个非抽象类,非接口类.实际IOC容器.所以这篇博客以DefaultListableBeanFactory ...
- Spring.net(二)----初探IOC容器
我在上一篇关于Spring.net的文章“Spring.NET框架简介及模块说明 ”中很详细的介绍了,本文就不旧话从提.我门就直奔主题吧. 1.首先了解两个接口. IObjectFactory接口和 ...
随机推荐
- UE4 Virtual Reality Input输入配置表导入
[/Script/Engine.InputSettings] AxisConfig=(AxisKeyName="OculusTouch_Right_FaceButton2",Axi ...
- C# 异步示例代码
在 使用BackgroundWorker组件 一文中,阐述了在Winform编程中,使用BackgroundWorker组件实现异步调用,本文主要讲述利用委托实现异步. 以下描述摘抄于MSDN: 异步 ...
- 左手是“Python”的身体,右手是“R”的灵魂,你爱哪个?
来源商业新知网,原标题:你爱 “Python”的身体,还是“R”的灵魂? 数据科学界有三大宝: Python.SAS和R,不过像SAS这种高端物种,不是我们这些平民能供养得起的啊. 根据 IEEE S ...
- web应用程序+HTTP协议
标签(空格分隔): Django web应用程序案例: 如果我们想通过自己电脑访问京东,就是一个网络编程(因为京东的服务部署在京东,通过自己的电脑浏览器传输到京东服务就是网络编程):只要涉及到网络编程 ...
- iframe登录超时跳转登录页面
1.可以在登录页面加jQuery验证 $(function () { //判断一下当前是不是做顶层,如果不是,则做一下顶层页面重定向 if (window != top) { top.location ...
- maven工程 添加本地jar依赖
和第三方平台对接的时候要用到对方提供的一个jar包,jar包怎么直接添加到pom文件的依赖中呢? jar包一般都是公共的,要上传到私服仓库.我们都是直接登录私服,操作仓库. 登录私服可以在项目的pom ...
- java第三章多态
多态: 多态不仅可以减少代码量,还可以提高代码的扩展和可维护性 (通过一个方法可以对所有所需方法一个运用)多态具体表现多种形态能力的特征,同一个实现接口使用不同实例而执行不同的操作 实现多态的三个条件 ...
- 第一次博客作业 <西北师范大学| 周安伟>
1.助教博客链接:https://home.cnblogs.com/u/zaw-315/ 2.本周点评的作业数:3份,有留言互动. 3.本周点评有困难的地方: https://www.cnblogs ...
- Leetcode1000 合并石头的最低成本 区间DP
有 N 堆石头排成一排,第 i 堆中有 stones[i] 块石头. 每次移动(move)需要将连续的 K 堆石头合并为一堆,而这个移动的成本为这 K 堆石头的总数. 找出把所有石头合并成一堆的最低成 ...
- 大数据实操3 - hadoop集群添加新节点
hadoop集群支持动态扩展,不需要停止原有集群节点就可以实现新节点的加入. 我是使用docker搭建的进群环境,制作了镜像文件,这里以我的工作基础为例子介绍集群中添加集群的方法 一.制作一个新节点 ...