首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 互联网 >

spring 小结

2012-11-23 
spring 总结??/------------------------------------------------\| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

spring 总结

?

?

/------------------------------------------------\

| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?A类-精华 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|

\------------------------------------------------/

1、IOC

? ? ? ? 1, 自动扫描bean

? ? ? ? ? ? ? ? IOC用法--只会注入属性,不会把bean纳入spring的管理范围

? ? ? ? ? ? ? ? xml配置上百个bean节点时,很难维护。

? ? ? ? ? ? ? ? spring2.5引入了bean自动扫描机制(自动把bean纳入spring的管理范围)

? ? ? ? ? ? ? ? ? ? ? ? @Component(泛指),@Service,@Controller(action),@Repository(dao组件,与数据库相关的)注解的类,

?

? ? ? ? ? ? ? ? ? ? ? ? <context:component-scan base-package="cn.itest"/>//需要自动扫描的包//引入context命名空间

? ? ? ? ? ? ? ? ? ? ? ? @Service("personService")?

? ? ? ? ? ? ? ? ? ? ? ? public class PersonServiceBean implements PersonService {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @Resource //Field//用注解--------------------------------//

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //等效于@Resource(name="pesronDao")

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? private PersonDao personDao;

?

? ? ? ? ? ? ? ? ? ? ? ? <context:component-scan base-package="cn.itcast"/>

? ? ? ? ? ? ? ? ? ? ? ? //它已包含 <context:annotation-config/> ,所以不需要在导入<context:annotation-config了

?

? ? ? ? ? ? ? ? ? ? ? ? <context:annotation-config/>

? ? ? ? ? ? ? ? ? ? ? ? //annotation-config隐式注册了CommonAnnotationBeanPostProcessor等

?

?

?

3, spring声明式事务

? ? ? ? 1、tx标签拦截器-------------------方法级别的-------------------

? ? ? ? //<aop:pointcut/><tx:advice/><aop:advisor/>元素都可以是多个,就可以实现方法级别了

? ? ? ? <tx:advice id="txAdvice" transaction-manager="transactionManager">

? ? ? ? ? ? ? ? <tx:attributes>

? ? ? ? ? ? ? ? ? ? ? ? <tx:method name="*" propagation="REQUIRED" />

? ? ? ? ? ? ? ? </tx:attributes>

? ? ? ? </tx:advice>

? ? ? ? <aop:config>

? ? ? ? ? ? ? ? <aop:pointcut id="interceptorPointCuts" expression="execution(* com.bluesky.spring.dao.*.*(..))" />

? ? ? ? ? ? ? ? <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />?

? ? ? ? </aop:config>?

?

? ? ? ? 2、全注解-------------------方法级别的-------------------spring官方推荐

? ? ? ? <context:annotation-config />

? ? ? ? <context:component-scan base-package="com.bluesky"/>//它已包含 <context:annotation-config/>

? ? ? ? <tx:annotation-driven transaction-manager="transactionManager"/>

?

? ? ? ? java代码中

? ? ? ? ? ? ? ? @Transactional

? ? ? ? ? ? ? ? @Component("userDao")

? ? ? ? ? ? ? ? public class UserDaoImpl extends HibernateDaoSupport{

? ? ? ? ? ? ? ? ? ? ? ? @Transactional(isolation = Isolation.READ_COMMITTED) ?

? ? ? ? ? ? ? ? ? ? ? ? public void test1(){...}

? ? ? ? ? ? ? ? }

?

? ? ? ? @Transactional ?的所有可选属性如下:

? ? ? ? ? ? ? ? propagation

? ? ? ? ? ? ? ? isolation

? ? ? ? ? ? ? ? readOnly:使用Hibernate时,它告知Hibernate不要在只读事务中检测和刷新变化?

? ? ? ? ? ? ? ? rollbackFor或“-”

? ? ? ? ? ? ? ? noRollbackFor或“+”

? ? ? ? Spring默认:(RuntimeException)回滚,checked异常提交。

?

? ? ? ? DB并发问题: ? ? ? ?

? ? ? ? ? ? ? ? 查询MysqL默认隔离级别语句:select @@tx_isolation;

? ? ? ? ? ? ? ? 修改MysqL默认隔离级别语句:

? ? ? ? ? ? ? ? ? ? ? ? set transaction isolation level read uncommitted;

? ? ? ? ? ? ? ? 修改后,给delete设置断点,就可以看到dirty read了

? ? ? ? ? ? ? ? ? ? ? ? public void delete(){

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? jdbcTemplate.update("delete from person where id=8");

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? jdbcTemplate.update("delete from persons where id=9");

? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? 可见,SQL提交后,就已经

? ? ? ? ? ? ? ? 恢复默认级别:

? ? ? ? ? ? ? ? ? ? ? ? set transaction isolation level Repeatable Read ? ? ? ?

? ? ? ? ? ? ? ? 隔离级别:

? ? ? ? ? ? ? ? ? ? ? ? Read Uncommited:不提交读

? ? ? ? ? ? ? ? ? ? ? ? Read Commited(Oracle默认):提交读-不可以“重复读”

? ? ? ? ? ? ? ? ? ? ? ? Repeatable Read(MysqL默认)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 可(以)“重复读”:一个事务内,多次(有时,有这种需要)读同一数据时,结果应该一致。

? ? ? ? ? ? ? ? ? ? ? ? Serialiazble:序列化读

? ? ? ? ? ? ? ? 问题:

? ? ? ? ? ? ? ? ? ? ? ? 1. 脏读 :事务2读到了事务1修改后的数据(这个数据可能被回滚,也可能被提交)?

? ? ? ? ? ? ? ? ? ? ? ? 2. 不可“重复读” :一个事务内,多次(有时,有这种需要)读同一数据时,结果应该一致。

?

?

? ? ? ? spring可以管理事务的原因:

? ? ? ? ? ? ? ? Spring管理DB,Sevice调用多个Dao(DML操作),都会先保存在同一Session中

? ? ? ? ? ? ? ? 发生异常时,Spring把这个Session中的DML操作回滚。

?

?

AOP概念:Aspect,Joinpoint,Advice,Pointcut,introduction,TargetObject,AOPProxy,weaving等。

spring-AOP概念

? ? ? ? AOP有三个最核心的概念:Advice、Pointcut和Advisor。

? ? ? ? ? ? ? ? Advice是你想【向其他程序内部不同地方】注入的代码。

? ? ? ? ? ? ? ? Pointcut定义了(需要注入Advice的)位置(通常是某个特定的类的一个public方法),

? ? ? ? ? ? ? ? Advisor是Pointcut和Advice的装配器,是将Advice注入程序中预定义位置的代码。

?

3,基础

? ? ? ? src:为类路径

? ? ? ? 类路径实例化spring容器

? ? ? ? ? ? ? ? ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"beans.xml"});

