1、spring框架什么时候被加载?

(1)ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml")执行的时候,

spring容器对象被创建。

(2)同时,applicationContext.xml里面配置的对象被创建到内存中。(内存模型类似于HashMap)。容器利用反射机制创建bean的实例。如下图

2、spring中配置的bean怎样被创建?

3、bean与bean之间的关系怎样维护?

代码结构图:

applicationContext代码

 <?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 class="com.spring.service.SpringService" id="springService">
<property name="userName" value="lvyf"/>
<property name="tService" ref="testService"></property>
</bean> <bean class="com.spring.service.TestService" id="testService">
<property name="pwd" value="123456"/>
</bean>
</beans>

SpringService代码

 package com.spring.service;

 public class SpringService {
private String userName; private TestService tService; public TestService gettService() {
return tService;
} public void settService(TestService tService) {
this.tService = tService;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public void sayHello() {
System.out.println("hello world " + userName);
tService.getPasswd();
} }

TestService代码

 package com.spring.service;

 public class TestService {
private String pwd; public String getPwd() {
return pwd;
} public void setPwd(String pwd) {
this.pwd = pwd;
} public void getPasswd(){
System.out.println(pwd);
}
}

Run代码

 package com.run;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.service.SpringService; public class Run { public static void testRun(){
//1、创建spring的IOC容器对象
@SuppressWarnings("resource")
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-bean.xml");
//2、从IOC容器获取spring bean实例
//SpringService sService = (SpringService)ac.getBean("springService");
SpringService sService = (SpringService)ac.getBean(SpringService.class);
//3、调用sayHello方法
sService.sayHello();
} public static void main(String[] args) {
testRun();
}
}

spring原理的更多相关文章

  1. spring原理案例-基本项目搭建 01 spring framework 下载 官网下载spring jar包

    下载spring http://spring.io/ 最重要是在特征下面的这段话,需要注意: All avaible features and modules are described in the ...

  2. spring原理案例-基本项目搭建 02 spring jar包详解 spring jar包的用途

    Spring4 Jar包详解 SpringJava Spring AOP: Spring的面向切面编程,提供AOP(面向切面编程)的实现 Spring Aspects: Spring提供的对Aspec ...

  3. spring原理案例-基本项目搭建 03 创建工程运行测试 spring ioc原理实例示例

    下面开始项目的搭建 使用 Java EE - Eclipse 新建一 Dynamic Web Project Target Runtime 选 Apache Tomcat 7.0(不要选 Apache ...

  4. spring 原理

    1.spring原理 内部最核心的就是IOC了,动态注入,让一个对象的创建不用new了,可以自动的生产,这其实就是利用java里的反射,反射其实就是在运行时动态的去创建.调用对象,Spring就是在运 ...

  5. 学习认识Spring原理

    学习认识Spring原理 Spring 是一种业务层框架.搭建Spring框架需要Spring开发包和commons-logging包.Spring的核心思想是控制反转也称依赖注入(创建者--(实例) ...

  6. 7.1 Spring原理

    7.1 Spring原理 一.spring是什么?(IOC.AOP.MVC) Spring是一个基于IOC和AOP的结构J2EE系统的框架 , 1.1 IOC 控制反转 是Spring的基础,Inve ...

  7. Spring原理系列一:Spring Bean的生命周期

    一.前言 在日常开发中,spring极大地简化了我们日常的开发工作.spring为我们管理好bean, 我们拿来就用.但是我们不应该只停留在使用层面,深究spring内部的原理,才能在使用时融汇贯通. ...

  8. MyBatis整合Spring原理分析

    目录 MyBatis整合Spring原理分析 MapperScan的秘密 简单总结 假如不结合Spring框架,我们使用MyBatis时的一个典型使用方式如下: public class UserDa ...

  9. 转载:把你的精力专注在java,jvm原理,spring原理,mysql锁,事务,多线程,大并发,分布式架构,微服务,以及相关的项目管理等等,这样你的核心竞争力才会越来越高

    https://developer.51cto.com/art/202001/608984.htm 把你的精力专注在java,jvm原理,spring原理,mysql锁,事务,多线程,大并发,分布式架 ...

  10. 探究Spring原理

    探究Spring原理 探究IoC原理 首先我们大致了解一下ApplicationContext的加载流程: 我们可以看到,整个过程极为复杂,一句话肯定是无法解释的,所以我们就从ApplicationC ...

随机推荐

  1. JSTL(JSP Standard Tag Library)读书笔记

    分类                                       Preifx                                          范例 核心标签库--- ...

  2. IT男的”幸福”生活

    IT男的”幸福”生活 IT男的”幸福”生活"续1 IT男的”幸福”生活"续2  IT男的”幸福”生活"续3  IT男的”幸福”生活"续4  IT男的”幸福”生活 ...

  3. 云计算之路-阿里云上:2014年6月11日17点遇到的CPU 100%状况

    今天下午17:00-17:05之间,在请求量没有明显变化的情况下,SLB中的1台云服务器的CPU突然串到100%(当时SLB中一共有3台云服务器),见下图: 造成的直接后果是请求执行时间变得超长,最长 ...

  4. [c#基础]集合foreach的必要条件和自定义集合

    引言 最近翻看了之前的学习笔记,看到foreach,记得当时老师讲的时候,有点犯浑,不是很明白,这好比,上小学时,你不会乘法口诀,但是随着时间的增长,你不自觉的都会了,也悟出个小道理,有些东西,你当时 ...

  5. WCF 入门 (18)

    前言 感冒了呀...but 不忌油炸,不忌辛辣,o(∩_∩)o . 第18集 WCF服务应该抛出fault 异常 Throwing fault exceptions from a WCF servic ...

  6. Bootstrap3.0学习第二十五轮(JavaScript插件——折叠)

    详情请查看http://aehyok.com/Blog/Detail/31.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...

  7. 软工实践练习——使用Git进行代码管理

    GITHUB上的预备活动: 注册 创建小组Organization,邀请组员进来 将代码库fork到小组Organization底下 下载并使用GIT: Git的安装 使用Git进行代码管理 1.从百 ...

  8. ORA-12737: Instant Client Light: unsupported server character set CHS16GBK/ZHS16GBK解决方案

    二.Navicat for Oracle的配置 1.启动该工具,出现如下的开始界面,单击“连接”选项,进行连接数据库,如图所示: 6.在“新建连接”对话框中,输入任意的连接名,选择默认的连接类型,输入 ...

  9. python 学习笔记3(循环方式;list初始化;循环对象/生成器/表推导;函数对象;异常处理)

    ### Python的强大很大一部分原因在于,它提供有很多已经写好的,可以现成用的对象 16. 循环方式笔记: 1)range(0, 8, 2)   #(上限,下限,步长)  可以实现对元素或者下标的 ...

  10. 【poj2187】 Beauty Contest

    http://poj.org/problem?id=2187 (题目链接) 题意 求点集上两点间最长距离 Solution 凸包+旋转卡壳. 旋转卡壳是看起来很难,但是很好意会也很好实现的算法,但是要 ...