Spring(3)--bean的xml开发

先概括的说bean相关内容,之后讨论具体应用。

有个大概了解即可。

Property Explained in…
Class Instantiating Beans
Name Naming Beans
Scope Bean Scopes
Constructor arguments Dependency Injection
Properties Dependency Injection
Autowiring mode Autowiring Collaborators
Lazy initialization mode Lazy-initialized Beans
Initialization method Initialization Callbacks
Destruction method Destruction Callbacks

bean可以创建很多的类型
这里假设工程结构是

bean的常用关键字

  • id
  • name
  • class
  • type
  • value
  • constructor-arg
  • ref
  • parent

一、构造器

无参构造

<bean id="exampleBean" class="com.pojo.Hello"></bean>
<bean name="anotherExample" class="com.pojo.Hello"/>

有参构造器

(1)根据下标顺序赋值(会模糊输入数据的类型)

<bean id="exampleBean" class="com.pojo.Hello">
    <constructor-arg index="0" value="7500000"/>
    <constructor-arg index="1" value="42"/>
</bean>

(2)根据类型赋值(多个构造器,若类型一致会有冲突)

<bean id="exampleBean" class="com.pojo.Hello">
    <constructor-arg type="int" value="7500000"/>
    <constructor-arg type="java.lang.String" value="42"/>
</bean>

(3)根据名字赋值

<bean id="exampleBean" class="com.pojo.Hello">
    <constructor-arg name="years" value="2020"/>
    <constructor-arg name="month" value="6"/>
</bean>

二、属性赋值

数值类型:int、short、long
字符串类型:string
布尔类型:boolean

在设置bean属性时,可以使用复合或嵌套属性名,只要路径的所有组件(最后的属性名除外)都不为空

<bean id="exampleBean" class="com.pojo.Hello">
    <property name="accountDao" ref="accountDao"/>
    <property name="itemDao" value="itemDao"/>
    <property name="fred.bob.sammy" value="123" />
</bean>

三、数组

bean | ref | idref | list | set | map | props | value | null

<bean id="moreComplexObject" class="example.ComplexObject">
    <!-- results in a setAdminEmails(java.util.Properties) call -->
    <property name="adminEmails">
        <props>
            <prop key="administrator">administrator@example.org</prop>
            <prop key="support">support@example.org</prop>
            <prop key="development">development@example.org</prop>
        </props>
    </property>
    <!-- results in a setSomeList(java.util.List) call -->
    <property name="someList">
        <list>
            <value>a list element followed by a reference</value>
            <ref bean="myDataSource" />
        </list>
    </property>
    <!-- results in a setSomeMap(java.util.Map) call -->
    <property name="someMap">
        <map>
            <entry key="an entry" value="just some string"/>
            <entry key ="a ref" value-ref="myDataSource"/>
        </map>
    </property>
    <!-- results in a setSomeSet(java.util.Set) call -->
    <property name="someSet">
        <set>
            <value>just some string</value>
            <ref bean="myDataSource" />
        </set>
    </property>
</bean>

四、其他操作

(1)导入其他xml文件

<beans>
    <import resource="services.xml"/>
    <import resource="resources/messageSource.xml"/>
    <import resource="/resources/themeSource.xml"/>

    <bean id="bean1" class="..."/>
    <bean id="bean2" class="..."/>
</beans>

(2)别名

<alias name="已经定义的bean的名字(需要对应)" alias="自定义的别名"/>
<alias name="myApp-dataSource" alias="subsystemB-dataSource"/>

(3)命名空间

3.1 属性property

<bean name="classic" class="com.example.ExampleBean">
    <property name="email" value="someone@somewhere.com"/>
</bean>

等价于

<bean name="p-namespace" class="com.example.ExampleBean"
     p:email="someone@somewhere.com"/>
</beans>

3.2 构造器constructor-arg

