spring-config文件部分注解

参考:https://blog.csdn.net/fox_bert/article/details/80793030

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="
        http://www.springframework.org/schema/aop   
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/security   
        http://www.springframework.org/schema/security/spring-security-3.2.xsd  
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/data/jpa   
        http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd  
        http://www.springframework.org/schema/tx   
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!-- Activates annotation-based bean configuration -->
    <context:annotation-config/>

    <!-- Scans for application @Components to deploy -->
    <context:component-scan base-package="cn.cmri.pds.*"/>

    <context:property-placeholder location="classpath:/jdbc.properties,classpath:/jdbcSklk.properties"/>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
	destroy-method="close">
	<property name="driverClassName" value="${driverClassName}" />
	<property name="url" value="${url}" />
	<property name="username" value="${username}" />
	<property name="password" value="${password}" />
	<!-- 连接初始值,连接池启动时创建的连接数量的初始值 -->
	<property name="initialSize" value="${initialSize}" />
	<!-- 连接池的最大值,同一时间可以从池分配的最多连接数量,0时无限制 -->
	<property name="maxActive" value="${maxActive}" />
	<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 ,0时无限制-->
	<property name="maxIdle" value="${maxIdle}" />
	<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
	<property name="minIdle" value="${minIdle}" />
	<!-- 是否对已备语句进行池管理(布尔值),是否对PreparedStatement进行缓存 -->
	<property name="poolPreparedStatements" value="true" />
	<!-- 是否对sql进行自动提交 -->
	<property name="defaultAutoCommit" value="true" />
</bean>
</beans>  

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

注解

1、context:annotation-config用于激活那些已经在spring容器里注册过的bean上面的注解,也就是显示的向Spring注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor这四个Processor,以让系统能识别对应的注解。

如在系统使用@Autowired注解,则需在spring容器中声明AutowiredAnnotationBeanPostProcessor Bean。传统声明方式为:

<bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/>

使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor。需声明

<bean class="org.springframework.beans.factory.annotation. CommonAnnotationBeanPostProcessor"/> 

使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean

<bean class="org.springframework.beans.factory.annotation.PersistenceAnnotationBeanPostProcessor"/> 

使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean

<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> 

像@ Resource 、@ PostConstruct、@Antowired注解在自动注入比较见用,所以如果总是需要按照传统的方式一条一条配置显得有些繁琐和没有必要,于是spring给我们提供< context:annotation-config/>的简化配置方式,自动帮你完成声明。

假如我们要使用如@Component、@Controller、@Service等这些注解,单纯使用< context:annotation-config/>对上面这些注解无效

2、context:component-scan

<context:component-scan base-package="xx.xx"/>

该配置自动注入上面4个processor,因此当使用<context:component-scan/>后,就可以不用<context:annotation-config/>移除。

通过对base-package配置,就可以把controller包下 service包下 dao包下的注解全部扫描到了!

(1)< context:annotation-config />:仅能够在已经在已经注册过的bean上面起作用。对于没有在spring容器中注册的bean,它并不能执行任何操作。 


(2)< context:component-scan base-package="XX.XX"/> :除了具有上面的功能之外,还具有自动将带有@component,@service,@Repository等注解的对象注册到spring容器中的功能。 

 

思考2:如果同时使用这两个配置会不会出现重复注入的情况呢?

答案:因为< context:annotation-config />和 < context:component-scan>同时存在的时候,前者会被忽略。如@autowire,@resource等注入注解只会被注入一次!

 

3 、BasicDataSource配置文件说明

<!-- 连接初始值,连接池启动时创建的连接数量的初始值 -->
<property name="initialSize" value="${initialSize}" />

maxActive为同一时间可以从池分配的最多连接数量,0表示无限制。
<property name="maxActive" value="${jdbc.smap.maxActive}
最大空闲值,当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止。0时无限制。
<property name="maxIdle" value="${maxIdle}" />

最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 
<property name="minIdle" value="${jdbc.smap.minIdle}"/>

<!-- 是否对sql进行自动提交 -->
    <property name="defaultAutoCommit" value="true" />

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值