以下列举我在工程中经常使用的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