spring環境搭建
阿新 • • 發佈:2018-03-06
ren imp .com hbm2ddl 事務管理 bubuko 屬性文件 led 註解
1.導入jar包:
2.配置文件 — applicationContext.xml:
添加schema約束,位置:spring-framework-3.2.0.RELEASE—》docs—》spring-framework-reference—》html—》xsd-config.html
再配置service
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"applicationContext.xmlxsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置service <bean> 配置需要創建的對象 id :用於之後從spring容器獲得實例時使用的 class :需要創建實例的全限定類名 --> <bean id="userServiceId" class="com.itheima.a_ioc.UserServiceImpl"></bean> </beans>
其次,裏面可以配置加載jdbc屬性文件、數據源、spring框架用於整合Hibernate的工廠bean、事務管理器、組件掃描、引入註解解析器、 事務註解 等。如:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"applicationContext.xmlxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 加載jdbc屬性文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 數據源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${driverClass}"/> <property name="jdbcUrl" value="${jdbcUrl}"/> <property name="user" value="${user}"/> <property name="password" value="${password}"/> </bean> <!-- spring框架用於整合Hibernate的工廠bean --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 註入Hibernate相關的屬性 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- 註入Hibernate的映射文件 --> <property name="mappingDirectoryLocations"> <list> <value>classpath:com/itheima/bos/domain</value> </list> </property> </bean> <!-- 事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 組件掃描 --> <context:component-scan base-package="com.itheima.bos"/> <!-- 引入註解解析器 --> <context:annotation-config/> <!-- 事務註解 --> <tx:annotation-driven /> </beans>
spring環境搭建