1. 程式人生 > >【Spring】application.xml文件配置

【Spring】application.xml文件配置

framework username beans factory 管理器 ext ram nco classname

什麽是Spring

Spring是分層的javaEE full-stack(一站式)輕量級開源框架。

---註解配置--針對SSM

<?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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 導入外部資源文件

              FALLBACK   默認值,不存在時覆蓋
              NEVER    不覆蓋
              OVERRIDE  覆蓋

-->
<context:property-placeholder location="classpath:db.properties" system-properties-mode="FALLBACK"/>

<!--
註解配置掃描
-->
<!-- 掃描 service dao -->
<context:component-scan base-package="com.hp"></context:component-scan>
<!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 數據庫連接的四要素 -->
<property name="driverClassName" value="${jdbc.driverClass}"></property>
<property name="url" value="${jdbc.jdbcUrl}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 註冊事務管理器 -->
<bean id="txMgr"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 開啟事務註解驅動 -->
<tx:annotation-driven transaction-manager="txMgr" />

<!-- 配置mybatis的sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>

<!-- 配置可以整體掃描Mapper的一個掃描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hp.bookstore.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>

</beans>

db.properties數據庫連接文件內容:

jdbc.user=xxx
jdbc.password=xxx
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/xxx?useSSL=true

【Spring】application.xml文件配置