spring4 --jar包简介

如下列举我在工程中常用的spring jar包,没有列出的,以后用到了逐步列出来
1.spring-core:它包含spring框架的基本核心工具类,spring其它jar包都要使用到这个jar包里面的类。例如:asm、cglib、serializer、type、util等。
2.spring-beans:它包含访问配置文件、创建/管理bean以及ioc/di操作相关的类。常用类:beans、factory等。例如如下这段代码大家就很熟悉

<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方面的类。如下常用代码

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:它主要提供事务管理(声明式和编程式),如下为常用配置代码

//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开发的,
未完待续

转载于:https://my.oschina.net/u/2312022/blog/730240

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值