p命名空間注入
需要引入xmlns:p="http://www.springframework.org/schema/p"
p命名空間注入的特點是使用屬性
而不是子元素的形式配置Bean的屬性,從而簡化了配置代碼。
<bean name="person" class="com.Person">
<property name="name" value="tom"/>
<property name="spouse" ref="jane"/>
</bean>
<bean name="john-modern"
class="com.example.Person"
p:name="tom"
p:spouse-ref="jane"/>
bean標簽
-
id屬性:用於指定Bean的名稱,在Bean被依賴時使用,在獲取Bean時使用等
-
name屬性:用於指定Bean的別名
-
class屬性:用於指定Bean的來源,即創建要創建的Bean的class類(需要全限定名)
-
singleton屬性:用於指定當前Bean的創建模式,若值為true表示為單例模式,false表示原型模式(prototype)
-
depends-on屬性:用於指定當前Bean的依賴Bean,強制指定的Bean在當前Bean初始化之前先完成初始化
-
init-method屬性:用於指定當前Bean的初始化方法,在Bean實例創建好后,首先會調用其指定名稱的方法
-
destory-method屬性:用於指定當前Bean的銷毀方法,在Bean即將被銷毀之前會自動調用該屬性指定的方法
-
lazy-init屬性:用於指定當前Bean的初始化時間,若值為true表示在初次調用時才會自動創建實例並初始化,false表示在IoC容器創建的時候就會完成創建和初始化
-
autowire屬性:用於指定當前Bean的依賴關系的自動注入方式,其有五個值:
-
byName值:表示通過id名稱來自動匹配;
-
byType值:表示通過class指定的類型來自動裝配;
-
constructor值:表示使用構造函數的參數進行自動裝配(參數的類型匹配);
-
autodetect值:表示自動進行選擇匹配方式,首先進行constructor自動裝配,若不存在構造方法則使用byType方式進行自動裝配;
-
no值:表示不適用自動裝配。
-
-
dependency-check屬性:用於指定Bean的依賴檢查模式,檢查依賴關系是否完整,與自動裝配合用,其有四個值:
-
simple值:表示針對基本類型、字符串、集合進行依賴檢查
-
object值:表示對引用對象進行依賴檢查
-
all值:表示對基本類型、字符串、集合、引用對象全部進行依賴檢查
-
none值:表示不進行任何依賴檢查,默認情況。
-
配置詳情
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 加載配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 自動掃描web包 ,將帶有注解的類納入spring容器管理 -->
<!--Spring 容器初始化的時候,會掃描 com.web 下標有
(@Component,@Service,@Controller,@Repository) 注解的類,納入spring容器管理-->
<context:component-scan base-package="com.web"></context:component-scan>
<!-- dataSource 配置 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 基本屬性 url、user、password -->
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="${jdbcUrl}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!-- 配置初始化大小 -->
<property name="initialSize" value="1"/>
<!-- 連接池最小空閑 -->
<property name="minIdle" value="1"/>
<!-- 連接池最大使用連接數量 -->
<property name="maxActive" value="20"/>
<!-- 配置獲取連接等待超時的時間 -->
<property name="maxWait" value="60000"/>
<!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<!-- 配置一個連接在池中最小生存的時間,單位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000"/>
</bean>
<!--使用Spring+MyBatis的環境下,我們需要配值一個SqlSessionFactoryBean來充當SqlSessionFactory
在基本的MyBatis中,SqlSessionFactory可以使用SqlSessionFactoryBuilder來創建,
而在mybatis-spring中,則使用SqlSessionFactoryBean來創建。-->
<!-- mybatis文件配置,掃描所有mapper文件 -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSource"
p:configLocation="classpath:mybatis-config.xml"
p:typeAliasesPackage="com.entity"
<!-- 如果 MyBatis 映射器 XML 文件在和映射器類相同的路徑下不存在,那么另外一個需要配置文件的原因就是它了。 -->
p:mapperLocations="classpath*:mapper/*.xml"/>
<!-- spring與mybatis整合配置,自動掃描所有dao ,將dao接口生成代理注入到Spring-->
<!-- MapperScannerConfigurer 的作用是取代手動添加 Mapper ,自動掃描完成接口代理。
而不需要再在mybatis-config.xml里面去逐一配置mappers。 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:basePackage="com.dao"
p:sqlSessionFactoryBeanName="sqlSessionFactory"/>
<!-- 對dataSource 數據源進行事務管理 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource"/>
<!-- 配置AOP通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 配置事務屬性 -->
<tx:attributes>
<!-- 添加事務管理的方法 -->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="select*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置一個切面AOP -->
<aop:config>
<aop:aspect id="helloWorldAspect" ref="txAdvice">
<!-- 配置切點 -->
<aop:pointcut id="pointcut" expression="execution(* com.aop.*.*(..))"/>
<!-- 配置前置通知 -->
<aop:before pointcut-ref="pointcut" method="beforeAdvice"/>
<!-- 配置前置通知 -->
<aop:after pointcut-ref="pointcut" method="afterAdvice"/>
<!-- 配置后置返回通知 -->
<aop:after-returning pointcut-ref="pointcut" method="afterReturnAdvice" returning="result"/>
<!-- 配置環繞通知 -->
<aop:around pointcut-ref="pointcut" method="aroundAdvice"/>
<!-- 異常通知 -->
<aop:after-throwing pointcut-ref="pointcut" method="throwingAdvice" throwing="e"/>
</aop:aspect>
</aop:config>
<!-- 配置使Spring采用CGLIB代理 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 啟用對事務注解的支持 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>