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接口和 ...
随机推荐
- OpenWRT平台搭建及简单应用[转帖]+华为HG255D编译实践(20190323更新)
转自:http://www.cnblogs.com/zmkeil/archive/2013/04/17/3027385.html对于HG255D参照这里:http://www.right.com.cn ...
- nginx日志 logrotate配置
nginx 日志 logrotate配置如下: /var/log/nginx/*.log { daily missingok rotate 20 compress delaycompress noti ...
- Filter过滤器 不登陆无法访问其他页面
package com.cscy.Filter; import java.io.IOException; import javax.servlet.Filter; import javax.servl ...
- ArcGIS按选定线分割面-案例教程
ArcGIS按选定线分割面-案例教程 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 功能 方法:高级编辑 实例: 分割前后 联系方式:谢老师,135-4855-43 ...
- django 设置session过期时间
session的超时时间设置settings中SESSION_COOKIE_AGE=60*30 30分钟.SESSION_EXPIRE_AT_BROWSER_CLOSE False:会话cookie可 ...
- ansible自动化
一,工具与环境介绍 1.1 ansible简介 批量管理服务器的工具 无需部署agent,通过ssh进行管理 流行的自动化运维工具:https://github.com/ansible/ansib ...
- vue mock
如果后端不肯来帮你 mock 数据的话,前端自己来 mock 也是很简单的.你可以使用mock server 或者使用 mockjs + rap 也是很方便的. 不久前出的 easy-mock也相当的 ...
- Java框架spring Boot学习笔记(七):@Configuration,@bean注解
@Configuration作用在类上,相当于一个xml文件 @bean作用于方法上,相当于xml配置中的<bean>标签 一个例子: 新建一个Springboot工程 新建一个User类 ...
- android sdk更新后出现please update ADT to the latest version的解决方法
参考:https://www.cnblogs.com/wicub/p/3613353.html 把 1.修改android-sdk-windows/tools/lib下的plugin.pro文件,用记 ...
- java中判断是否是一个无用的类
类需要同时满足以下三个条件才能算是无用的类: 1.该类所有的实例都已经被回收,也就是java堆中不存在类的任何实例 2.加载该类的ClassLoader已经被回收 3.该类对应的java.lang.C ...