spring4 --jar包簡介

以下列舉我在工程中經常使用的spring jar包,沒有列出的,之後用到了逐步列出來
1.spring-core:它包含spring框架的基本核心工具類,spring其它jar包都要使用到這個jar包裏面的類。例如:asm、cglib、serializer、type、util等。
2.spring-beans:它包含訪問配置文件、建立/管理bean以及ioc/di操做相關的類。經常使用類:beans、factory等。例如以下這段代碼你們就很熟悉mysql

<bean id="propertyConfigurer"    
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">   
         <property name="location" value="classpath:db.properties"/>   
</bean>
<bean id="propertiesReader" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
	<property name="locations">
		<value>classpath:resourceConfig.properties</value>
        </property>
</bean>

3.spring-context:它爲spring核心提供了大量的擴展類,包括使用spring ApplicationContext特性時所需的所有類,cache、jndi、validation方面的類。以下經常使用代碼web

package com.test.spring.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

	public static void main(String[] args) {
//		HelloWorld helloworld = new HelloWorld();
//		helloworld.setName("test");
//		helloworld.helloworld();
		
		//1.建立spring ioc容器對象
		ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml");
		//2.從ioc容器中獲取bean實例,即上述配置文件中bean 的 id
		HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
		helloWorld.helloworld();
	}
}

4.spring-expression:它與表達式相關,包括基本表達式(數學、關係、邏輯、正則)、類相關表達式、集合表達式、模板表達式。 5.spring-aop:它是面向切面編程的簡稱,包括aop、aspectj、config、intercepter、target等類。aop中相關術語切面(Aspect)、鏈接點(Joinpoint)、通知(Advice)、切入點(Pointcut)、引入(Introduction)、目標(Target)、代理(proxy)、織入(Weaving)等。spring提供了4種AOP方式
1>基於代理的aop
2>基於@AspectJ註解驅動
3>純POJO切面
4>注入式aspect切面
6.spring-tx:它主要提供事務管理(聲明式和編程式),以下爲經常使用配置代碼spring

//db.properties文件
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/testdb
username=admin
password=123456
//加載db配置文件
<context:property-placeholder location="classpath:dbconfig.properties"/>
//配置datasource
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClass" value="${driverClassName}"/>
        <property name="jdbcUrl" value="${url}"/>
        <property name="user" value="${username}"/>
        <property name="password" value="${password}"/>
    </bean>
//配置工廠
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
                <!-- xx.xml文件-->
		<property name="mapperLocations" value="classpath:com/readygo/missBang/mapper/*.xml" />
	</bean>
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
                <!--xx.mapper文件-->
		<property name="basePackage" value="com.readygo.missBang.mapper" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>
//配置事務
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
<!-- 使用註解spring事務 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
spring 默認捕獲的異常是運行時異常
    @Transactional              //聲明一個事務
    @Transactional(rollbackFor=Exception.class)         //表示就是Exception異常也要回滾事務
    @Transactional(noRollbackFor=RuntimeException.class)     //表示就是運行時異常也不回滾事務
    @Transactional(propagation=Propagation.NOT_SUPPORTED);      //聲明方法不須要事務
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			
			<tx:method name="get*" propagation="SUPPORTS" />
			<tx:method name="*" propagation="SUPPORTS" />
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut expression="execution(* com.readygo.missBang.service.impl.*Impl.*(..))" id="transactionPointcut"/>
		<aop:advisor advice-ref="transactionAdvice" pointcut-ref="transactionPointcut"/>
	</aop:config>

7.spring-web:它主要是針對web開發的,
未完待續sql

相關文章
相關標籤/搜索