spring Bean初始化方法:无参构造、有参构造、set注入

20 篇文章 1 订阅

1.Bean的初始化方式:

1.无参构造(默认构造方法);
2.有参构造(自定义构造方法);
3.set方法

2.例子

2.1新建一个空的spring项目

在这里插入图片描述
其中:
client包是主方法所在类的包;
domain包是实体Bean的包;
resource包是xml配置文件所在的包;
readme是项目介绍。

2.2类文件创建

在这里插入图片描述

2.3java文件

package client;

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

import domain.People;

public class Main {

	public static void main(String[] args) {
		
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"resource/*With.xml");
		People people = (People) applicationContext.getBean("people");
		people.say();
		
		applicationContext = new ClassPathXmlApplicationContext(
				"resource/*No.xml");
		people = (People) applicationContext.getBean("people");
		people.say();
		
		applicationContext = new ClassPathXmlApplicationContext(
				"resource/*Pro.xml");
		people = (People) applicationContext.getBean("people");
		people.say();
	}

}

package domain;

import java.io.Serializable;

public class People implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3290777865940655640L;

	private String name;
	
	private String age;

	public void say(){
		System.out.println("say");
	}
	
	public People(String name,String age){
		this.name = name;
		this.age = age;
		System.out.println(this.getClass()+"有参构造");
	}
	
	public People(){
		System.out.println(this.getClass()+"无参构造");
	}
	
	public String getName() {
		return name;
	}

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

	public String getAge() {
		return age;
	}

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

2.4xml文件

beanNo.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="people" class="domain.People">
	</bean>
	
</beans>

beanPro.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="people" class="domain.People">
		<property name="name">
			<value>xiaojua</value>
		</property>
		<property name="age">
			<value>23</value>
		</property>
	</bean>
	
</beans>

beanWith.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="people" class="domain.People">
		<constructor-arg name="name" type="java.lang.String">
			<value>xiaoming</value>
		</constructor-arg>
		<constructor-arg name="age" type="java.lang.String">
			<value>12</value>
		</constructor-arg>
	</bean>
	
</beans>

2.5运行结果

class domain.People有参构造
say
class domain.People无参构造
say
class domain.People无参构造
say

3.总结

JavaBean的初始化默认使用默认的构造方法,所以,如果某些操作必须在类加载时完成,可以使用显示的默认无参构造方法中完成,否则会使用隐式的默认无参构造。

这个项目实现不同的bean文件配置以及使用不同的方式初始化
使用无参构造方法,
有参构造
set方法

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值