? ? ? ? spring的规范

? ? ? ? ? ? ? ? bean命名==变量命名

? ? ? ? ? ? ? ? List users,不要List userList

? ? ? ? ? ? ? ? 属性用封装类,局部变量用int

? ? ? ? ? ? ? ? 接口中加方法注释,实现类中不用加

?

? ? ? ? 注解

? ? ? ? ? ? ? ? 不是耦合,因为它只改变了一个类

? ? ? ? ? ? ? ? 很少改动的用注解,因为要编译;经常改变的用xml配置

?

? ? ? ? 轻量级重量级框架区别

? ? ? ? ? ? ? ? 容器使用的服务少-轻量级,多时spring就变为重量级了

?

? ? ? ? spring的作用范围

? ? ? ? ? ? ? ? 1,signleton,prototype

? ? ? ? ? ? ? ? 2,web下request,session

? ? ? ? spring默认管理bean

? ? ? ? ? ? ? ? signleton,判断bean1==bean2

? ? ? ? ? ? ? ? 启动时实例化bean,在无参构造中System.out

? ? ? ? ? ? ? ? 生命周期,至spring容器结束

?

IOC

? ? ? ? PersonService中

? ? ? ? ? ? ? ? PersonDao pDao=new PersonDao()//pDao在service的内部被创建及维护

