Spring入门之配置bean的简单原理

Spring入门之Hello World

标签(空格分隔): Spring


Spring框架,IOC容器的基本使用

//这是student类
package com.test.beans;

public class Students {
	private int id;
	private String name;
	private String sex;
	private int age;
	private double money;
	
	
	public Students() {
		super();
		System.out.println("调用无参构造");
	}
	
	public Students(int id, String name, String sex, int age, double money) {
		super();
		this.id = id;
		this.name = name;
		this.sex = sex;
		this.age = age;
		this.money = money;
		System.out.println("调用全参构造器");
	}

	public Students(int id, String name, String sex, int age) {
		super();
		this.id = id;
		this.name = name;
		this.sex = sex;
		this.age = age;
		System.out.println("缺少金钱构造器");
	}

	public Students(int id, String name, String sex, double money) {
		super();
		this.id = id;
		this.name = name;
		this.sex = sex;
		this.money = money;
		System.out.println("缺少年龄构造器");
	}

	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	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;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}

	@Override
	public String toString() {
		return "Students [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + ", money=" + money + "]";
	}
	
	
}

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:给当前bean起个名字,该id在Spring的配置文件中是唯一的
		 class:配置bean的全类名,Spring会利用反射技术调用无参构造实例化bean
	 -->
	 
	 <!-- 这是通过属性注入(setter方法注入)的方式注入bean
	      默认情况下,IOC容器只要一创建,Spring会调用bean的无参构造创建对象,然后调用相应的set方法赋值
	 -->
	<bean id="students" class="com.test.beans.Students">
	    <!-- 通过property标签给bean属性赋值 -->
		<property name="id" value="01"></property>
		<property name="name" value="张笑航"></property>
		<property name="age" value="19"></property>
		<property name="sex" value=""></property>
		<property name="money" value="1000.00"></property>
	</bean>
	
	<!-- 下面是通过有参构造器给属性添加值 -->
	<bean id="students2" class="com.test.beans.Students">
		<constructor-arg value="02"></constructor-arg>
		<!-- 当参数顺序不正确的时候,可以添加index属性排序,开始的索引为0 -->
		<constructor-arg value="" index="2"></constructor-arg>
		<constructor-arg value="高欣越" index="1"></constructor-arg>
		
		<constructor-arg value="18"></constructor-arg>
		<constructor-arg value="500.00"></constructor-arg>
	</bean>
	
	<bean id="students3" class="com.test.beans.Students">
		<constructor-arg value="02"></constructor-arg>
		<constructor-arg value="高欣越"></constructor-arg>
		<constructor-arg value=""></constructor-arg>
		<!-- 当两个有参构造器后面参数类型差不多相同,默认情况下会选最下面的构造器,这时候就可以通过type这个属性,指定其类型,就可以指定构造器了,
		当构造器里面的参数类型有相同的时候,当给属性加上type的时候,他会优先给赋值,所以需要再加上index防止顺序变乱 -->
		<constructor-arg value="18" type="int" index="3"></constructor-arg>
		
		<!-- 通过P属性配置 需要去namespaces里面勾选上p属性-->
	<bean id="students4" class="com.test.beans.Students"
	p:id="03"
	p:name="波波"
	p:sex=""
	p:age="18"
	p:money="1000.00"
	
	></bean>
	
	<bean id="student5" class="com.test.beans.Students">
		<property name="id" >
			<value>01</value>
		</property>
		<property name="name">
			<!-- 特殊符号就需要用CDATA区包裹起来 -->
			<value><![CDATA[<三国演义>]]></value>
		</property>
		<property name="age" value="19"></property>
		<property name="sex" value=""></property>
		<property name="money" value="1000.00"></property>
	</bean>
		

</beans>

白盒测试类

package com.test.helloworldtest;



import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.test.helloworld.HelloWorld;



class HelloWorldTest {

	@Test
	void test() {
		//1.创建对象
		//HelloWorld helloWorld = new HelloWorld();
		//2.赋值
		//helloWorld.setName("Spring");
		
		//有了Spring之后。1、2步可以交给Spring
		
		//创建IOC容器对象
		//ApplicationContext ioc = new ClassPathXmlApplicationContext("helloworldBean.xml");
		
		ApplicationContext ioc2 = new ClassPathXmlApplicationContext("beans.xml");
		//从IOC容器中获取对象
		/*获取bean的方式:
		 *	1)根据名称获取(id属性值)
		 *	2)根据bean的类型获取,必须保证IOC容器中该类型的bean只有一个,否则会抛出异常
		 */
//		HelloWorld hello =(HelloWorld) ioc.getBean("hello");
		//HelloWorld hello = ioc.getBean(HelloWorld.class);
		
		Students bean = (Students) ioc2.getBean("students2");
		System.out.println(bean);
		//3.调用
		hello.sayHello();
	}
	

}

###基本流程:

  • 先通过XML创建bean标签配置需要反射的类,然后通过property标签可以给属性赋值,然后可以通过Spring的ApplicationContext接口的实现类ClassPathXmlApplicationContext(xmlPath),里面的参数为Spring的XML配置文件地址,然后就会生成一个IOC容器对象,只要IOC容器被创建,就会执行无参构造和set方法。
  • 然后通过的到的ioc容器,使用getBean(BeanId)方法,参数为XML文件里面的bean的id名。就可以通过反射它的无参构造创造一个相对应的对象,赋值操作会经过该属性的set方法赋值。得到该对象之后就可以使用其中的方法。
  • 使用属性注入(setter方法注入)的方式注入bean,默认情况下,IOC容器只要一创建,Spring会调用bean的无参构造创建对象,然后调用相应的set方法赋值。

###有参构造器注意事项

  • 当通过使用有参构造方法赋值的时候,默认赋值的情况下顺序不可以乱,可能会因为顺序乱,所赋值的类型不同而造成异常报错,如果顺序不对,可以添加index 属性,默认索引为0开始。
  • 当有参构造器有多个,而且类型相差不大,而且都可以赋值,比如int和double类型,都可以赋值数字,但是怕用错构造器,默认情况下会选顺序在下面的构造器。不过可以通过type属性,给定值得类型,就可以调用想要的构造器。有时候会出现构造器里面多个参数类型相同,当给参数加上type属性之后,会默认使得该参数得优先级变大,也必须配合使用index属性给定顺序。

###需要注意的细节问题

<property name="id" value="01"></property>
<!-- 赋值也可以通过value的子节点赋值 -->
<property name="id">
    <value>01</value>
</property>
  • 字面量问题:
  • 基本数据类型及其封装类、String类等类型都可以采取字面值注入的方式。
  • 若字面值中包含特殊字符,可以使用<![CDATA[]]>把字面值包裹起来。
  • 指定null值
<property name="id">
    <!-- 特殊符号就需要用CDATA区包裹起来 -->
    <value><![CDATA[<三国演义>]]></value>
</property>
<!-- 指定null值 -->
<property name="name">
    <null></null>
</property>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值