依赖注入的方式;
1.属性注入(最常用)通过setter方法,如下面的配置文件,为springtest.HelloSpring的name属性设置数据。
<bean id="helloSpring" class = "springtest.HelloSpring">
<property name="name" value="SpringValue"></property>
</bean>
2.构造器注入()
定义Person bean,有一个构造函数,在配置文件中通过构造器注入,构建对象。
public Person(String name, int age){
super();
this.name = name;
this.age = age;
}
配置文件如下:
另外,当没有多个构造器的时候,index,name,type都可以去掉,使用默认的值即可。
<bean id="person" class="spring2.Person">
<constructor-arg name="name" type="java.lang.String" index="0" value="high"></constructor-arg><!-- 构造器第1个参数值设定 -->
<constructor-arg index="0" name="age" type="int" value="18"></constructor-arg><!-- 构造器第2个参数值设定 -->
</bean>
main函数写法:
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person)ctx.getBean("person");
System.out.println(person);
3.工厂方法注入(很少使用)
可以自己写工厂方法,然后在xml中配置后,即可让spring容器来调用自己写的工厂方法来实例化。
※xml配置文件中的特殊字符
也可以用子元素的形式来表达数据。<bean id="person2" class="spring2.Person">
<property name="name">
<value>high</value>
</property>
</bean>
在子元素中当数据是特殊符号,比如<、>时,不能在value中使用,如果想表达"<high>"则可以用 <![CDATA[<xxx>]]>
bean对象的相互引用
要实现以下功能,Person类中有一个
Car类型的成员变量,即引用类型的变量赋值,只需要使用ref属性,在xml配置文件中设置即可实现。
Person{ Car car;}
<bean id="carid" class="spring.Car"></bean>
<bean id="person" class="spring.Person">
<property name="car" ref="carid"></property>
</bean>
创建内部Bean
上面的例子是首先创建一个Car的bean,然后在person中引用已经创建好的Bean,还有一种方法是创建一个内部Bean。内部bean不需要id,即使有也无法在外部引用的到。
<bean id="person" class="spring.Person">
<property name="car">
<bean class="spring.Car"> <!-- 创建内部bean -->
<property name="carname" value="chagnan"></property>
</bean>
</property>
</bean>
<null/>
<bean id="person1" class="spring2.Person">
<constructor-arg name="name"><null/></constructor-arg>
<constructor-arg name="age" value="25"></constructor-arg>
<constructor-arg name="car"><null/></constructor-arg> //类中引用类型不赋初值的情况下默认为Null,也可以显示赋值为NULL
</bean>
级联属性
如下例子,可以通过car.carname的级联方式来设置属性值。注意car属性必须先初始化。不能为null。
<bean id="person2" class="spring2.Person">
<property name="car">
<bean class="spring2.Car"/>
</property>
<property name="car.carname" value="QQ"></property>
</bean>
集合属性
如果Person定义了一个ArrayList<Car> cars的集合,则可以像下面的方式进行赋值,该方法同样适用于Array,Set,Map等。
<bean id="person" class="spring2.Person">
<property name="cars">
<list>
<ref bean="car1"/>
<ref bean="car2"/>
<ref bean="car3"/>
</list>
</property>
</bean>
Map的例子:
<bean id="person" class="spring2.Person">
<property name="cars">
<map>
<entry key="aa" value-ref="reference type" />
<entry key="name" value="stringtype" />
</map>
</property>
</bean>
props举例:
<bean id="person" class="spring2.Person">
<property name="properties">
<props>
<prop key="dbname">mysql</prop>
<prop key="user">high</prop>
</props>
</property>
</bean>
在配置文件中使用utils包或者其他包
只需要将对应的命名空间导入后,既可以使用提供的功能,比如下面引用了util命名空间,则自己可以定义util:list,util:properties等功能。<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"
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.2.xsd">
<util:list id="lists">
<ref bean="car1"/>
<ref bean="car2"/>
</util:list>
</beans>
p命名空间
在导入p命名空间后,可以对property进行简写的方式进行配置。
<?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">
<bean id="person1" class="spring2.Person" p:name="high1"></bean>
</beans>
<完>