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容器的具体方法的更多相关文章

  1. Spring中Bean获取IOC容器服务的方法

    Spring 依赖注入可以让所有的Bean对其IOC容器的存在是没有意识的,甚至可以将容器换成其它的.但实际开发中如果某个Bean对象要用到Spring 容器本身的功能资源,需要意识到IOC容器的存在 ...

  2. JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(6):Spring IOC容器学习(概念、作用、Bean生命周期)

    一.IOC控制反转概念 控制反转(IOC)是一种通过描述(在Java中可以是XML或者是注解)并通过第三方去生产或获取特定对象的方式. 主动创建模式,责任在于开发者,而在被动模式下,责任归于Ioc容器 ...

  3. 02.Spring Ioc 容器 - 创建

    基本概念 Spring IoC 容器负责 Bean 创建.以及其生命周期的管理等.想要使用 IoC容器的前提是创建该容器. 创建 Spring IoC 容器大致有两种: 在应用程序中创建. 在 WEB ...

  4. 当你的Spring IOC 容器(即applicationContext.xml文件)忘记配到web.xml 文件中时

    当你的Spring IOC 容器忘记配到web.xml 文件中时,启动服务器就会报错. 部分错误如下: Caused by: org.springframework.beans.factory.NoS ...

  5. Spring IOC容器在Web容器中是怎样启动的

    前言 我们一般都知道怎样使用spring来开发web应用后,但对spring的内部实现机制通常不是很明白.这里从源码角度分析下Spring是怎样启动的.在讲spring启动之前,我们先来看看一个web ...

  6. spring-framework-中文文档一:IoC容器、介绍Spring IoC容器和bean

    5. IoC容器 5.1介绍Spring IoC容器和bean 5.2容器概述 本章介绍Spring Framework实现控制反转(IoC)[1]原理.IoC也被称为依赖注入(DI).它是一个过程, ...

  7. 自定义模拟一个Spring IOC容器

    一.模拟一个IOC容器: 介绍:现在,我们准备使用一个java project来模拟一个spring的IOC容器创建对象的方法,也就是不使用spring的jar自动帮助我们创建对象,而是通过自己手动书 ...

  8. Spring IoC容器的初始化过程

    Spring IoC容器的初始化包括 BeanDefinition的Resource定位.载入和注册 这三个基本的过程.IoC容器的初始化过程不包含Bean依赖注入的实现.Bean依赖的注入一般会发生 ...

  9. 【Spring】非Spring IOC容器下获取Spring IOC上下文的环境

    前言 在Spring Web项目中,有些特殊的时候需要在非Spring IOC容器下获取Spring IOC容器的上下文环境,比如获取某个bean. 版本说明 声明POM文件,指定需引入的JAR. & ...

随机推荐

  1. nc之一:NetCat简介与使用方法

    精品学习网考试频道小编应广大考生的需要,特为参加考试的考生策划了“NetCat简介与使用方法”专题等有关资料,供考生参考! 在入侵中它是最经典的工具之一 ,NetCat被所有的网络安全爱好者和研究者称 ...

  2. mybatis 学习五 动态SQL语句

    3.1 selectKey 标签 在insert语句中,在Oracle经常使用序列.在MySQL中使用函数来自动生成插入表的主键,而且需要方法能返回这个生成主键.使用myBatis的selectKey ...

  3. json 工具处理类

    package com.js.ai.modules.pointwall.util; import java.lang.reflect.Type; import java.net.URLDecoder; ...

  4. Java中自动装箱代码初探

    <深入理解Java虚拟机>中讲语法糖时,提到了下面这个例子(不是原文中的例子,我自己改过): public class AutoBoxingTest { /** * @param args ...

  5. Firemonkey Android IOS 图标

    图标很多

  6. LNMP 1.6 常见的502问题解决

    在nginx上跑discuz,先修改配置文件 cd /usr/local/nginx/conf/vhosts/ vim test.conf server { listen ; server_name ...

  7. 部署和调优 1.1 nfs部署和优化-2

    更改共享目录文件默认的所有者和所属组 已知道客户端有个user11用户 cat /etc/passwd user11:x:501:501::/home/user11:/bin/bash 服务端打开 v ...

  8. 【Android 多媒体应用】使用 MediaPlayer 播放视频

    1.MainActivity.java import android.media.AudioManager; import android.media.MediaPlayer; import andr ...

  9. C语言学习笔记--指针阅读技巧

    1. 指针阅读技巧:右左法则 (1)从最里层的圆括号中未定义的标示符看起 (2)首先往右看,再往左看 (3)遇到圆括号或方括号时可以确定部分类型,并调转方向 (4)重复 2.3 步骤,直到阅读结束 注 ...

  10. 转载-你应该知道的 RPC 原理

    在校期间大家都写过不少程序,比如写个hello world服务类,然后本地调用下,如下所示.这些程序的特点是服务消费方和服务提供方是本地调用关系. 而一旦踏入公司尤其是大型互联网公司就会发现,公司的系 ...