Eclipse中使用Spring IOC容器的具体方法
1、通过IOC容器创建对象,并为属性赋值
在IOC容器本身对象创建时(xml文件加载时),会将配置文件中配置好的bean先创建出来,按照xml文件中配置的先后顺序创建
<bean id="user1" class="com.neuedu.springfirst.bean.User" >
<property name="username" value="张三"></property>
<property name="password" value="123456"></property>
<property name="email" value="12345@qq.com"></property>
</bean>
测试方法:
@Test
public void test01() {
ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
Object user = ioc.getBean("user1");
System.out.println(user);
}
2、根据bean的类型从IOC容器中获取bean的实例★【要求:IOC容器同种类型只存在一个】
@Test
public void test02() throws Exception {
ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
User user = ioc.getBean(User.class);
System.out.println(user);
}
3-4、通过构造器为bean的属性赋值 ,通过index属性指定参数的位置
<bean id="book1" class="com.neuedu.springfirst.bean.Book">
<constructor-arg index="0" value="二月"></constructor-arg>
<constructor-arg index="1" value="张三"></constructor-arg>
</bean>
测试代码:
1 @Test
public void test03() throws Exception {
ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
Object book = ioc.getBean("book1");
System.out.println(book);
}
5、通过类型不同区分重载的构造器
<bean id="book2" class="com.neuedu.springfirst.bean.Book">
<constructor-arg type="String" value="二月"></constructor-arg>
<constructor-arg type="String" value="邹梦洁"></constructor-arg>
</bean>
<bean id="book3" class="com.neuedu.springfirst.bean.Book">
<constructor-arg type="String" value="阿勒股"></constructor-arg>
<constructor-arg type="Double" value="123"></constructor-arg>
</bean>
测试代码:
@Test
public void test05() throws Exception {
ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
Object book = ioc.getBean("book2");
System.out.println(book);
}
6、通过p名称空间为bean赋值
<bean id="book4" class="com.neuedu.springfirst.bean.Book"
p:bookName="阿克"
p:author="张三" />
测试代码:
@Test
public void test06() throws Exception {
ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
Object book = ioc.getBean("book4");
System.out.println(book);
}
7、测试使用null值
<bean id="book00" class="com.neuedu.springfirst.bean.Book">
<property name="bookName"><null/></property>
</bean>
测试代码:
@Test
public void test07() throws Exception {
ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
Object book = ioc.getBean("book00");
System.out.println(book);
}
8、引用其他bean
1 <bean id="bookshop" class="com.neuedu.springfirst.bean.BookShop">
<property name="kind" value="小说"></property>
<property name="book" ref="book1"></property>
</bean>
测试代码:
@Test
public void test08() throws Exception {
ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
Object bookshop = ioc.getBean("bookshop");
System.out.println(bookshop);
}
9、引用内部bean
<bean id="bookshop1" class="com.neuedu.springfirst.bean.BookShop">
<property name="kind" value="小说"></property>
<property name="book">
<bean class="com.neuedu.springfirst.bean.Book">
<property name="bookName" value="私发给"></property>
<property name="author" value="邹梦洁"></property>
</bean>
</property>
</bean>
测试代码:
@Test
public void test09() throws Exception {
ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
Object bookshop = ioc.getBean("bookshop1");
System.out.println(bookshop);
}
10、使用List类型的集合属性
<bean id="bookshop2" class="com.neuedu.springfirst.bean.BookShop">
<property name="kind" value="小说"></property>
<property name="bookList">
<list>
<ref bean="book1" />
<ref bean="book2" />
<ref bean="book3" />
</list>
</property>
</bean>
测试代码:
@Test
public void test10() throws Exception {
ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
BookShop bookshop = (BookShop) ioc.getBean("bookshop2");
for (Book book : bookshop.getBookList()) {
System.out.println(book);
}
}
11、使用Map类型的集合属性
1 <bean id="bookshop3" class="com.neuedu.springfirst.bean.BookShop">
<property name="kind" value="小说"></property>
<property name="bookMap">
<map>
<entry>
<key>
<value>book1</value>
</key>
<ref bean="book1" />
</entry>
<entry>
<key>
<value>book2</value>
</key>
<ref bean="book2" />
</entry>
<entry>
<key>
<value>book3</value>
</key>
<ref bean="book3" />
</entry>
</map>
</property>
</bean>
测试代码:
@Test
public void test11() throws Exception {
ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
BookShop bookshop = (BookShop) ioc.getBean("bookshop3");
Map<String, Book> bookMap = bookshop.getBookMap();
Set<String> nameSet=bookMap.keySet();
for (String name : nameSet) {
System.out.println(bookshop.getBookMap().get(name));
}
}
12、使用prop子元素为Properties类型的属性赋值
<bean id="bookshop4" class="com.neuedu.springfirst.bean.BookShop">
<property name="kind" value="历史"></property>
<property name="prop">
<props>
<prop key="name1">book1</prop>
<prop key="name2">book2</prop>
<prop key="name3">book3</prop>
</props>
</property>
</bean>
测试代码:
@Test
public void test12() throws Exception {
ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
BookShop bookshop = (BookShop) ioc.getBean("bookshop4");
Properties prop=bookshop.getProp();
System.out.println(prop.getProperty("name1"));
}
14、给bean的级联属性赋值
<bean id="book5" class="com.neuedu.springfirst.bean.Book"></bean>
<bean id="bookshop5" class="com.neuedu.springfirst.bean.BookShop">
<property name="book" ref="book5"></property>
<property name="book.bookName" value="熊清华"></property>
<property name="book.author" value="爱疯了"></property>
<property name="book.price" value="45"></property>
</bean>
测试代码:
@Test
public void test14() throws Exception {
ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
BookShop bookshop = (BookShop) ioc.getBean("bookshop5");
System.out.println(bookshop.getBook());
}
15、配置通过静态工厂方法创建的bean[通过静态方法提供实例对象,工厂类本身不需要实例化!
<bean id="book6" class="com.neuedu.springfirst.bean.StaticFactory" factory-method="getBook">
<constructor-arg value="book02"/>
</bean>
测试代码:
@Test
public void test15() throws Exception {
ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
Object book = ioc.getBean("book6");
System.out.println(book);
}
16、配置通过实例工厂方法创建的bean[通过实例方法提供实例对象,工厂类本身需要先创建对象!
<bean id="instanceFactory" class="com.neuedu.springfirst.bean.InstanceFactory"></bean>
<bean id="book7" class="com.neuedu.springfirst.bean.Book" factory-bean="instanceFactory" factory-method="getBook">
<constructor-arg value="book01"/>
</bean>
测试代码:
@Test
public void test16() throws Exception {
ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
Object book = ioc.getBean("book7");
System.out.println(book);
}
18、通过继承实现bean配置信息的重用
<bean id="user" class="com.neuedu.springfirst.bean.User">
<property name="id" value="1"></property>
<property name="username" value="杨涛"></property>
<property name="password" value="123456"></property>
<property name="email" value="12345@qq.com"></property>
</bean>
<bean id="user2" class="com.neuedu.springfirst.bean.User" parent="user">
<property name="password" value="654321"></property>
<property name="email" value="54321@qq.com"></property>
</bean>
测试代码:
1 @Test
public void test18() throws Exception {
ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
Object user = ioc.getBean("user2");
System.out.println(user);
}
19、通过abstract属性创建一个模板bean
1 <bean id="userTemplate" class="com.neuedu.springfirst.bean.User" abstract="true">
<property name="id" value="1"></property>
<property name="username" value="杨涛"></property>
<property name="password" value="123456"></property>
<property name="email" value="12345@qq.com"></property>
</bean>
<bean id="user3" class="com.neuedu.springfirst.bean.User" parent="userTemplate">
</bean>
测试代码:
@Test
public void test19() throws Exception {
ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
//userTemplate为抽象bean,只能被继承,不能被创建
//Object user = ioc.getBean("userTemplate");
Object user = ioc.getBean("user3");
System.out.println(user);
}
20、bean之间的依赖 depends-on="order"被依赖的对象会先创建
<bean id="user4" class="com.neuedu.springfirst.bean.User" depends-on="book8"></bean>
<bean id="book8" class="com.neuedu.springfirst.bean.Book"></bean>
测试代码:
@Test
public void test20() throws Exception {
ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
//执行test20测试方法前,需先将ApplicationContext.xml文件中之前的代码全部删除
//由于之前创建的对象会显示创建信息,
}
21、分别创建单实例和多实例的bean
单实例:
<bean id="user1" class="com.neuedu.spring.bean.User" scope="singleton">
<property name="id" value="1"></property>
<property name="username" value="张三"></property>
<property name="password" value="123456"></property>
<property name="email" value="123456@qq.com"></property>
</bean>
测试代码:
1 @Test
public void test21() throws Exception {
ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
System.out.println("=========");
User user1 = ioc.getBean(User.class);
User user2 = ioc.getBean(User.class);
System.out.println(user1==user2);
System.out.println(user1);
System.out.println(user2);
}
测试结果:是同一个对象,bean对象的创建默认是在ApplicationContext.xml文件加载时创建

多实例:
<bean id="user1" class="com.neuedu.spring.bean.User" scope="prototype">
<property name="id" value="1"></property>
<property name="username" value="张三"></property>
<property name="password" value="123456"></property>
<property name="email" value="123456@qq.com"></property>
</bean>
测试代码同上:
测试结果:不是一个对象,并且scope属性值为prototype的bean对象,将在获取bean对象的时候创建

注意:
①IOC容器本身对象创建时,会将配置文件中配置好的bean先创建出来
②默认是单实例的,只创建bean的一个对象
③如果设置bean的scope属性为prototype,那么创建bean的对象就是多实例的,在获取的时候创建,每次获取对象都会创建新的
④.从IOC容器中获取对象
①根据bean的id获取
②根据bean的类型获取:要求容器中指定类型的bean是唯一的
22、创建带有生命周期方法的bean
先创建含有生命周期相关的方法的类
public class LifeObject {
public LifeObject(){
System.out.println("LifeObject对象创建了");
}
public void initMethod(){
System.out.println("init方法执行了");
}
public void destoryMethod(){
System.out.println("destory方法执行了");
}
}
配置生命周期相关方法
<bean id="lifeObject" class="com.neuedu.spring.bean.LifeObject" init-method="initMethod" destroy-method="destoryMethod">
</bean>
测试代码:
@Test
public void test22() throws Exception {
ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
Object object = ioc.getBean("lifeObject");
System.out.println(object);
ConfigurableApplicationContext ioc1=(ConfigurableApplicationContext) ioc;
ioc1.close();
}
测试结果:

22[补充]:测试bean的后置处理器
bean的后置处理器将对所有在.xml文件加载时创建的bean对象执行处理器中的方法,bean的后置处理器实现了BeanPostProcessor接口,
可重写接口中的postProcessBeforeInitialization方法和postProcessAfterInitialization方法,分别在bean对象初始化之前和之后进行相应的操作
注:若bean对象scope属性值为prototype时,表明该bean为多实例,bean对象在获取对象时才创建,因此后置处理器不起作用
<bean id="postProcessor" class="com.neuedu.spring.bean.MyBeanPostProcessor"></bean>
2 <bean id="lifeObject" class="com.neuedu.spring.bean.LifeObject" init-method="initMethod" destroy-method="destoryMethod">
</bean>
测试代码:
@Test
public void test22_1() throws Exception {
//测试方法不需要写任何代码,
//因为ApplicationContext.xml文件加载时就创建了bean对象
}
测试结果:

Eclipse中使用Spring IOC容器的具体方法的更多相关文章
- Spring中Bean获取IOC容器服务的方法
Spring 依赖注入可以让所有的Bean对其IOC容器的存在是没有意识的,甚至可以将容器换成其它的.但实际开发中如果某个Bean对象要用到Spring 容器本身的功能资源,需要意识到IOC容器的存在 ...
- JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(6):Spring IOC容器学习(概念、作用、Bean生命周期)
一.IOC控制反转概念 控制反转(IOC)是一种通过描述(在Java中可以是XML或者是注解)并通过第三方去生产或获取特定对象的方式. 主动创建模式,责任在于开发者,而在被动模式下,责任归于Ioc容器 ...
- 02.Spring Ioc 容器 - 创建
基本概念 Spring IoC 容器负责 Bean 创建.以及其生命周期的管理等.想要使用 IoC容器的前提是创建该容器. 创建 Spring IoC 容器大致有两种: 在应用程序中创建. 在 WEB ...
- 当你的Spring IOC 容器(即applicationContext.xml文件)忘记配到web.xml 文件中时
当你的Spring IOC 容器忘记配到web.xml 文件中时,启动服务器就会报错. 部分错误如下: Caused by: org.springframework.beans.factory.NoS ...
- Spring IOC容器在Web容器中是怎样启动的
前言 我们一般都知道怎样使用spring来开发web应用后,但对spring的内部实现机制通常不是很明白.这里从源码角度分析下Spring是怎样启动的.在讲spring启动之前,我们先来看看一个web ...
- spring-framework-中文文档一:IoC容器、介绍Spring IoC容器和bean
5. IoC容器 5.1介绍Spring IoC容器和bean 5.2容器概述 本章介绍Spring Framework实现控制反转(IoC)[1]原理.IoC也被称为依赖注入(DI).它是一个过程, ...
- 自定义模拟一个Spring IOC容器
一.模拟一个IOC容器: 介绍:现在,我们准备使用一个java project来模拟一个spring的IOC容器创建对象的方法,也就是不使用spring的jar自动帮助我们创建对象,而是通过自己手动书 ...
- Spring IoC容器的初始化过程
Spring IoC容器的初始化包括 BeanDefinition的Resource定位.载入和注册 这三个基本的过程.IoC容器的初始化过程不包含Bean依赖注入的实现.Bean依赖的注入一般会发生 ...
- 【Spring】非Spring IOC容器下获取Spring IOC上下文的环境
前言 在Spring Web项目中,有些特殊的时候需要在非Spring IOC容器下获取Spring IOC容器的上下文环境,比如获取某个bean. 版本说明 声明POM文件,指定需引入的JAR. & ...
随机推荐
- [cinder] volume type 使用简记
cinder type-create sharecinder type-key share set volume_backend_name=GLUSTERFScinder type-create lo ...
- 转:MySQL Row Format(MySQL行格式详解)
MySQL Row Format(MySQL行格式详解) --转载自登博的博客
- nignx 重启
sudo /opt/nginx/sbin/nginx -s stop sudo /opt/nginx/sbin/nginx
- 【新手向】Centos系统文件权限的系统阐述与演示
在linux服务器日常管理中,我们会经常管理查看文件或者文件夹的权限内容以保证服务的正常运行.今天就和大家聊聊文件权限的那些事. 查看文件的权限情况可以用 ll 命令例: ll -d /kid #查看 ...
- oracle——pl/sql 查询中文乱码
1.查看服务器端编码select userenv('language') from dual;我实际查到的结果为:AMERICAN_AMERICA.AL32UTF82.执行语句 select * fr ...
- scrapy(1)安装
用的是python3.6 pip install -i https://pypi.douban.com/simple/ scrapy scrapy startproject Article scrap ...
- MongoDB数据导入hbase + 代码
需求: 从mongoDB里面查出来数据,判断是否有该列簇,如果有则导入此条数据+列簇,如果没有,则该条数据不包含该列簇 直接贴出代码: package Test; import java.util.A ...
- Mediaplayer
Mediaplayer报错 prepareAsync called in state 1 是因为在setDataSource之前调用了prepare.因为setDataSource放到了线程里 ...
- android 自定义控件之事件
首先,继承需要扩展的VIEW,然后在里面添加一个自己的事件方法,例如, oniconclick(myinterface pinterface){ minterface = pinterface; } ...
- [codeforces126B]Password
解题关键:KMP算法中NEXT数组的理解. #include<bits/stdc++.h> #define maxn 1000006 using namespace std; typede ...