spring详解之配置详解(二)

一 配置Bean元素

<bean name="user" class="cn.hd.test.User" id="user" scope="prototype"></bean>

ApplicationContext.xml文件中配置bean元素,属性解释

name:属性,自己起个名字,没有限制

class:类的相对路径

id:命名方式唯一,里面不能出现特殊字符,比如“/*action”就不可以,也可以不写

scope:对象的创建方式,里面有两个属性单例模式(singleton)和多例模式(prototype),默认为单例模式

单例模式和多例模式会在另一边博客上详细解释

二spring创建对象的方式

    

(1)无参的构造函数创建

public User() {
    System.out.println("调用了无参构造函数");
}

(2)静态工厂实例化

public class UserFactory {
    //静态工厂创建
    
private static User createUser(){
        System.out.println("静态工厂创建");
        return new User();
    }

applicationContext.xml配置文件

<bean name="userFactory" class="cn.hd.test.UserFactory" factory-method="createUser"></bean>

并且创建一个UserFactory该类中提供一个静态的createUser方法

(3)实例化工厂创建

//实例化工厂创建
public User createUser1(){
    System.out.println("实例化工厂创建方式");
    return new User();
}

<bean name="userFactory1" class="cn.hd.test.UserFactory"></bean>
<bean name="user1" factory-bean="userFactory1" factory-method="createUser1"></bean>

三对象属性的注入

(1)set注入

<bean name="car" class="cn.hd.injection.Car">
    <property name="name" value="法拉利" ></property>
    <property name="color" value="red"></property>
</bean>

name为类属性名  value属性值

基本属性有8大数据类型+String,引入属性  对象

<bean name="user" class="cn.hd.injection.User">
    <property name="name" value="张三"></property>
    <property name="age" value="20"></property>
    <property name="address" value="华点"></property>
    <property name="car" ref="car"></property>
</bean>

name为类的属性值,value为属性值 ref为引用的bean类的name

(2)构造函数注入


<bean name="user4" class="cn.hd.injection.User">
    <constructor-arg name="name" value="20" type="java.lang.Integer"></constructor-arg>
    <constructor-arg name="age" value="18" index="0" type="java.lang.Integer"></constructor-arg>
</bean>

Name value(ref)  index  type

属性 具体的值 参数的位置 参数的类型

(3)p空间注入(最简单)但是业界不认可可以自己开发用,大部分人还是使用<property>

<bean name="user5" class="cn.hd.injection.User"
      p:age="12" p:name="君莫邪" p:car-ref="car" p:address="*"
>
</bean>

用p空间注入还必须导入约束,不然会报错



xmlns:p="http://www.springframework.org/schema/p"

http://www.springframework.org/schema/p

(4)spel注入(这个注入有点类似于El和OGNL表达式)


<bean name="user6" class="cn.hd.injection.User">
    <property name="name" value="#{user5.name}"></property>
    <property name="age" value="#{user.age}"></property>
    <property name="car" value="#{car}"></property>
</bean>

(5)复杂类型的注入

数组(List/set/map/properties)等类型注入,先来看第一个注入方式

①数组注入

<bean name="collectionBean" class="cn.hd.injection.CollectionBean">
    <!--数组中的元素只有一个,那么他的配置和基本类型一样-->
    <!--<property name="arr" value=""></property>-->

如果有多个用array包裹,如果有引用用ref
    
<property name="arr">
        <array>
            <value>莫邪</value>
            <value>君莫邪</value>
            <ref bean="user"></ref>
        </array>
    </property>
</bean>

@Test
public void fun(){
    //读取文件配置
    
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("cn/hd/injection/applicationContext.xml");

CollectionBean collectionBean = (CollectionBean) ac.getBean("collectionBean");
Object[]arr=collectionBean.getArr();
for (Object o:arr) {
    System.out.println(o);
}

至于结果你们可以自己演示一下,上面是applicationContext.xml文件配置,以及测试代码

②list注入

<!--<property name="list" value="1"></property>-->
<property name="list">
    <list>
        <value>莫邪</value>
        <value>君莫邪</value>
        <ref bean="user"></ref>
    </list>
</property>

CollectionBean collectionBean1 = (CollectionBean) ac.getBean("collectionBean");
List list=collectionBean1.getList();
for (Object o:list) {
    System.out.println(o);
}

③map注入

map注入是以键值对key key-ref(引入方式) value value-ref   方式注入

<property name="map">
    <map>
        <entry key="name" value="莫邪"></entry>
        <entry key="user" value-ref="user"></entry>
        <!--<entry key-ref="user"></entry>-->
    
</map>
</property>

 

CollectionBean collectionBean2 = (CollectionBean) ac.getBean("collectionBean");
Map map=collectionBean2.getMap();
String name = (String) map.get("name");
System.out.println(name);
User user = (User) map.get("user");
System.out.println(user);

④properties注入

<property name="properties">
    <props>
        <prop key="driverClass">com.mysql...</prop>
        <prop key="jdbcUrl">jdbc://loclhost:3306/..</prop>
        <prop key="user">root</prop>
        <prop key="password">1234</prop>
    </props>
</property>

 

CollectionBean collectionBean3 = (CollectionBean) ac.getBean("collectionBean");
Properties properties = collectionBean3.getProperties();
String name1 = (String) properties.get("driverClass");
System.out.println(name1);
String name2 = (String) properties.get("jdbcUrl");
System.out.println(name2);
String name3 = (String) properties.get("user");
System.out.println(name3);
String name4 = (String) properties.get("password");
System.out.println(name4);

以上的注入方式都是可以测试的,下面是测试的结果不完全

spring详解之配置详解(二)

注入方式说完就给大家说一个模块化小知识,很简单的就是在在applicationContext.xml文件导入其他类的applicationContext.xml文件。就相当于struts2文件。就是直接在applicationContext.xml文件中添加以下代码就可以了

<import resource="classpath:applicationContext.xml" ></import>

其中resource后面的xml就是其他配置文件的地址