Spring中Bean实例化的三种方式
程序员文章站
2022-03-03 11:34:12
...
bean 实例化的三种方式实现:
一、构造器实例化(无参)
1.定义一个 bean:
bean1.java:
package com.lym;
public class Bean1 {
}
2.在Spring配置文件Bean1.xml 中 配置 bean:
<?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-4.3.xsd">
<bean id="bean1" class="com.lym.Bean1"/>
</beans>
<!-- 关键部分 -->
<bean id="bean1" class="com.lym.Bean1"/>
id 为 在 xml 里的这个 bean的标识, class 为xml 里的这个bean 绑定的java类(bean)的全路径(包名+类名)
3.测试代码从配置文件中取出Bean1对象:
package com.lym;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test1 {
public static void main(String[] args) {
//定义配置文件路径
String xmlPath="com/lym/Beans1.xml";
//ApplicationContext在加载配置文件时,对Bean进行实例化
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlPath);
Bean1 bean=(Bean1) applicationContext.getBean("bean1");
System.out.println(bean);
}
}
getBean() 返回的就是由spring 实例化的对象,这里是bean1。
第二种:静态工厂方式实例化
1.定义一个bean:
bean2.java:
package com.lym2;
public class Bean2 {
}
2.配置spring的配置文件
<?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-4.3.xsd">
<bean id="bean2" class="com.lym2.MyBean2Factory" factory-method="createBean"/>
</beans>
<!-- 关键部分 -->
<bean id="bean2" class="com.lym2.MyBean2Factory" factory-method="createBean"/>
factory-method = "createBean"表示调用 class 下的 createBean方法来创建对象(factory-method指定的方法必须为static静态方法)
3.定义静态工厂类
package com.lym2;
public class MyBean2Factory {
public static Bean2 createBean() {
return new Bean2();
}
}
注意静态工厂类的创建对象方法为静态
4.测试代码:
package com.lym2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
String xmlPath="com/lym2/beans2.xml";
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlPath);
System.out.println(applicationContext.getBean("bean2"));
}
}
第三种:实例工厂方式实例化
1.定义一个bean:
package com.lym3;
public class Bean3 {
}
2.配置spring的配置文件:
<?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-4.3.xsd">
<bean id="myBean3Factory" class="com.lym3.MyBean3Factory" />
<bean id="bean3" factory-bean="myBean3Factory" factory-method="createBean" />
</beans>
<!-- 关键部分 -->
<bean id="myBean3Factory" class="com.lym3.MyBean3Factory" />
<bean id="bean3" factory-bean="myBean3Factory" factory-method="createBean" />
</beans>
3.工厂类(非静态)
package com.lym3;
public class MyBean3Factory {
public MyBean3Factory() {
System.out.println("bean3工厂实例化中");
}
public Bean3 createBean() {
return new Bean3();
}
}
4.测试代码:
package com.lym3;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
String xmlPath="com/lym3/beans3.xml";
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlPath);
System.out.println(applicationContext.getBean("bean3"));
}
}
总结:
静态工厂类与非静态工厂类的区别是,前者不需要创建对象,直接可以调用静态方法创建bean;后者则要先创建对象,然后再通过对象调用其方法创建bean