package com.hgh.spring.helloworld;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring {
public static void main(String[] args) {
//创建spring的ioc容器
/*
* 在 Spring IOC 容器读取 Bean 配置创建 Bean 实例之前, 必须对它进行实例化. 只有在容器实例化后, 才可以从 IOC 容器里获取 Bean 实例并使用.
Spring 提供了两种类型的 IOC 容器实现.
BeanFactory: IOC 容器的基本实现.
ApplicationContext: 提供了更多的高级特性,是 BeanFactory 的子接口.
* ApplicationContext 面向使用 Spring 框架的开发者,
* 几乎所有的应用场合都直接使用 ApplicationContext 而非底层的 BeanFactory
*
* ApplicationContext实现了多个接口,它的父接口又实现了其他接口
* */
ApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
//从容器中获取Bean
Person person = (Person) ac.getBean("person");
System.out.println(person);
Person person2 = (Person) ac.getBean("person2");
System.out.println(person2);
PersonNew personNew = (PersonNew) ac.getBean("carperson");
System.out.println(personNew);
PersonNew personNew2 = (PersonNew) ac.getBean("carperson2");
System.out.println(personNew2);
PersonNew personNew3 = (PersonNew) ac.getBean("carperson3");
System.out.println(personNew3);
PersonHasCars personHasCars = (PersonHasCars) ac.getBean("PersonList");
System.out.println(personHasCars);
PersonMap personMap = (PersonMap) ac.getBean("personMap");
System.out.println(personMap);
Person person3 = (Person) ac.getBean("person6");
System.out.println(person3);
Person person4 = (Person) ac.getBean("person7");
System.out.println(person4);
}
}
springpeizhi
<?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:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!--
id:Bean 的名称。
在 IOC 容器中必须是唯一的
若 id 没有指定,Spring 自动将权限定性类名作为 Bean 的名字
id 可以指定多个名字,名字之间可用逗号、分号、或空格分隔
-->
<bean id="person" class="com.hgh.spring.helloworld.Person">
<!-- 通过反射,调动对应的setxxx方法为属性赋值 -->
<property name="name2" value="hgh"></property>
<property name="age" value="27"></property>
<property name="city" value="shenzhen"></property>
</bean>
<!--
依赖注入的方式
Spring 支持 3 种依赖注入的方式
属性注入
构造器注入
工厂方法注入(很少使用,不推荐)
-->
<bean id="person2" class="com.hgh.spring.helloworld.Person">
<!-- 属性注入
<property name="name2" value="hfz"></property>
<property name="age">
<value>25</value>
</property>
<property name="city" value="shenzhen"></property>
-->
<!-- 构造器注入 ,可以指定参数的列表(第几个和类型)
字面值:可用字符串表示的值,可以通过 <value> 元素标签或 value 属性进行注入。
基本数据类型及其封装类、String 等类型都可以采取字面值注入的方式
若字面值中包含特殊字符,可以使用 <![CDATA[]]> 把字面值包裹起来。
<constructor-arg value="hfz" index="0" type="java.lang.String"></constructor-arg>
-->
<constructor-arg index="0" type="java.lang.String">
<value><![CDATA[HFZ^^>]]></value>
</constructor-arg>
<constructor-arg value="25" index="1"></constructor-arg>
<constructor-arg value="sz" index="2"></constructor-arg>
</bean>
<!--
引用其它 Bean
组成应用程序的 Bean 经常需要相互协作以完成应用程序的功能. 要使 Bean 能够相互访问, 就必须在 Bean 配置文件中指定对 Bean 的引用
在 Bean 的配置文件中, 可以通过 <ref> 元素或 ref 属性为 Bean 的属性或构造器参数指定对 Bean 的引用.
也可以在属性或构造器里包含 Bean 的声明, 这样的 Bean 称为内部 Bean
-->
<bean name="bmw" class="com.hgh.spring.helloworld.Car">
<property name="company" value="bmw"></property>
<property name="price" value="5000000"></property>
<property name="maxSpice" value="300"></property>
</bean>
<bean name="benchi" class="com.hgh.spring.helloworld.Car">
<property name="company" value="benchi"></property>
<property name="price" value="5000000"></property>
<property name="maxSpice" value="300"></property>
</bean>
<bean name="carperson" class="com.hgh.spring.helloworld.PersonNew">
<property name="name" value="hgh"></property>
<property name="age" value="11"></property>
<property name="city" value="gx"></property>
<property name="car" ref="bmw"></property>
</bean>
<!-- 内部 Bean
当 Bean 实例仅仅给一个特定的属性使用时, 可以将其声明为内部 Bean. 内部 Bean 声明直接包含在 <property> 或 <constructor-arg> 元素里, 不需要设置任何 id 或 name 属性
内部 Bean 不能使用在任何其他地方
-->
<bean name="carperson2" class="com.hgh.spring.helloworld.PersonNew">
<property name="name" value="hgh"></property>
<property name="age" value="11"></property>
<property name="city" value="gx"></property>
<property name="car">
<bean name="audit" class="com.hgh.spring.helloworld.Car">
<property name="company" value="audit"></property>
<property name="price" value="5000000"></property>
<property name="maxSpice" value="300"></property>
</bean>
</property>
</bean>
<!--
null 值和级联属性
-->
<bean name="carperson3" class="com.hgh.spring.helloworld.PersonNew">
<property name="name" value="hgh"></property>
<property name="age" value="11"></property>
<property name="city" value="gx"></property>
<property name="car"><null/></property>
</bean>
<!--
集合属性
在 Spring中可以通过一组内置的 xml 标签(例如: <list>, <set> 或 <map>) 来配置集合属性.
配置 java.util.List 类型的属性, 需要指定 <list> 标签, 在标签里包含一些元素. 这些标签可以通过 <value> 指定简单的常量值, 通过 <ref> 指定对其他 Bean 的引用. 通过<bean> 指定内置 Bean 定义. 通过 <null/> 指定空元素. 甚至可以内嵌其他集合.
数组的定义和 List 一样, 都使用 <list>
配置 java.util.Set 需要使用 <set> 标签, 定义元素的方法与 List 一样.
-->
<bean id="PersonList" class="com.hgh.spring.helloworld.PersonHasCars">
<property name="name" value="hgh"></property>
<property name="age" value="11"></property>
<property name="city" value="gx"></property>
<property name="cars">
<list>
<ref bean="bmw"/>
<ref bean="benchi"/>
</list>
</property>
</bean>
<!--
使用 utility scheme 定义集合
使用基本的集合标签定义集合时, 不能将集合作为独立的 Bean 定义, 导致其他 Bean 无法引用该集合, 所以无法在不同 Bean 之间共享集合.
可以使用 util schema 里的集合标签定义独立的集合 Bean. 需要注意的是, 必须在 <beans> 根元素里添加 util schema 定义
-->
<bean id="PersonList2" class="com.hgh.spring.helloworld.PersonHasCars">
<property name="name" value="hgh"></property>
<property name="age" value="11"></property>
<property name="city" value="gx"></property>
<property name="cars" ref="cars"></property>
</bean>
<util:list id = "cars">
<ref bean="bmw"/>
<ref bean="benchi"/>
</util:list>
<bean id="personMap" class="com.hgh.spring.helloworld.PersonMap">
<property name="name" value="hgh"></property>
<property name="age" value="11"></property>
<property name="city" value="gx"></property>
<property name="productMap">
<map>
<entry key="hgh" value="123"></entry>
<entry key="hfz" value="1213123"></entry>
</map>
</property>
</bean>
<!--
使用 p 命名空间
为了简化 XML 文件的配置,越来越多的 XML 文件采用属性而非子元素配置信息。
Spring 从 2.5 版本开始引入了一个新的 p 命名空间,可以通过 <bean> 元素属性的方式配置 Bean 的属性。
使用 p 命名空间后,基于 XML 的配置方式将进一步简化
-->
<bean id="person6" class="com.hgh.spring.helloworld.Person" p:age="12" p:city="sz" p:name2="hfg">
</bean>
<!-- bean 的配置能够继承吗 ? 使用 parent 来完成继承 -->
<bean id="person7" parent="person6" p:name2="hgh"></bean>
<!--
properties
-->
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:person.properties</value>
</property>
</bean>
<bean id="person9" class="com.hgh.spring.helloworld.Person">
<property name="age">
<value>${age}</value>
</property>
<property name="city">
<value>${city}</value>
</property>
<property name="name2">
<value>${name}</value>
</property>
</bean>
</beans>
person.properties,直接放在src下
name=hgh
age=12
city=shangsi