Spring Bean高级配置方法大全

栏目: Java · 发布时间: 6年前

内容简介:案例一:组合配置(1)枚举类型的属性(2)使用构造函数赋值

案例一:组合配置

(1)枚举类型的属性

(2)使用构造函数赋值

(3)property结点下面用bean作为value

<bean id="jacksonMapper" class="org...Jackson2ObjectMapperFactoryBean">

<property name="objectMapper">

<!-- bean标签嵌套在property标签内部 -->

<bean class="com.fasterxml.jackson.databind.ObjectMapper">

<property name="serializationInclusion">

<!-- 枚举类型值 -->

<value type="com...JsonInclude.Include">NON_EMPTY</value>

</property>

<property name="dateFormat">

<bean class="java.text.SimpleDateFormat">

<!-- 构造函数 -->

<constructor-arg type="java.lang.String" value="yyyy-MM-dd" />

</bean>

</property>

</bean>

</property>

<property name="featuresToDisable">

<array>

<!--  注入静态常量 -->

<util:constant static-field="com...DeserializationFeature.UNKNOWN_PROP" />

</array>

</property>

</bean>

包含枚举类型属性:

<property name="serializationInclusion">

<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_EMPTY</value>

</property>

public enum Include {

ALWAYS,

NON_NULL,

NON_EMPTY;

}

案例二:指定 init-method和destroy-method

<bean id="database" class="com.alibaba.druid.pool.DruidDataSource"

init-method="init" destroy-method="close">

</bean>

案例三:使用 p-schema 给属性赋值

<bean id="multipartResolver" class="org.spring..CommonsMultipartResolver" p:defaultEncoding="utf-8" >

<property name="maxUploadSize" value="52428800"/>

<property name="maxInMemorySize" value="52428800"/>

</bean>

需要声明如下:(红色部分)

<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"

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

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

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

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

xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

案例四:(1)使用 factory-bean & factory-method 构造对象

<!-- httpclient对象构建器 -->

<bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder">

</bean>

<bean id="httpClient" class="org...client.CloseableHttpClient"

factory-bean="httpClientBuilder" factory-method="build" destroy-method="close">

</bean>

案例五:使用scope=protoype定义bean,每次使用都创建新对象

<!-- 每次都会创建一个 -->

<bean id="httpClient" class="org.zollty.client.HttpClient"  scope="prototype">

</bean>

案例六:混合构造函数和setter给属性赋值

<bean class="com.zollty.lightning.push.HttpConnectionMonitorThread">

<constructor-arg index="0" ref="httpClientConnectionManager" />

<property name="connectionIdleTimeout" value="${idle.timeout}"/>

</bean>

案例七:给Properties类型的属性赋值

<bean id="exceptionResolver" class="org...SimpleMappingResolver">

<property name="exceptionMappings">

<props>

<prop key="java.lang.RuntimeException">exception/exception</prop>

<prop key="java.lang.Exception">exception/exception</prop>

</props>

</property>

</bean>

private Properties exceptionMappings;

public void setExceptionMappings(Properties mappings) {

this.exceptionMappings = mappings;

}

案例八:指定 在当前bean初始化之前,先初始化 其非直接依赖的beans

< bean id = "lifecycle" class = "org.apache.shiro.spring.LifecycleBeanPostProcessor" />

<!-- 使用depends-on,指定在当前bean初始化之前,先初始化lifecycle

    ,注意当前bean实际上并没有注入lifecycle对象 -->

< bean class = "org...DefaultAdvisorAutoProxyCreator"

   depends-on = "lifecycleBeanPostProcessor" >

  < property name = "proxyTargetClass" value = "true" />

</ bean >

备注:如果有多个非直接依赖的beans,则在depends-on可以用逗号分隔开。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Alone Together

Alone Together

Sherry Turkle / Basic Books / 2011-1-11 / USD 28.95

Consider Facebookit’s human contact, only easier to engage with and easier to avoid. Developing technology promises closeness. Sometimes it delivers, but much of our modern life leaves us less connect......一起来看看 《Alone Together》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

随机密码生成器
随机密码生成器

多种字符组合密码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码