Spring(3)--bean

先概括的说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">[email protected]</prop>
            <prop key="support">[email protected]</prop>
            <prop key="development">[email protected]</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="[email protected]"/>
</bean>

等价于

<bean name="p-namespace" class="com.example.ExampleBean"
     p:email="[email protected]"/>
</beans>

3.2 构造器constructor

<bean id="beanOne" class="x.y.ThingOne">
    <constructor-arg name="thingTwo" ref="beanTwo"/>
    <constructor-arg name="thingThree" ref="beanThree"/>
    <constructor-arg name="email" value="[email protected]"/>
</bean>

等价于

<bean id="beanOne" class="x.y.ThingOne" c:thingTwo-ref="beanTwo"
    c:thingThree-ref="beanThree" c:email="[email protected]"/>
</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

<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定义指定autowire模式。

Mode Explanation
no
byName
byType
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作用域

Scope Description
singleton
prototype
request
session
application
websocket

猜你喜欢

转载自www.cnblogs.com/code906/p/12670034.html