bean的4种实例化方法

具体文件格式

application.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd" >

<import resource="classpath:beaninstance/xml/beaninstance.xml"></import>

</beans>

beaninstance.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd" >

<!--bean的初始化-->

<!--①.构造器实例化(无参数构造器),最标准,使用最多。-->

<bean id="bean1" class="beaninstance.beanold.bean1"></bean>

<!--②.静态工厂方法实例化:解决系统遗留问题-->

<!-- class:静态工厂的类名 从根目录中获取

factory-method:静态工厂中对应的静态方法,该方法会返回相对应的对象(在这里返回的是bean2对象)

-->

<bean id="bean2" class="beaninstance.staticfactory.bean2factory" factory-method="getbean2"></bean>

<!--③.实例工厂方法实例化:解决系统遗留问题-->

<!--

配置时应该配置2bean,第一个为工厂bean,第二个为对象的bean

其中factory-bean:实例化工厂对象

factory-method:实例化工厂对象的方法,该方法返回对应的对象

-->

<bean id="bean3factory" class="beaninstance.instancefactory.bean3factory"></bean>

<bean id="bean3" factory-bean="bean3factory" factory-method="getbean3"></bean>

<!--④.实现FactoryBean接口实例化:实例工厂变种-->

<!-- class:为具体实例工厂 -->

<bean id="bean4" class="beaninstance.factorybean.bean4factory"></bean>

</beans>

//第一种实例化方式

bean1类

package beaninstance.beanold;

public class bean1 {

//<!--①.构造器实例化(无参数构造器),最标准,使用最多。-->

public bean1() {

System.out.println("走好选择的路,别选择好走的路,你才能拥有真正的自己。");

}

public void sayhello(){

System.out.println("所谓的焦虑就是书读得太少,而想得又太多");

}

}

//第二种实例化方式

bean2类

package beaninstance.staticfactory;

public class bean2 {

//②.静态工厂方法实例化:解决系统遗留问题

public bean2() {

System.out.println("刚开始是假装坚强,后来就真的坚强了。");

}

public void sayhello(){

System.out.println("人间不会有单纯的快乐,快乐总夹杂着烦恼和忧虑,人间也没有永远。");

}

}

bean2factory静态工厂类

package beaninstance.staticfactory;

public class bean2factory {

//②.静态工厂方法实例化:解决系统遗留问题

public static bean2 getbean2(){

return new bean2();

}

}

//第三种实例化方式

bean3类

package beaninstance.instancefactory;

public class bean3 {

public bean3() {

System.out.println("年轻的时候以为不读书不足以了解人生,直到后来才发现如果不了解人生,是读不懂书的。读书的意义大概就是用生活所感去读书,用读书所得去生活吧。");

}

public void sayhello(){

System.out.println("我不要儿子,我要女儿──只要一个,像你的。见她之前,从未想结婚;娶她之后,我从未后悔");

}

}

bean3factory实例化工厂类

package beaninstance.instancefactory;

public class bean3factory {

public bean3 getbean3(){

return new bean3();

}

}

//第四种实例化方式

bean4类

package beaninstance.factorybean;

public class bean4 {

public bean4() {

System.out.println("我和谁都不争,和谁争我都不屑。简朴的生活、高贵的灵魂是人生的至高境界。");

}

public void sayhello(){

System.out.println("一个人不想攀高就不怕下跌,也不用倾轧排挤,可以保其天真,成其自然,潜心一志完成自己能做的事。");

}

}

bean4factory工厂类

package beaninstance.factorybean;

import org.springframework.beans.factory.FactoryBean;

public class bean4factory implements FactoryBean<bean4> {

//实例工厂方法,创建当前类的对象

@Override

public bean4 getObject() throws Exception {

return new bean4();

}

//返回对象的类型

@Override

public Class<?> getObjectType() {

return bean4.class;

}

//是否弄成单例,如果返回true,永远返回该对象

@Override

public boolean isSingleton() {

return true;

}

}

test测试类

package beaninstance.test;

import beaninstance.beanold.bean1;

import beaninstance.factorybean.bean4;

import beaninstance.instancefactory.bean3;

import beaninstance.instancefactory.bean3factory;

import beaninstance.staticfactory.bean2;

import beaninstance.staticfactory.bean2factory;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.ApplicationContext;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

//SpringJUnit4ClassRunner.class为单元测试

@RunWith(SpringJUnit4ClassRunner.class)

//@ContextConfiguration:关联自己的测试文件,xml格式文件,其中文件路径为classpath路径下的文件

@ContextConfiguration("classpath:beaninstance/xml/beaninstance.xml")

public class test {

//自动注入

@Autowired

private bean1 bean1;

@Test

public void beaninstance(){

System.out.println(bean1);

bean1.sayhello();

}

//②.静态工厂方法实例化:解决系统遗留问题

@Autowired

private ApplicationContext ctx;

@Test

public void testStaticFactory(){

//老方法

bean2 bean2= bean2factory.getbean2();

bean2.sayhello();

System.out.println(bean2);

//新方法

bean2 bean2new=ctx.getBean("bean2",bean2.class);

System.out.println("-----------------");

System.out.println(bean2new);

bean2new.sayhello();

}

//③.实例工厂方法实例化:解决系统遗留问题

@Test

public void testInstanceFactory(){

//老方法

bean3factory factory=new bean3factory();

bean3 bean3=factory.getbean3();

System.out.println(bean3);

bean3.sayhello();

//新方法

bean3 beannew3=ctx.getBean("bean3",bean3.class);

System.out.println(beannew3);

beannew3.sayhello();

}

@Test

public void testFactoryBean(){

bean4 bean4=ctx.getBean("bean4",bean4.class);

System.out.println(bean4);

bean4.sayhello();

}

}

测试结果如下

猜你喜欢

转载自blog.csdn.net/zhouzhou_98/article/details/84647998