Spring实例化Bean的三种方式

导读:本篇文章讲解 Spring实例化Bean的三种方式,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

Spring实例化Bean的三种方式

Bean的实例化方式有三种,分别是构造器实例化,静态工厂方式实例化,实例工厂方式实例化

构造器实例化

  • 无参构造器(默认构造器)
  • 有参构造器

无参构造器实例化Bean

Users类:

public class Users {

	private Integer id;
	private String name;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String toString() {
		return "Users [id=" + id + ", name=" + name + "]";
	}

	// 无参构造
	public Users() {
		super();
	}

}

配置XML文件 beans.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-4.3.xsd">
	<bean id="bean" class="instance.contructor.Users">
		<property name="id" value='01'></property>
		<property name="name" value='张三'></property>
	</bean>
</beans>

Test类:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/*
 * 无参构造器实例化
 */
public class InstanceTest {
	public static void main(String[] args) {
		String xmlPathString = "instance/contructor/beans.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPathString);
		Users users = (Users) applicationContext.getBean("bean");
		System.out.println(users);
	}
}

运行结果:

在这里插入图片描述

有参构造器实例化Bean
Users类

public class Users {

	private Integer id;
	private String name;

	public String toString() {
		return "Users [id=" + id + ", name=" + name + "]";
	}

	// 有参构造器
	public Users(Integer id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

}

配置文件 beans.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-4.3.xsd">
	<bean id="bean" class="instance.contructor.Users">
	<constructor-arg index="0" value="01"></constructor-arg>
	<constructor-arg index="1" value="张三"></constructor-arg>
	</bean>
</beans>

Test类

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/*
 * 有参构造器实例化
 */
public class InstanceTest {
	public static void main(String[] args) {
		String xmlPathString = "instance/contructor/beans.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPathString);
		Users users = (Users) applicationContext.getBean("bean");
		System.out.println(users);
	}
}

静态工厂方式与实例工厂方式实例化
Student类

public class Student {

	private Integer id;
	private String name;
	private String gender;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public Student(Integer id, String name, String gender) {
		super();
		this.id = id;
		this.name = name;
		this.gender = gender;
	}

	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", gender=" + gender + "]";
	}

}

Instance_factory类

import java.util.HashMap;
import java.util.Map;

public class Instance_factory {

	private static Map<String, Student> stuMap=new HashMap<String, Student>();
	public Instance_factory() {
		stuMap.put("stu1", new Student(01, "张三", "男"));
		stuMap.put("stu2", new Student(02, "李四", "女"));
	}
	public Student getStu(String stu) {
		return stuMap.get(stu);
	}
}

Static_factory类

import java.util.HashMap;
import java.util.Map;

public class Static_factory {

	private static Map<String, Student> stuMap = new HashMap<String, Student>();
	static {
		stuMap.put("stu1", new Student(01, "张三", "男"));
		stuMap.put("stu2", new Student(02, "李四", "男"));
	}

	public static Student getStu(String stu) {
		return stuMap.get(stu);

	}
}

配置文件 beans.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-4.3.xsd">
	<!-- 实例工厂方法先将工厂实例化 -->
	<bean id="Instance_factory"
		class="factory.Instance_factory" />
	<bean id="stu11" factory-bean="Instance_factory"
		factory-method="getStu">
		<constructor-arg value="stu1" />
	</bean>
	<bean id="stu12" factory-bean="Instance_factory"
		factory-method="getStu">
		<constructor-arg value="stu2" />
	</bean>
	<!-- 静态工厂方法不需要将工厂实例化 -->
	<bean id="stu21"
		class="factory.Static_factory"
		factory-method="getStu">
		<constructor-arg value="stu1" />
	</bean>
	<bean id="stu22"
		class="factory.Static_factory"
		factory-method="getStu">
		<constructor-arg value="stu2" />
	</bean>
</beans>

Test类

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

	public static void main(String[] args) {

		String xmlPath = "factory/beans.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);

		System.out.println("实例工厂方法");
		Student stu11 = (Student) applicationContext.getBean("stu11");
		Student stu12 = (Student) applicationContext.getBean("stu12");
		System.out.println(stu11);
		System.out.println(stu12);
		System.out.println("静态工厂方法");
		Student stu21 = (Student) applicationContext.getBean("stu21");
		Student stu22 = (Student) applicationContext.getBean("stu22");
		System.out.println(stu21);
		System.out.println(stu22);
	}

}

运行结果:
在这里插入图片描述
需要导入的jar包:
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/5044.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!