<bean id="beanOne" class="x.y.ThingOne">
    <constructor-arg name="thingTwo" ref="beanTwo"/>
    <constructor-arg name="thingThree" ref="beanThree"/>
    <constructor-arg name="email" value="something@somewhere.com"/>
</bean>

等价于

<bean id="beanOne" class="x.y.ThingOne" c:thingTwo-ref="beanTwo"
    c:thingThree-ref="beanThree" c:email="something@somewhere.com"/>
</beans>

(4)depend-on和ref

所有引用最终都是对另一个对象的引用。范围确定和验证取决于是否通过bean或父属性指定另一个对象的ID或名称。

​ 依赖其他bean,功能类似ref。主要用于处理强依赖处理。
​ 如用于数据库驱动程序注册。 依赖属性可以显式地强制初始化一个或多个使用该元素的bean之前的bean

<bean id="beanOne" class="ExampleBean" depends-on="manager"/>
<bean id="manager" class="ManagerBean" />

(5)懒初始化

​ 即用的时候再实例化,比如有些数据随时可变的,可以防止风险

<bean id="beanOne" class="ExampleBean" depends-on="manager"/>
<bean id="manager" class="ManagerBean" />

您还可以使用元素上的default-lazy-init属性来控制容器级别的延迟初始化

<beans default-lazy-init="true">
    <!-- no beans will be pre-instantiated... -->
</beans>

(6)import

导入其他bean,可以用于规范代码模块和分类,有利于管理代码

<beans>
    <import resource="services.xml"/>
    <import resource="resources/messageSource.xml"/>
    <import resource="/resources/themeSource.xml"/>

    <bean id="bean1" class="..."/>
    <bean id="bean2" class="..."/>
</beans>

(7)自动加载

自动装配可以大大减少指定属性或构造函数参数的相关代码。可以避免编写显式连接的操作,转由隐式操作。
在使用基于xml的配置元数据(请参阅依赖项注入)时,可以使用元素的autowire属性为bean定义指定自动加载模式。

<bean id="people" class="com.dao.People" autowire="byName">
    <property name="Dog" value="狗">
</bean>

对应的class文件

package com.pojo;
public class Hello {
    private Cat cat;
    private  Dog dog;
    
    public Cat getCat() {
        return cat;
    }
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
}
Mode Explanation
no 无操作
byName 会在上下文中查找,找到和自己对象setter函数后那么一致的name值,然后赋值
byType 会在上下文中查找,找到和自己对象setter函数后那么一致的类型,然后赋值
constructor 根据构造器名字查找

五、方法

(1)工厂方法

一个工厂类也可以包含一个或多个工厂方法

5.1 一个工厂类对应一个工厂方法

<bean id="clientService"
    class="examples.ClientService"
    factory-method="createInstance"/>

对应的class文件

public class ClientService {
    private static ClientService clientService = new ClientService();
    private ClientService() {}

    public static ClientService createInstance() {
        return clientService;
    }
}

5.2 一个工厂类对应多个工厂方法

<bean id="serviceLocator" class="examples.DefaultServiceLocator">
    <!-- inject any dependencies required by this locator bean -->
</bean>

<bean id="clientService"
    factory-bean="serviceLocator"
    factory-method="createClientServiceInstance"/>

<bean id="accountService"
    factory-bean="serviceLocator"
    factory-method="createAccountServiceInstance"/>
public class DefaultServiceLocator {

    private static ClientService clientService = new ClientServiceImpl();

    private static AccountService accountService = new AccountServiceImpl();

    public ClientService createClientServiceInstance() {
        return clientService;
    }

    public AccountService createAccountServiceInstance() {
        return accountService;
    }
}

(2)destroy-method

(3)lookup-method

(4)replaced-method

(5)init-method

六、Bean作用域

<bean id="people" class="com.dao.People" scope="singleton">
    <property name="Dog" value="狗">
</bean>

Scope Description
singleton
prototype
request
session
application
websocket
posted @ 2020-04-09 22:18  code906  阅读(216)  评论(0)    收藏  举报