Spring实例化bean的四种方式

博客介绍了Spring实例化bean的四种方式,包括setter方式、构造方法实例化、静态工厂方法实例化和工厂方法实例化。详细说明了每种方式的特点及配置方法,如setter方式需实体有set方法,构造方法有默认和带参实例化等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面介绍的是Spring实例化bean的四种方式,分别为:

  1. setter方式
  2. 构造方法实例化
  3. 静态工厂方法实例化
  4. 工厂方法实例化

在介绍spring实例化bean的方式前,先展示bean代码,后面实例化所用到的都是同一个bean

public class Student {

	private String name;
	private int age;
	
	public Student() {
		super();
		System.out.println("无参构造方法");
	}
	
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
		System.out.println("带参构造方法");
	}

	public String getName() {
		return name;
	}

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

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
}

setter方式:

setter方式需要实体有对应的set方法,在配置文件中利用property注入值。

bean.xml配置

<!-- setter 方式 -->
<bean id="student" class="com.zjm.spring.ioc.Student">
    <property name="name" value="李四"></property>
    <property name="age" value="30"></property>
</bean>

测试方法

@Test
public void test(){
	ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
	Student stu = (Student)context.getBean("student");
	System.out.println("姓名:"+stu.getName()+";年龄:"+stu.getAge());
}

构造方法实例化

构造方法实例化是最常用的实例化方法,一种是默认实例化,一种是带参实例化

bean.xml

<!-- 默认构造方法实例化 -->
<bean id="student" class="com.zjm.spring.ioc.Student"></bean> 
<!-- 带参构造方法实例化 -->
<bean id="studentArgs" class="com.zjm.spring.ioc.Student"> 
	<constructor-arg name="name" value="张三"></constructor-arg> 
	<constructor-arg name="age" value="18"></constructor-arg> 
</bean>

测试方法


@Test
public void test(){
	ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
	Student stu = (Student)context.getBean("student");
	System.out.println("姓名:"+stu.getName()+"	年龄:"+stu.getAge());
	stu = (Student)context.getBean("studentArgs");
	System.out.println("姓名:"+stu.getName()+"	年龄:"+stu.getAge());
}

静态工厂方法实例化

静态工厂指定使用class属性指定工厂方法的类路径,使用factory-method属性来指定要实例化bean的方法,并且可以指定方法参数

<!-- 静态工厂方法 -->
<bean id="studentFactory" class="com.zjm.spring.ioc.StudentFactory"
	factory-method="createStudent"></bean>
<bean id="studentFactoryArgs" class="com.zjm.spring.ioc.StudentFactory"
	factory-method="createStudent">
	<constructor-arg name="name" value="张三" />
	<constructor-arg name="age" value="18" />
</bean>

工厂方法

public static Student createStudent(){
	return new Student();
}

public static Student createStudent(String name,int age){
	return new Student(name,age);
}

测试方法

@Test
public void test(){
	ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
	Student stu = context.getBean("studentFactory",Student.class);
	System.out.println("姓名:"+stu.getName()+"	年龄:"+stu.getAge());
	stu = context.getBean("studentFactoryArgs",Student.class);
	System.out.println("姓名:"+stu.getName()+"	年龄:"+stu.getAge());
}

工厂方法实例化

工厂方法用class属性指定工厂方法所在的bean,用factory-bean指定工厂bean,用factory-method指定工厂方法

bean.xml

<!-- 实例工厂方法 -->
<!-- 工厂方法所在的bean -->
<bean id="studentFactory" class="com.zjm.spring.ioc.StudentFactory"></bean>
<!-- 用工厂方法实例化bean -->
<bean id="student" factory-bean="studentFactory" factory-method="createStudent"></bean>
<bean id="studentArgs" factory-bean="studentFactory"
	factory-method="createStudent">
	<constructor-arg name="name" value="张三"></constructor-arg>
	<constructor-arg name="age" value="18"></constructor-arg>
</bean>

工厂方法

public Student createStudent(){
	return new Student();
}

public Student createStudent(String name,int age){
	return new Student(name,age);
}

测试方法

@Test
public void test(){
	ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
	Student stu = context.getBean("student",Student.class);
	System.out.println("姓名:"+stu.getName()+"	年龄:"+stu.getAge());
	stu = context.getBean("studentArgs",Student.class);
	System.out.println("姓名:"+stu.getName()+"	年龄:"+stu.getAge());
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值