?

? ? ? ? IOC后,只需要声明一下PersonDao

?

IOC用法--只会注入属性,不会把bean纳入spring的管理范围

? ? ? ? 1,set对各种类型的装配

? ? ? ? ? ? ? ? 1,单个属性value

? ? ? ? ? ? ? ? 2,各种集合set,list,map,properties[]

? ? ? ? 2,Field//用注解--------推荐

? ? ? ? ? ? ? ? @Resource默认按名称(id)装配,找不到在按类型

? ? ? ? ? ? ? ? @Autowired按类型进行装配//不推荐@Autowired

? ? ? ? 3,构造

?

? ? ? ? 4,spring2.5引入了组件自动扫描机制(其实是Field方式)

? ? ? ? ? ? ? ? xml配置上百个bean节点时,很难维护。

? ? ? ? ? ? ? ? 从类路径下寻找标注了@Component(泛指),@Service,@Controller(action),@Repository(dao组件,与数据库相关的)注解的类,

? ? ? ? ? ? ? ? 并把这些类纳入到spring容器中管理

?

? ? ? ? ? ? ? ? 引入context命名空间

? ? ? ? ? ? ? ? <context:component-scan base-package="cn.itest"/>//需要自动扫描的包

? ? ? ? ? ? ? ? @Service("personService")?

