Spring基本功能-IOC
一、SpringIOC
Spring的控制反转:把对象的创建,初始化,销毁的过程交给SpringIOC容器来做,由Spring容器控制对象的生命周期。
1.1 启动Spring容器的方式:
(1)加载classpath下的spring配置文件。其中xml可以是全路径,也可以是classpath的书写方式,该方式下Spring的配置文件必须放置于classpath路径下。
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:jyk.xml");
(2)加载文件系统下的配置文件。其中xml是存放于某个具体文件目录下的Spring配置文件。
ApplicationContext ac = new FileSystemXmlApplicationContext("d:\\jyk.xml");
1.2 从Spring容器中提取对象的方式。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
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
"> <bean id="person" class="com.jyk.spring.simpletest.Person">
</bean> </beans>
package com.jyk.spring.simpletest;
public class Person {
public Person() {
System.out.println("fuck...............");
}
public void printInfo(){
System.out.println("hello..............");
}
}
package com.jyk.spring.simpletest; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) {
ApplicationContext ac = new
ClassPathXmlApplicationContext("classpath:jyk.xml");
//根据beanid获取相应的对象并强制转换
Person p = (Person)ac.getBean("person");
p.printInfo();
}
}
二、Spring对象的常用创建方式
2.1 无参构造函数
<bean id="person" class="com.jyk.spring.simpletest.Person"></bean>
2.2 有参构造函数
<!-- 按照方法参数的顺序赋值 -->
<bean id="person" class="com.jyk.spring.simpletest.Person">
<constructor-arg index="0" value="tom"></constructor-arg>
<constructor-arg index="1" value="12"></constructor-arg>
</bean> <!-- 按照方法参数的类型赋值 -->
<bean id="person" class="com.jyk.spring.simpletest.Person">
<constructor-arg type="java.lang.String" value="tom"></constructor-arg>
<constructor-arg type="java.lang.Double" value="12"></constructor-arg>
</bean> <!-- 按照方法参数的名称赋值 -->
<bean id="person" class="com.jyk.spring.simpletest.Person">
<constructor-arg name="name" value="tom"></constructor-arg>
<constructor-arg name="age" value="12"></constructor-arg>
</bean>
2.3 静态工厂
<bean id="personFactory" class="com.jyk.spring.simpletest.PersonFactory" factory-method="createPerson"> </bean>
package com.jyk.spring.simpletest;
public class PersonFactory {
public static Person createPerson(){
return new Person();
}
}
三、Spring对象的scope
3.1 singleton(默认值)
在Spring容器中一个Bean定义只有一个共享的对象实例。在一个普通的只有beanid和class配置的bean定义中,默认情况下是Spring容器初始化时便初始化该bean。
但我们可以在bean节点中添加lazy-init="true"来延迟加载bean。这样只有在第一次获取bean时才会有初始化该bean。
<bean id="person" class="com.jyk.spring.simpletest.Person" lazy-init="true"></bean>
如果想对所有的bean设置延迟初始化,可以在beans节点设置该属性值,如下
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
" default-lazy-init="true">
3.2 prototype
当一个bean的scope为prototype时,该bean允许被多次创建(每使用一次就实例化一次),Spring无法对prototype的bean的生命周期负责,因此以为着持有prototype的对象并释放的责任,都需由客户端负责。
<bean id="person" class="com.jyk.spring.simpletest.Person" scope="prototype"></bean>
3.3 request
每次http请求都会有各自的bean实例创建,即一个bean实例对应一次http请求,该作用域仅在基于web的spring applicationcontext场景下有效。
3.4 session
一个bean实例对应一个http session,该作用域仅在基于web的spring applicationcontext场景下有效。
3.5 global session
四、Spring对象的初始化与销毁
Spring默认在启动时对所有singleton bean进行实例化,即ApplicationContext提前地创建并配置好所有singleton bean。提前实例化意味着对启动的性能有所影响,但长远来看是好事,因为一旦某个对象的创建出现了问题,在初始化时便能及时发现并修复,而不是等到调用该对象时才报错(lazy-init="false"的情况)。
Spring初始化和销毁bean时,有时需要人工介入处理一些逻辑,Spring提供了初始化和销毁bean时的自定义方法配置,init-method和destory-method,当容器初始化该bean时调用init方法,当该bean对象中容器中删除时调用destroy方法。
<bean id="person" class="com.jyk.spring.simpletest.Person" init-method="init" destroy-method="destroy"></bean>
Spring基本功能-IOC的更多相关文章
- Spring框架的IOC核心功能快速入门
2. 步骤一:下载Spring框架的开发包 * 官网:http://spring.io/ * 下载地址:http://repo.springsource.org/libs-release-local/ ...
- Spring框架及IOC容器
Spring是一个非常活跃的开源框架, 它是一个基于IOC和AOP来构架多层JavaEE系统的框架,它的主要目地是简化企业开发.Spring以一种非侵入式的方式来管理你的代码, Spring提倡”最少 ...
- Spring学习笔记IOC与AOP实例
Spring框架核心由两部分组成: 第一部分是反向控制(IOC),也叫依赖注入(DI); 控制反转(依赖注入)的主要内容是指:只描述程序中对象的被创建方式但不显示的创建对象.在以XML语言描述的配置文 ...
- Spring框架(3)---IOC装配Bean(注解方式)
IOC装配Bean(注解方式) 上面一遍文章讲了通过xml来装配Bean,那么这篇来讲注解方式来讲装配Bean对象 注解方式需要在原先的基础上重新配置环境: (1)Component标签举例 1:导入 ...
- Spring框架之IOC(控制反转)
[TOC] 第一章Spring框架简介 IOC(控制反转)和AOP(面向方面编程)作为Spring框架的两个核心,很好地实现了解耦合.所以,简单来说,Spring是一个轻量级的控制反转(IoC)和面向 ...
- 一) Spring 介绍、IOC控制反转思想与DI依赖注入
一.spring介绍1.IOC反转控制思想(Inversion of Control)与DI依赖注入(Dependency Injection)2.AOP面向切面的编程思想与动态代理3.作用:项目的粘 ...
- Spring系列之IOC的原理及手动实现
目录 Spring系列之IOC的原理及手动实现 Spring系列之DI的原理及手动实现 导语 Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架.也是几乎所有J ...
- Spring框架[一]——spring概念和ioc入门(ioc操作xml配置文件)
Spring概念 spring是开源的轻量级框架(即不需要依赖其他东西,可用直接使用) spring核心主要两部分 aop:面向切面编程,扩展功能不是修改源代码来实现: ioc:控制反转,比如:有一个 ...
- [02] Spring主要功能模块概述
1.Spring主要功能模块 1.1 Core Container Spring的核心容器模块,其中包括: Beans Core Context SpEL Beans和Core模块,是框架的基础部 ...
随机推荐
- Deep learning for Human Strategic Behaviour
没看,但是论文UI和视频做的很好. 论文地址:https://papers.nips.cc/paper/6509-deep-learning-for-predicting-human-strategi ...
- java----IO和NIO的区别
概念:NIO即New IO,这个库是在JDK1.4中才引入的.NIO和IO有相同的作用和目的,但实现方式不同,NIO主要用到的是块,所以NIO的效率要比IO高很多.在Java API中提供了两套NIO ...
- linux -- Ubuntu14.04及之后版本重启网卡不生效
Ubuntu14.04修改配置,重启网卡没有生效,出现如下问题: service networking restart //重启网络服务 stop: Job failed while stopping ...
- php -- 判断文件是否存在
file_exists is_file is_dir 基本上,PHP的 file_exists = is_dir + is_file 写程序验证一下: 分别执行1000次,记录所需时间. ------ ...
- 基于SSH框架实际开发时遇到的问题及解决办法
1. 发现通过注解注入bean不起作用(对应的.java文件上没有'S'标记) 需要在pring .xml配置文件中加 <!-- 使用自动注解就必须配置加入自动扫描加载容器的包 --> & ...
- nginx php文件上传的大小配置问题
- Objective-C Runtime初探:self super
题目 上题目,已知A是爷爷,B是爸爸,C是孙子. @interface A : NSObject - (void)f; @end @interface B : A - (void)f; - (void ...
- python3----练习题(....)
# 保存文件(文本,图片,视频...) 1 def save_file(): url = 'http://css.8684.cn/citys/images/line/45.jpg' root = r' ...
- keyword static
1. 不能通过类名来调用类的非静态成员函数 2. 类的对象可以使用静态成员函数和非静态成员函数 3. 静态成员函数中不能引用非静态成员 因为静态成员函数属于整个类, 在类的实例化对象之前就已经分配了空 ...
- ubuntu 12.04中环境变量设置
Persistent environment variables So far we've only discussed ways set an environment variable value ...