框架编写流程:
- 导包:导入四个核心jar包以及日志包,4个核心(beans\core\context\expression)+1个依赖(common-loggins.jar)
- 写配置:spring的配置文件中集合了spring的ioc容器管理的所有组件
- 测试,开发Spring框架的应用,经常要写框架的配置文件,写起来复杂,我们需要给idea安装插件,或者使用官方STS软件。
导包:
- 使用idea进行Spring框架的编写,首先需要创建一个Spring工程,如下图(首先确保idea内有spring相关插件,可以在工程设置的plugins内搜索并下载插件):
写bean.xml配置
对应.java文件中的类,编写ioc容器的配置(简单实例)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--<注册一个Person对象,spring会自动创建这个Person对象>-->
<!--一个Bean标签可以注册一个组件(对象、类)
class:要写注册组件的全类名
id:这个对象的唯一标识
-->
<bean id="person01" class="com.xyb.first.bean.Person">
<!--使用property标签为Person对象的属性赋值
name="name",指定属性名
value="张三"为这个属性赋值
-->
<property name="name" value="张三"></property>
<property name="age" value="18"></property>
<property name="agend" value="F"></property>
<property name="Email" value="123546@qq.com"></property>
</bean>
<bean id="person02" class="com.xyb.first.bean.Person">
<property name="name" value="李四"></property>
<property name="age" value="19"></property>
<property name="agend" value="M"></property>
<property name="Email" value="654321@qq.com"></property>
</bean>
<bean id="person03" class="com.xyb.first.bean.Person">
<!--调用有参构造器进行创建对象并赋值-->
<!--调用有参构造器必须把所有属性全部赋值-->
<!--若要省略属性名直接赋值value,必须按照顺序-->
<!--重载的话可以用type指定参数类型-->
<constructor-arg name="name" value="王五"></constructor-arg>
<constructor-arg name="age" value="20"></constructor-arg>
<constructor-arg name="email" value="123456789@qq.com"></constructor-arg>
<constructor-arg name="agend" value="M"></constructor-arg>
</bean>
<!--通过p名称空间为bean赋值;导入p名称-->
<!--名称空间,在xml中名称空间是用来防止标签重复的-->
<!--
<book>
<b:name>西游记</b:name>
<price>19.98</price>
<author>
<a:name>吴承恩</a:name>
<gender>男</gender>
</author>
</book>
-->
<bean id="person04" class="com.xyb.first.bean.Person"
p:age="18" p:name="嘿嘿嘿"
p:agend="男" p:email="456789@qq.com">
</bean>
</beans>
注意一(通过IOC容器创建对象,并为属性赋值):
1、ApplicationContext(IOC容器的接口)
2、给容器中注册一个组件,我们也从容器中按照id拿到了这个组件的对象组件的创建工作,是容器完成
Person对象是什么时候创建好了呢?容器中对象的创建在容器创建的时候就创建了
3、同一个组件在ioc容器中是单实例的
4、容器中如果没有组件,报错异常
5、Property标签创建对象调用setter方法赋值
6、Property中javaBean的属性名由getter/setter中set后的名字决定
注意二(根据bean的类型从IOC容器中获取bean的实例)
- 如果ioc容器中这个类型的bean有多个,查找就会报错
- 通过p名称空间为bean赋值
<!--通过p名称空间为bean赋值;导入p名称-->
<!--名称空间,在xml中名称空间是用来防止标签重复的-->
<book>
<b:name>西游记</b:name>
<price>19.98</price>
<author>
<a:name>吴承恩</a:name>
<gender>男</gender>
</author>
</book>
注意三(实验4:正确的为各种属性赋值)
- 测试使用null值
不能直接对变量赋值为"null"
<property name="name">
<!--进行复杂的赋值-->
<null></null>
</property>
- 引用类型赋值(引用其他bean、引用内部bean)
<!--ref,代表引用外面的一个值 car=ioc.getBean("car01")-->
<!--<property name="car" ref="car01"></property>-->
<property name="car">
<!--相当于new 一个car然后赋值;引用内部bean,不能被获取到,只能内部使用-->
<bean class="com.xyb.first.bean.Car">
<property name="name" value="奥迪"></property>
</bean>
</property>
- 集合类型赋值(List、Map、Properties)
<bean id="person02" class="com.xyb.first.bean.Person">
<!--如何为list类型赋值-->
<property name="books">
<!--相当于books=new ArrayList<Book>();-->
<list>
<!--在list标签体中添加每一个元素-->
<bean class="com.xyb.first.bean.Book" p:bookName="西游记" p:author="吴承恩"/>
<!--引用内部bean,不能被获取到,只能内部使用-->
<bean id="bookinner" class="com.xyb.first.bean.Book" p:bookName="水浒传"/>
</list>
</property>
<!--如何为Map类型赋值-->
<property name="maps">
<map>
<!--一个entry代表一个键值对-->
<entry key="key01" value="张三"/>
<entry key="key02" value="18"/>
<entry key="key03" value-ref="book01"/>
<entry key="key04">
<bean class="com.xyb.first.bean.Car">
<property name="name" value="大众"/>
<property name="price" value="5000"/>
<property name="number" value="陕A12345"/>
</bean>
</entry>
<entry key="key05">
<map></map>
</entry>
</map>
</property>
<!--如何为properties赋值-->
<property name="properties">
<!--所有的键值对k=v都是String类型-->
<props>
<prop key="username">root</prop>
<prop key="id">1234567</prop>
</props>
</property>
</bean>
- util名称空间创建集合类型的bean
<!--util名称空间创建集合类型的bean:方便别人引用-->
<bean id="person03" class="com.xyb.first.bean.Person">
<property name="maps" ref="mymap"/>
</bean>
<util:map id="mymap">
<!--填元素,方便被别人引用-->
<!--一个entry代表一个键值对-->
<entry key="key01" value="张三"/>
<entry key="key02" value="18"/>
<entry key="key03" value-ref="book01"/>
<entry key="key04">
<bean class="com.xyb.first.bean.Car">
<property name="name" value="大众"/>
<property name="price" value="5000"/>
<property name="number" value="陕A12345"/>
</bean>
</entry>
<entry key="key05">
<map></map>
</entry>
</util:map>
- 级联属性赋值
<!--级联属性赋值:级联属性指的是某成员属性的属性-->
<bean id ="person04" class="com.xyb.first.bean.Person">
<!--为Car赋值的时候,改变Car的价格,因为是引用所以其他地方也会被更改-->
<property name="car" ref="car01"/>
<property name="car.price" value="90000"/>
</bean>
测试
package com.xyb.first.test;
import java.util.List;
import com.xyb.first.bean.Car;
import com.xyb.first.bean.Book;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xyb.first.bean.Person;
public class IOC_test {
private static ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc.xml");
private static ApplicationContext ioc2 = new ClassPathXmlApplicationContext("ioc2.xml");
/**
* 实验1:通过IOC容器创建对象,并未属性赋值
* 1、ApplicationContext(IOC容器的接口)
* 2、给容器中注册一个组件,我们也从容器中按照id拿到了这个组件的对象
* 组件的创建工作,是容器完成
* Person对象是什么时候创建好了呢?
* 容器中对象的创建在容器创建的时候就创建了
* 3、同一个组件在ioc容器中是单实例的
* 4、容器中如果没有组件,报错异常
* 5、Property标签创建对象调用setter方法赋值
* 6、Property中javaBean的属性名由getter/setter中set后的名字决定
*/
public static void test01() {
//ApplicationContext;代表ioc容器
//ClassPathXmlApplicationContext:当前应用的xml配置文件在ClassPath内
//根据spring的配置文件得到ioc容器对象
Person bean_01 = (Person) ioc.getBean("person01");
Person bean_011 = (Person) ioc.getBean("person01");
Person bean_02 = (Person) ioc.getBean("person02");
System.out.println("同一个组件:" + bean_01.equals(bean_011));
System.out.println("不同组件:" + bean_01.equals(bean_02));
System.out.println(bean_01.getName() + bean_01.getAge() + bean_01.getAgend() + bean_01.getEmail());
System.out.println("************************************");
}
/**
* 实验2:根据bean的类型从IOC容器中获取bean的实例
* 如果ioc容器中这个类型的bean有多个,查找就会报错
*/
public static void test02() {
Person bean = ioc.getBean("person02", Person.class);
System.out.println(bean);
System.out.println("************************************");
}
/**
* 实验三:通过构造器为bean属性赋值(index,type属性介绍)
* 通过p名称空间为bean赋值
*/
public static void test03() {
Object bean = ioc.getBean("person03", Person.class);
System.out.println((bean).toString());
System.out.println("************************************");
}
/**
* 实验4:正确的为各种属性赋值
* 测试使用null值
* 引用类型赋值(引用其他bean、引用内部bean)
* 集合类型赋值(List、Map、Properties)、util名称空间创建集合类型的bean
* 级联属性赋值
*/
public static void test04() {
Person bean = ioc2.getBean("person01", Person.class);
Person bean1=ioc2.getBean("person02",Person.class);
Person bean2=ioc2.getBean("person04",Person.class);
Object car=ioc2.getBean("car01",Car.class);
System.out.println(bean.getName());
System.out.println(bean.getCar().getName());
System.out.println(bean1.getBooks().get(0).getBookName());
System.out.println((Car)(bean1.getMaps().get("key04")));
System.out.println(((Car) car).getPrice()==bean2.getCar().getPrice());
}
public static void main(String[] args) {
test01();
test02();
test03();
test04();
}
}