? ? ? ? ? ? ? ? public class PersonServiceBean implements PersonService {

? ? ? ? ? ? ? ? ? ? ? ? @Resource //Field//用注解--------------------------------//

? ? ? ? ? ? ? ? ? ? ? ? //等效于@Resource(name="pesronDao")

? ? ? ? ? ? ? ? ? ? ? ? private PersonDao personDao;

?

? ? ? ? ? ? ? ? <context:component-scan base-package="cn.itcast"/>

? ? ? ? ? ? ? ? //它已包含 <context:annotation-config/> ,所以不需要在导入<context:annotation-config了

?

? ? ? ? ? ? ? ? <context:annotation-config/>

? ? ? ? ? ? ? ? //annotation-config隐式注册了CommonAnnotationBeanPostProcessor等

? ? ? ? ? ? ? ? //用于注入(IOC 只会注入属性,不会把bean纳入spring的管理范围),

?

?

?

?

?

?

?

<====================== 3,spring-AOP====================== > ? ? ? ?

1,注解方式:需要<aop:aspectj-autoproxy/>//"aop:"说明需要xmlns:aop

? ? ? ? @Aspect?

? ? ? ? public class MyInterceptor0 {

? ? ? ? ? ? ? ? @Pointcut("execution (* cn.itcast.service.impl.PersonServiceBean0.*(..))")

? ? ? ? ? ? ? ? private void anyMethod(){}//声明一个pointcut

?

? ? ? ? ? ? ? ? @Before("anyMethod() ?&& args(name)")//增加pointcut参数限制

? ? ? ? ? ? ? ? public void doAccessCheck(String name){

? ? ? ? ? ? ? ? ? ? ? ? System.out.println("before advice:"+name);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? @AfterReturning(pointcut="anyMethod()",returning="result")

? ? ? ? ? ? ? ? public void doAfterReturning(String result){

? ? ? ? ? ? ? ? ? ? ? ? System.out.println("after advice:"+result);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? @After("anyMethod()")//最终通知finally

? ? ? ? ? ? ? ? public void doAfter(){

? ? ? ? ? ? ? ? ? ? ? ? System.out.println("after finally advice");

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? @AfterThrowing(pointcut="anyMethod()",throwing="e")

? ? ? ? ? ? ? ? public void doAfterThrowing(Exception e){

? ? ? ? ? ? ? ? ? ? ? ? System.out.println("after throw advice:"+e);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? @Around("anyMethod()")

? ? ? ? ? ? ? ? public Object doAround(ProceedingJoinPoint pjp)throws Throwable{

? ? ? ? ? ? ? ? ? ? ? ? System.out.println("进入 around");

? ? ? ? ? ? ? ? ? ? ? ? Object result=pjp.proceed();

? ? ? ? ? ? ? ? ? ? ? ? System.out.println("退出 around");

? ? ? ? ? ? ? ? ? ? ? ? return result;

? ? ? ? ? ? ? ? }

? ? ? ? }

? ? ? ? <aop:aspectj-autoproxy/>

? ? ? ? <bean id="myInterceptor0" ref="aspectBean">

? ? ? ? ? ? ? ? ? ? ? ? <aop:pointcut id="mycut" expression="execution(* cn.itcast.service.impl.PersonServiceBean.*(java.lang.String,..))"/>

? ? ? ? ? ? ? ? ? ? ? ? <aop:before pointcut-ref="mycut" method="doAccessCheck"/>

? ? ? ? ? ? ? ? ? ? ? ? <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>

? ? ? ? ? ? ? ? ? ? ? ? <aop:after-throwing pointcut-ref="mycut" method="doAfter"/>

? ? ? ? ? ? ? ? ? ? ? ? <aop:after pointcut-ref="mycut" method="doAfterThrowing"/>

? ? ? ? ? ? ? ? ? ? ? ? <aop:around pointcut-ref="mycut" method="doAround"/>

? ? ? ? ? ? ? ? </aop:aspect>

? ? ? ? </aop:config>

?

? ? ? ? MyInterceptor类,去掉所有的注解

?

?

<====================== 4,spring-Hibernate-JDBC====================== >

spring-Hibernate

? ? ? ? UserDao extends HibernateDaoSupport,后,就不用setSessionFactory

? ? ? ? 但xml里还是要的

?

spring-JDBC

? ? ? ? userDao需要

? ? ? ? ? ? ? ? <property name="dataSource" ref="dataSource"/>

?

? ? ? ? ? ? ? ? private JdbcTemplate jdbcTemplate;

? ? ? ? ? ? ? ? public void setDataSource(DataSource dataSource) {

? ? ? ? ? ? ? ? ? ? ? ? this.jdbcTemplate =new JdbcTemplate(dataSource);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? //IOC不需要属性,只需要set方法

? ? ? ? ? ? ? ? 不需要private DataSource dataSource,有setDataSource就可以了

?

?

?

/------------------------------------------------\

| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?B类-所有 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|

\------------------------------------------------/

?

?

?

?

?

?

?

<====================== 1,声明式事务====================== >

###:方法1,2,3已过时,方法4,5主流

?

1,代理模式-------------------类级别的-------------------但是要写很多的代理

? ? ? ? ? <!-- 每个要加事务的service对应一个 *ServiceProxyBean-->

? ? ? ? <bean id="rightService".../>

? ? ? ? <bean id="rightServiceProxyBean" value="com.bluesky.spring.dao.GeneratorDao" />//可有可无

? ? ? ? ? ? ? ? <property name="transactionAttributes"> ? ? ? ? ? ? ? ? ? ? ? ?//配置事务属性

? ? ? ? ? ? ? ? ? ? ? ? <props>

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <prop key="find*">PROPAGATION_REQUIRED, readOnly</prop>

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <prop key="add*">PROPAGATION_REQUIRED, -AddOrModifyException</prop>

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <prop key="*">PROPAGATION_REQUIRED</prop>

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //还有隔离级别没设

? ? ? ? ? ? ? ? ? ? ? ? </props>

? ? ? ? ? ? ? ? </property>

? ? ? ? </bean>

?

? ? ? ? 然后再(ssh1下)被action调用

? ? ? ? <bean name="/role/roleadd" ?

? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? lazy-init="true" abstract="true">?

? ? ? ? ? ? ? ? ...把<property name="target">拿出来,其他不变

? ? ? ? </bean>

? ? ? ? <bean id="rightServiceProxyBean" ?parent="transactionBase" > ?

? ? ? ? ? ? ? ? <property name="target" ref="rightService" /> ??

? ? ? ? ? ?</bean>

?

3,拦截器----------------------类级别的-------------------

? ? ? ? <bean id="transactionInterceptor" ?

? ? ? ? ? ? ? ? ref="transactionManager" /> ? ? ? ? ?//配置事务管理器de实现类

? ? ? ? ? ? ? ? <property name="transactionAttributes"> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// 配置事务属性

? ? ? ? ? ? ? ? ? ? ? ? <props> ?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <prop key="*">PROPAGATION_REQUIRED</prop> ?

? ? ? ? ? ? ? ? ? ? ? ? </props> ?

? ? ? ? ? ? ? ? </property> ?

? ? ? ? </bean>

?

? ? ? ? <bean transaction-manager="transactionManager">

? ? ? ? ? ? ? ? <tx:attributes>

? ? ? ? ? ? ? ? ? ? ? ? <tx:method name="*" propagation="REQUIRED" />

? ? ? ? ? ? ? ? </tx:attributes>

? ? ? ? </tx:advice>

? ? ? ? <aop:config>

? ? ? ? ? ? ? ? <aop:pointcut id="interceptorPointCuts" expression="execution(* com.bluesky.spring.dao.*.*(..))" />

? ? ? ? ? ? ? ? <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />?

? ? ? ? </aop:config>?

?

5,全注解-------------------方法别的-------------------spring官方推荐

? ? ? ? <context:annotation-config />

? ? ? ? <context:component-scan base-package="com.bluesky" />

? ? ? ? <tx:annotation-driven transaction-manager="transactionManager"/>

?

? ? ? ? java代码中

? ? ? ? ? ? ? ? @Transactional

? ? ? ? ? ? ? ? @Component("userDao")

? ? ? ? ? ? ? ? public class UserDaoImpl extends HibernateDaoSupport{

? ? ? ? ? ? ? ? ? ? ? ? @Transactional(isolation = Isolation.READ_COMMITTED) ?

? ? ? ? ? ? ? ? ? ? ? ? public void test1(){...}

? ? ? ? ? ? ? ? }

?

? ? ? ? @Transactional ?的所有可选属性如下:

? ? ? ? ? ? ? ? propagation

? ? ? ? ? ? ? ? isolation

? ? ? ? ? ? ? ? readOnly:使用Hibernate时,它告知Hibernate不要在只读事务中检测和刷新变化?

? ? ? ? ? ? ? ? rollbackFor或“-”

? ? ? ? ? ? ? ? noRollbackFor或“+”

? ? ? ? Spring默认:(RuntimeException)回滚,checked异常提交。

?

? ? ? ? DB并发问题: ? ? ? ?

? ? ? ? ? ? ? ? 查询MysqL默认隔离级别语句:select @@tx_isolation;

? ? ? ? ? ? ? ? 修改MysqL默认隔离级别语句:

? ? ? ? ? ? ? ? ? ? ? ? set transaction isolation level read uncommitted;

? ? ? ? ? ? ? ? 修改后,给delete设置断点,就可以看到dirty read了

? ? ? ? ? ? ? ? ? ? ? ? public void delete(){

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? jdbcTemplate.update("delete from person where id=8");

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? jdbcTemplate.update("delete from persons where id=9");

? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? 可见,SQL提交后,就已经

? ? ? ? ? ? ? ? 恢复默认级别:

? ? ? ? ? ? ? ? ? ? ? ? set transaction isolation level Repeatable Read ? ? ? ?

? ? ? ? ? ? ? ? 隔离级别:

? ? ? ? ? ? ? ? ? ? ? ? Read Uncommited:不提交读

? ? ? ? ? ? ? ? ? ? ? ? Read Commited(Oracle默认):提交读-不可以“重复读”

? ? ? ? ? ? ? ? ? ? ? ? Repeatable Read(MysqL默认)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 可(以)“重复读”:一个事务内,多次(有时,有这种需要)读同一数据时,结果应该一致。

? ? ? ? ? ? ? ? ? ? ? ? Serialiazble:序列化读

? ? ? ? ? ? ? ? 问题:

? ? ? ? ? ? ? ? ? ? ? ? 1. 脏读 :事务2读到了事务1修改后的数据(这个数据可能被回滚,也可能被提交)?

? ? ? ? ? ? ? ? ? ? ? ? 2. 不可“重复读” :一个事务内,多次(有时,有这种需要)读同一数据时,结果应该一致。

?

?

? ? ? ? spring可以管理事务的原因:

? ? ? ? ? ? ? ? Spring管理DB,Sevice调用多个Dao(DML操作),都会先保存在同一Session中

? ? ? ? ? ? ? ? 发生异常时,Spring把这个Session中的DML操作回滚。

?

?

<====================== 2,spring-公共基础====================== > ? ? ? ?

1,通用的AOP术语,这并不直观。如果Spring使用它自己的术语,那么就更显得混淆。

? ? ? ? Aspect(切面):事务管理就是一个很好的横切关系的例子。切面作为顾问(advisor)和拦截(interceptor)应用于Spring中。

? ? ? ? ? ? ? ? aspects的2种方式:

? ? ? ? ? ? ? ? ? ? ? ? regular classes (the schema-based approach) ?

? ? ? ? ? ? ? ? ? ? ? ? regular classes annotated with the @Aspect annotation (the@AspectJ style).--spring2.0以后

? ? ? ? Joinpoint(接合点):

? ? ? ? ? ? ? ? Spring中,一个接合点通常是方法调用。Spring并不明显地使用"接合点"作为术语;

? ? ? ? ? ? ? ? 接合点信息可以通过MethodInvocation的参数穿过拦截器访问,相当于实现org.springframework.aop.Poinkcut接口。

? ? ? ? Advice(通知):

? ? ? ? ? ? ? ? action taken by an aspect at a particular join point.//被AOP框架使用的“特定接合点”

? ? ? ? ? ? ? ? "around,""before" and "after","after exception" advice.

? ? ? ? ? ? ? ? around adive--最常用

? ? ? ? ? ? ? ? Many AOP frameworks, including Spring,model an advice as an interceptor, maintaining a chain of interceptors around the join point.

? ? ? ? Pointcut(切入点):AOP框架必须允许开发者指定切入点:例如使用正则表达式。

? ? ? ? Introduction(引入):

? ? ? ? ? ? ? ? 向一个通知类中添加方法或成员变量。Spring允许你向任何通知类中引入新接口。

? ? ? ? ? ? ? ? 有自己的Advisor:IntroductionAdvisor

? ? ? ? Target object(标签对象):

? ? ? ? ? ? ? ? 包含接合点的对象。也被通知对象或者代理对象所引用。

? ? ? ? ? ? ? ? object being advised by one or more aspects. Also referred to as the advised object.?

? ? ? ? ? ? ? ? Since Spring AOP is implemented using runtime proxies, this object will always be a proxied object.

? ? ? ? AOP proxy(AOP代理):

? ? ? ? ? ? ? ? 被AOP框架创建的对象,包括通知。在Spring中,一个AOP代理将会是一个JDK dynamic proxy or a CGLIB proxy.

? ? ? ? Weaving(编织):

? ? ? ? ? ? ? ? 聚合切面以生成一个通知对象。这可以在编译时(比如使用AspectJ编译器)或在运行时完成。

? ? ? ? ? ? ? ? Spring,如同其它纯Java AOP框架一样,在运行时完成编织。

?

2,spring-AOP概念

? ? ? ? ?AOP也是非常抽象难以理解的

? ? ? ? AOP有三个最核心的概念:Advice、Pointcut和Advisor。

? ? ? ? ? ? ? ? Advice是你想向其他程序内部不同地方注入的代码。

? ? ? ? ? ? ? ? Pointcut定义了需要注入Advisor(应该为Advice吧)的位置(通常是某个特定的类的一个public方法),

? ? ? ? ? ? ? ? Advisor是Pointcut和Advice的装配器,是将Advice注入程序中预定义位置的代码。

?

3,基础

? ? ? ? src:为类路径

? ? ? ? 类路径实例化spring容器

? ? ? ? ? ? ? ? ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"beans.xml"});

? ? ? ? spring的规范

? ? ? ? ? ? ? ? bean命名==变量命名

? ? ? ? ? ? ? ? List users,不要List userList

? ? ? ? ? ? ? ? 属性用封装类,局部变量用int

? ? ? ? ? ? ? ? 接口中加方法注释,实现类中不用加

?

? ? ? ? 注解

? ? ? ? ? ? ? ? 不是耦合,因为它只改变了一个类

? ? ? ? ? ? ? ? 很少改动的用注解,因为要编译;经常改变的用xml配置

?

? ? ? ? 轻量级重量级框架区别

? ? ? ? ? ? ? ? 容器使用的服务少-轻量级,多时spring就变为重量级了

?

? ? ? ? spring的作用范围

? ? ? ? ? ? ? ? 1,signleton,prototype

? ? ? ? ? ? ? ? 2,web下request,session

? ? ? ? spring默认管理bean

? ? ? ? ? ? ? ? signleton,判断bean1==bean2

? ? ? ? ? ? ? ? 启动时实例化bean,在无参构造中System.out

? ? ? ? ? ? ? ? 生命周期,至spring容器结束

?

IOC

? ? ? ? PersonService中

? ? ? ? ? ? ? ? PersonDao pDao=new PersonDao()//pDao在service的内部被创建及维护

?

? ? ? ? IOC后,只需要声明一下PersonDao

?

IOC用法--只会注入属性,不会把bean纳入spring的管理范围

? ? ? ? 1,set对各种类型的装配

? ? ? ? ? ? ? ? 1,单个属性value

? ? ? ? ? ? ? ? 2,各种集合set,list,map,properties[]

? ? ? ? 2,Field//用注解--------推荐

? ? ? ? ? ? ? ? @Resource默认按名称(id)装配,找不到在按类型

? ? ? ? ? ? ? ? @Autowired按类型进行装配//不推荐@Autowired

? ? ? ? 3,构造

?

? ? ? ? 4,spring2.5引入了组件自动扫描机制(其实是Field方式)

? ? ? ? ? ? ? ? xml配置上百个bean节点时,很难维护。

? ? ? ? ? ? ? ? 从类路径下寻找标注了@Component(泛指),@Service,@Controller(action),@Repository(dao组件,与数据库相关的)注解的类,

? ? ? ? ? ? ? ? 并把这些类纳入到spring容器中管理

?

? ? ? ? ? ? ? ? 引入context命名空间

? ? ? ? ? ? ? ? <context:component-scan base-package="cn.itest"/>//需要自动扫描的包

? ? ? ? ? ? ? ? @Service("personService")?

? ? ? ? ? ? ? ? public class PersonServiceBean implements PersonService {

? ? ? ? ? ? ? ? ? ? ? ? @Resource //Field//用注解--------------------------------//

? ? ? ? ? ? ? ? ? ? ? ? //等效于@Resource(name="pesronDao")

? ? ? ? ? ? ? ? ? ? ? ? private PersonDao personDao;

?

? ? ? ? ? ? ? ? <context:component-scan base-package="cn.itcast"/>

? ? ? ? ? ? ? ? //它已包含 <context:annotation-config/> ,所以不需要在导入<context:annotation-config了

?

? ? ? ? ? ? ? ? <context:annotation-config/>

? ? ? ? ? ? ? ? //annotation-config隐式注册了CommonAnnotationBeanPostProcessor等

? ? ? ? ? ? ? ? //用于注入(IOC 只会注入属性,不会把bean纳入spring的管理范围),

?

?

?

?

?

?

?

<====================== 3,spring-AOP====================== > ? ? ? ?

1,注解方式:需要<aop:aspectj-autoproxy/>//"aop:"说明需要xmlns:aop

? ? ? ? @Aspect?

? ? ? ? public class MyInterceptor0 {

? ? ? ? ? ? ? ? @Pointcut("execution (* cn.itcast.service.impl.PersonServiceBean0.*(..))")

? ? ? ? ? ? ? ? private void anyMethod(){}//声明一个pointcut

?

? ? ? ? ? ? ? ? @Before("anyMethod() ?&& args(name)")//增加pointcut参数限制

? ? ? ? ? ? ? ? public void doAccessCheck(String name){

? ? ? ? ? ? ? ? ? ? ? ? System.out.println("before advice:"+name);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? @AfterReturning(pointcut="anyMethod()",returning="result")

? ? ? ? ? ? ? ? public void doAfterReturning(String result){

? ? ? ? ? ? ? ? ? ? ? ? System.out.println("after advice:"+result);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? @After("anyMethod()")//最终通知finally

? ? ? ? ? ? ? ? public void doAfter(){

? ? ? ? ? ? ? ? ? ? ? ? System.out.println("after finally advice");

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? @AfterThrowing(pointcut="anyMethod()",throwing="e")

? ? ? ? ? ? ? ? public void doAfterThrowing(Exception e){

? ? ? ? ? ? ? ? ? ? ? ? System.out.println("after throw advice:"+e);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? @Around("anyMethod()")

? ? ? ? ? ? ? ? public Object doAround(ProceedingJoinPoint pjp)throws Throwable{

? ? ? ? ? ? ? ? ? ? ? ? System.out.println("进入 around");

? ? ? ? ? ? ? ? ? ? ? ? Object result=pjp.proceed();

? ? ? ? ? ? ? ? ? ? ? ? System.out.println("退出 around");

? ? ? ? ? ? ? ? ? ? ? ? return result;

? ? ? ? ? ? ? ? }

? ? ? ? }

? ? ? ? <aop:aspectj-autoproxy/>

? ? ? ? <bean id="myInterceptor0" ref="aspectBean">

? ? ? ? ? ? ? ? ? ? ? ? <aop:pointcut id="mycut" expression="execution(* cn.itcast.service.impl.PersonServiceBean.*(java.lang.String,..))"/>

? ? ? ? ? ? ? ? ? ? ? ? <aop:before pointcut-ref="mycut" method="doAccessCheck"/>

? ? ? ? ? ? ? ? ? ? ? ? <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>

? ? ? ? ? ? ? ? ? ? ? ? <aop:after-throwing pointcut-ref="mycut" method="doAfter"/>

? ? ? ? ? ? ? ? ? ? ? ? <aop:after pointcut-ref="mycut" method="doAfterThrowing"/>

? ? ? ? ? ? ? ? ? ? ? ? <aop:around pointcut-ref="mycut" method="doAround"/>

? ? ? ? ? ? ? ? </aop:aspect>

? ? ? ? </aop:config>

?

? ? ? ? MyInterceptor类,去掉所有的注解

?

?

<====================== 4,spring-Hibernate-JDBC====================== >

spring-Hibernate

? ? ? ? UserDao extends HibernateDaoSupport,后,就不用setSessionFactory

? ? ? ? 但xml里还是要的

?

spring-JDBC

? ? ? ? userDao需要

? ? ? ? ? ? ? ? <property name="dataSource" ref="dataSource"/>

?

? ? ? ? ? ? ? ? private JdbcTemplate jdbcTemplate;

? ? ? ? ? ? ? ? public void setDataSource(DataSource dataSource) {

? ? ? ? ? ? ? ? ? ? ? ? this.jdbcTemplate =new JdbcTemplate(dataSource);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? //IOC不需要属性,只需要set方法

? ? ? ? ? ? ? ? 不需要private DataSource dataSource,有setDataSource就可以了


热点排行