spring學習筆記(下)

11:AOP 介紹:
	面向切面;可以攔擊方法,比如:如果用戶沒有權限就不讓用戶執行
				業務中的某些方法;
12:spring提供了兩種切面的使用方法:
	(1)基於XML配置方法進行AOP開發
	(2)基於註解方式進行AOP開發
13:spring+JDBC組合開發
	(1)配置數據源
		<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
			<property name="driverClassName" value="org.git.mm.mysql.Driver"/>
			<property name="url" 
				value="jdbc:mysql://localhost:3306/mydataBase"/>
			<property name="username" value="lid"/>
			<property name="password" value="123"/>
			<!-- 連接池啓動時的初始值-->
			<property name="initialSize" value="1"/>  
			<!-- 連接池的最大值-->
			<property name="maxActive" value="500"/>
			<!-- 最大空閒值-->
			<property name="maxIdle" value="2"/>
			<!-- 最小空閒值-->
			<property name="minIdle" value="1"/>   
		</bean>

	(2)配置事物:基於註解方式和基於XML配置方式
		基於註解:
			spring配置文件中:
				<?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: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/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">
					
						<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
							<property name="driverClassName" value="org.git.mm.mysql.Driver"/>
							<property name="url" 
								value="jdbc:mysql://localhost:3306/mydataBase"/>
							<property name="username" value="lid"/>
							<property name="password" value="123"/>
							<!-- 連接池啓動時的初始值-->
							<property name="initialSize" value="1"/>  
							<!-- 連接池的最大值-->
							<property name="maxActive" value="500"/>
							<!-- 最大空閒值-->
							<property name="maxIdle" value="2"/>
							<!-- 最小空閒值-->
							<property name="minIdle" value="1"/>   
						</bean>
						<!--事物管理器-->
						<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
							<property name="dataSource" ref="dataSource"/>
						</bean>
						<!--採用@Transactional 註解方式使用事物-->
						<tx:annotation-driven transaction-manager="txManager"/>
						
						<!-- 使用的是jdbcTemple,可換成其它的工具 -->
						<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
							<property name="dataSource">
								<ref bean="dataSource"/>
							</property>
						</bean>
					</beans>
					類:
						import javax.annotation.Resource;
						import javax.sql.DataSource;
						import org.springframework.jdbc.core.JdbcTemplate;

						public class PersonDao {
							@Resource
							private JdbcTemplate jdbcTemplate;	
						}
					可以將數據源的鏈接信息等放到一個屬性文件中,spring通過佔位符的方式從屬性文件中取得值:
					創建一個屬性文件:dataSourceConfig.properties(在裏面配置如下內容)
						driverClassName=org.git.mm.mysql.Driver
						url=jdbc:mysql://localhost:3306/mydataBase
						username=lid
						password=123
						initialSize=1  
						maxActive=500
						maxIdle=2
						minIdle=1  
					spring配置文件中這樣:
						<!-- 加載配置文件-->
						<context:property-placeholder location="classpath*:dataSourceConfig.properties"/>
						<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}"/>  
							<!-- 連接池的最大值-->
							<property name="maxActive" value="#{maxActive}"/>
							<!-- 最大空閒值-->
							<property name="maxIdle" value="#{maxIdle}"/>
							<!-- 最小空閒值-->
							<property name="minIdle" value="#{minIdle}"/>   
						</bean>
		基於XML方式配置事物:
			<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
				<property name="dataSource" ref="dataSource" />
			</bean>
			
			<aop:config>  
				<aop:advisor pointcut="execution(* service..*.*(..))"  advice-ref="txAdvice" />  
			</aop:config>  
		  
			<tx:advice id="txAdvice" transaction-manager="transactionManager">  
				<tx:attributes>  
					<tx:method name="get*" read-only="true" />  
					<tx:method name="query*" read-only="true" />  
					<tx:method name="find*" read-only="true" />  
					<tx:method name="load*" read-only="true" />
					<tx:method name="select*" read-only="true" />  
					<tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />  
				</tx:attributes>  
			</tx:advice>  
