重新学习之spring第一个程序,配置IOC容器
第一步:导入相关jar包(此范例导入的是spring3.2.4版本,spring2.5版本只需要导入spring核心包即可)

第二步:在项目的src下配置applicationContext.xml的配置文件
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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!--
lazy-init:true 懒加载,初始ioc化容器的时候,不建立对象
lazy-init:false(默认) 不懒加载,初始化ioc容器的时候,就讲applicationContext.XML中所有配置的类的对象创建,并建立关系
scope:prototype每次取对应id的类的对象,都新创建一个
scope:singleton(默认)类似于单例模式,只要容器初始化一个类对象,以后所有的取用,都是取这一个
--> <!-- 通过setter方法,依赖注入对象。控制反转 -->
<bean id="userDao" class="com.bjsxt.shang.dao.UserDao" lazy-init="default" scope="singleton"></bean>
<bean id="userAction" class="com.bjsxt.shang.action.UserAcion" lazy-init="default" scope="prototype">
<property name="userDao" ref="userDao"></property>
</bean> </beans>
第三步:编写测试类,获取applicationContext对象,即IOC容器对象
test类源码
package com.bjsxt.shangxiaofei; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bjsxt.shang.action.UserAcion;
import com.bjsxt.shang.dao.UserDao; public class Test {
public static void main(String[] args) { //官方推荐,建立xml文件,配置程序中对象,和建立对象间关系的是applicationContext.xml
//平民版的xml是 beans.xml controller.xml dao.xml service.xml
//获取解析,并依赖注入applicationContext.XML文件的ioc容器
ApplicationContext acIoc=new ClassPathXmlApplicationContext("applicationContext.xml");
//ApplicationContext ioc=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); //获取所有的ioc容器
//BeanFactory是所有Spring Ioc容器的父接口
//BeanFactory beanFactory=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); UserDao dao=(UserDao) acIoc.getBean("userDao");
UserDao dao1=(UserDao) acIoc.getBean("userDao");
//测试IOC容器中生成的对象是不是单例的。
System.out.println(dao==dao1); UserAcion userAction=(UserAcion) acIoc.getBean("userAction");
userAction.test();
} }
在applicationContext.xml中是将项目中对象进行管理。不需要手动去new对象,只要在xml中将对象之间的依赖关系配置清楚即可。所以该测试类,最后打印结果是执行了UserDao中的方法。
重新学习之spring第一个程序,配置IOC容器的更多相关文章
- spring——通过xml文件配置IOC容器
创建相关的类(这里是直接在之前类的基础上进行修改) package com.guan.dao; public interface Fruit { String getFruit(); } packag ...
- Spring IOC-基于XML配置的容器
Spring IOC-基于XML配置的容器 我们先分析一下AbstractXmlApplicationContext这个容器的加载过程. AbstractXmlApplicationContext的老 ...
- 转 Spring源码剖析——核心IOC容器原理
Spring源码剖析——核心IOC容器原理 2016年08月05日 15:06:16 阅读数:8312 标签: spring源码ioc编程bean 更多 个人分类: Java https://blog ...
- 【spring源码分析】IOC容器初始化(三)
前言:在[spring源码分析]IOC容器初始化(二)中已经得到了XML配置文件的Document实例,下面分析bean的注册过程. XmlBeanDefinitionReader#registerB ...
- 【spring源码分析】IOC容器初始化——查漏补缺(五)
前言:我们知道在Spring中经常使用配置文件的形式对进行属性的赋值,那配置文件的值是怎么赋值到属性上的呢,本文将对其进行分析. 首先了解一个类:PropertySourcesPlaceholderC ...
- 【spring源码分析】IOC容器初始化(二)
前言:在[spring源码分析]IOC容器初始化(一)文末中已经提出loadBeanDefinitions(DefaultListableBeanFactory)的重要性,本文将以此为切入点继续分析. ...
- 【spring源码分析】IOC容器初始化(七)
前言:在[spring源码分析]IOC容器初始化(六)中分析了从单例缓存中加载bean对象,由于篇幅原因其核心函数 FactoryBeanRegistrySupport#getObjectFromFa ...
- 【spring源码分析】IOC容器初始化(十)
前言:前文[spring源码分析]IOC容器初始化(九)中分析了AbstractAutowireCapableBeanFactory#createBeanInstance方法中通过工厂方法创建bean ...
- Spring源码分析专题 —— IOC容器启动过程(上篇)
声明 1.建议先阅读<Spring源码分析专题 -- 阅读指引> 2.强烈建议阅读过程中要参照调用过程图,每篇都有其对应的调用过程图 3.写文不易,转载请标明出处 前言 关于 IOC 容器 ...
随机推荐
- ubuntu16.04(liunx) 离线安装 xgboost (anaconda3,anaconda2共存)
服务器ubuntu 系统同时安装了 anaconda3,anaconda2 ,但服务器没有连接外网,所以 想在python3 环境下安装离线安装xgboost. 主要分2步: 0:进入py3环境 ( ...
- R语言统计词频 画词云
原始数据: 程序: #统计词频 library(wordcloud) # F:/master2017/ch4/weibo170.cut.txt text <- readLines("F ...
- 千亿级SaaS市场:企业级服务的必争之地
2015年企业级服务融资案例数量飙升,大额融资频现.不少企业纷纷涉足企业级服务市场,其中,以IM为主打的阿里钉钉,以企业CRM为主的纷享逍客高调进入人们的视野,以产品管理为核心.集成多种工具服务的iC ...
- springcloud17---zuul-reg-exp
package com.itmuch.cloud; import org.springframework.boot.SpringApplication; import org.springframew ...
- 前端面试题之 sum(2)(3) (链式调用,toString,柯里化,数组操作)
写一个函数让下面两个输出结果相同:console.log(sum(2)(3));console.log(sum(2,3)); var sum = (function() { var list = [] ...
- Git提交撤销
场景:本地修改代码后,执行commit提交,但此时想撤销该提交. 执行命令: git reflog 查看所有commit历史 git reset --hard commitid 将当前远程 ...
- AngularJs 表单提交按钮状态
表单属性: $invalid:未经过验证的表单,就是表单里面信息通过验证就为false,没有通过为true $valid:经过验证的表单,表单里信息验证通过为true,反之为false $dirty: ...
- atcoder ARC092 D - Two Sequences 二分 & 二进制
今天生日捏,嘻嘻~ 题意:给定A B数组长度为n 求所有 (1<=i,j <=n ) a[i]+b[j] 的异或和. n <=200000 ai bi <=228 这题比赛没 ...
- LightOJ 1030 Discovering Gold (期望)
https://vjudge.net/problem/LightOJ-1030 题意: 在一个1×N的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得到该格子的金币. 现在从1格子开始,每次 ...
- Gym 101334F Feel Good
http://codeforces.com/gym/101334 题意:给定一串数,求一个区间,使得该区间的所有数之和乘以该区间内最小的数的乘积最大. 思路:先预处理一下,计算出前缀和. 我们可以把每 ...