(一)构造器实例化Bean

1. Bean1.java

package com.inspur.ioc;

public class Bean1 {

}

2.Beans1.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bean1" class="com.inspur.ioc.Bean1"></bean>
</beans>

3.InstanceTest.java

package com.inspur.ioc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class InstanceTest { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//1.定义配置文件路径
String xmlPath = "com/inspur/ioc/Beans1.xml";
//2.ApplicationContext 在加载文件时,对bean实例化
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
Bean1 bean1 = (Bean1) applicationContext.getBean("bean1");
System.out.println(bean1); } }

( 二)静态工厂实例化Bean

1.Bean2.java

package com.inspur.static_factory;

public class Bean2 {

}

2.InstanceTest2.java

package com.inspur.static_factory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.inspur.ioc.Bean1; public class InstanceTest2 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//1.定义配置文件路径
String xmlPath = "com/inspur/static_factory/Beans2.xml";
//2.ApplicationContext 在加载文件时,对bean实例化
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
Bean2 bean2 = (Bean2) applicationContext.getBean("bean2");
System.out.println(bean2); } }

3.MyBean2Factory.java

package com.inspur.static_factory;

public class MyBean2Factory {
//使用自己的工厂创建beans实例
public static Bean2 creatBean(){
return new Bean2();
} }

4.beans2.xml:注意配置时class=“里面是工厂的包装类” ,另外需要加factory-method=“是MyBean2Factory里面的静态方法”

<?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 id="bean2" class="com.inspur.static_factory.MyBean2Factory" factory-method="creatBean"></bean>
</beans>

(三)实例工厂创建Bean实例

1.Bean3.java

package com.inspur.factory;

public class Bean3 {

}

2.InstanceTest.java

package com.inspur.factory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.inspur.scope.Bean4;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class InstanceTest { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String xmlPath = "com/inspur/factory/beans3.xml";
//2.ApplicationContext 在加载文件时,对bean实例化
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
Bean3 bean3 = (Bean3) applicationContext.getBean("bean3");
System.out.println(bean3); } }

3.MyBean3Factory.java

package com.inspur.factory;

public class MyBean3Factory {
public MyBean3Factory(){
System.out.println("beans工厂实例化中");
}
//创建beans的实例方法
public Bean3 createBean(){
return new Bean3(); } }

4.beans3.xml:注意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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="MyBean3Factory" class="com.inspur.factory.MyBean3Factory"></bean>
<bean id="bean3" factory-bean="MyBean3Factory" factory-method="createBean"></bean>
</beans>

Bean实例化(三种方法)的更多相关文章

  1. spring注入bean的三种方法

    在Spring的世界中, 我们通常会利用bean config file 或者 annotation注解方式来配置bean. 在第一种利用bean config file(spring xml)方式中 ...

  2. Spring -- 配置bean的三种方法

    配置通过静态工厂方法创建的bean public class StaticBookFactory { //静态工厂方法: public static Book getBook(String bookN ...

  3. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring实例化Bean的三种方法

    在面向对象的程序中,要想调用某个类的成员方法,就需要先实例化该类的对象.在 Spring 中,实例化 Bean 有三种方式,分别是构造器实例化.静态工厂方式实例化和实例工厂方式实例化. 构造器实例化 ...

  4. Spring实例化bean的三种方法

    1.用构造器来实例化 <bean id="hello2" class="com.hsit.hello.impl.ENhello" /> 2.使用静态 ...

  5. Spring、实例化Bean的三种方法

    1.使用类构造器进行实例化 <bean id="personIService" class="cn.server.impl.PersonServiceImpl&qu ...

  6. Spring实例化Bean三种方法:构造器、静态工厂、实例工厂

    Spring中Bean相当于java中的类,可以通过xml文件对bean进行配置和管理. 一.Bean的实例化: 构造器实例化.静态工厂实例化.实例工厂方式实例化. 目录: 构造器实例化: xml配置 ...

  7. Spring学习之实例化bean的三种方式

    实例化bean的三种方式 构造器实例化bean Person.java public class Person { private String name; private Integer age; ...

  8. Spring 实例化bean的三种方式

    第一种方法:直接配置Bean <bena id="所需要实例化的一个实例名称" class="包名.类名"/> 例如: 配置文件中的bean.XML ...

  9. Spring Ioc源码分析系列--容器实例化Bean的四种方法

    Spring Ioc源码分析系列--实例化Bean的几种方法 前言 前面的文章Spring Ioc源码分析系列--Bean实例化过程(二)在讲解到bean真正通过那些方式实例化出来的时候,并没有继续分 ...

  10. 普通Java类获取spring 容器的bean的5种方法

    方法一:在初始化时保存ApplicationContext对象方法二:通过Spring提供的工具类获取ApplicationContext对象方法三:继承自抽象类ApplicationObjectSu ...

随机推荐

  1. DIV的变高与变宽

    代码: <!DOCTYPE HTML><html><head> <meta charset="utf-8"> <title&g ...

  2. 利用vue-cli创建Vue项目

    1.安装node.js:Node.js安装包及源码下载地址为:https://nodejs.org/en/download/. 配置参考:http://www.runoob.com/nodejs/no ...

  3. 排序(bzoj 4552)

    Description 在2016年,佳媛姐姐喜欢上了数字序列.因而他经常研究关于序列的一些奇奇怪怪的问题,现在他在研究一个难题 ,需要你来帮助他.这个难题是这样子的:给出一个1到n的全排列,现在对这 ...

  4. The 2017 ACM-ICPC Asia East Continent League Final记录

    首先感谢tyz学弟的麻麻-给我们弄到了名额- 然后就开始了ACM ECLFinal的玩耍,A*仙人掌可是立了flag要好好打的- 试机赛好像就全是GCJ kickstart的原题,然后AK了但是由于一 ...

  5. C++ 智能指针的简单实现

    智能指针的用处:在c++中,使用普通指针容易造成堆内存的泄露问题,即程序员会忘记释放,以及二次释放,程序发生异常时内存泄漏等问题,而使用智能指针可以更好的管理堆内存.注意,在这里智能指针是一个类而非真 ...

  6. spring boot修改内置容器tomcat的服务端口

    方式一 在spring boot的web 工程中,可以使用内置的web container.有时需要修改服务端口,可以通过配置类和@Configuration注解来完成. // MyConfigura ...

  7. python基础===正则表达式,常用函数re.split和re.sub

    sub的用法: >>> rs = r'c..t' >>> re.sub(rs,'python','scvt dsss cvrt pocdst') 'scvt dss ...

  8. json相关注解和序列化与反序列化

    使用jackson进行序列化时,往往会遇到后台某个实体对象的属性为null,当序列化成json时对应的属性也为null,可以用以下的注解方式完成当属性为null时不参与序列化: @JsonSerial ...

  9. 非旋转Treap——普通平衡树

    扔板跑…… #include<bits/stdc++.h> #define N 100010 #define mp make_pair using namespace std; typed ...

  10. 解决spf13-vim编辑php丢失语法颜色问题

    $ vim .vim/bundle/PIV/ftplugin/php.vim //注释掉以下: "call s:InitVariable("g:load_doxygen_synta ...