14:spring 與hibernate的整合:
	就是在spring.xml 文件中配置一下
		<!--配置sessionFactory-->
		<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
			  <property name="dataSource" ref="dataSource" />
			  <property name="mappingResources">
				  <list>
					<value>org/springframework/samples/petclinic/hibernate/petclinic.hbm.xml</value>
				  </list>
			  </property>
			  <property name="hibernateProperties">
				<value>
				  hibernate.dialect=${hibernate.dialect}
				</value>
			  </property>
		</bean>
		<!--配置事物管理(針對hibernate的事物管理器)-->
		<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
			<property name="sessionFactory" ref="sessionFactory" />
		</bean>
		<!--採用@Transactional 註解方式使用事物-->
		<tx:annotation-driven transaction-manager="txManager"/>
		
	類:
		@Transactional
		public class PersonDao {
			@Resource
			private SessionFactory sessionFactory;
			
			public void save(Person person){
				sessionFactory.getCurrentSession().persist(person);
			}
		}
15:小插曲:web系統怎麼加載spring的配置文件:
		(1)如果配置文件爲WEB-INF/ 下的applicationContext.xml
			那我們只需在web.xml中添加如下:
				<!--實例化spring,此時會默認的去WEB-INF中找applicationContext.xml-->
				<listener>
					<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
				</listener>
		(2)如果不在WEB-INF下或名字也不是applicationContext.xml,而是自己命名的,
			在src/config下的spring.xml,那麼我們可以在web.xml中這樣配置
			<context-param>
				<param-name>contextConfigLocation</param-name>
				<param-value>
					classpath*:config/spring.xml
				</param-value>
			</context-param> 
16:編碼過濾器:
	在web.xml中配置如下:
		<!-- 編碼過濾器-->
		<filter>
			<filter-name>CharacterEncodingFilter</filter-name>
			<filter-class>
				org.springframework.web.filter.CharacterEncodingFilter
			</filter-class>
			<init-param>
				<param-name>encoding</param-name>
				<param-value>UTF-8</param-value>
			</init-param>
			<init-param>
				<param-name>forceEncoding</param-name>
				<param-value>true</param-value>
			</init-param>
		</filter>

		<filter-mapping>
			<filter-name>CharacterEncodingFilter</filter-name>
			<url-pattern>/*</url-pattern>
		</filter-mapping>
17: spring+struts2:
	spring容器的實例化和struts2的啓用----就是在web.xml中進行如下的配置:
		<!-- 對Spring容器進行實例化 -->
		<listener>
			<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
		</listener>
		
		<!-- 啓動struts2框架 -->
		<filter>
			<filter-name>struts2</filter-name>
			<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
		</filter>
		<filter-mapping>
			<filter-name>struts2</filter-name>
			<url-pattern>/*</url-pattern>
		</filter-mapping>
		
		然後在類路徑下配置一個名爲struts.xml的文件:
			<?xml version="1.0" encoding="UTF-8" ?>
			<!DOCTYPE struts PUBLIC
				"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
				"http://struts.apache.org/dtds/struts-2.0.dtd">
			<struts>
				<!-- 更換掉struts2內部的對象創建工廠,action用spring創建 -->
				<constant name="struts.objectFactory" value="spring"/>
				<!-- 將struts的主題改成默認主題,防止其生成不必要的html代碼 -->
				<constant name="struts.ui.theme" value="simple"/>
				<package name="employee" namespace="/employee" extends="struts-default">
					<!-- 因爲action有spring創建,所以只需指定spring創建的action的對象的名稱就行  -->
					<action name="list" class="employeeAction">
						<result name="list">/WEB-INF/page/employee.jsp</result>
					</action>
					<action name="manage_*" class="employeeManageAction" method="{1}">
						<result name="add">/WEB-INF/page/employeeAdd.jsp</result>
						<result name="message">/WEB-INF/page/message.jsp</result>
					</action>
				</package>
			</struts>
			ps:action交給spring管理後,<action>標籤中的class就不用寫
				全路徑了(當然前提是用spring的註解標註action,這樣spring纔可以實例化action)
			
			

發佈了244 篇原創文章 · 獲贊 31 · 訪問量 39萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章