青木求鱼——leejie
我要强大到没有任何事物能够打扰到我内心地平静。
I want to be strong enough that no thing can disturb the tranquility of my heart.

Spring编程  理解IoCAOP

Spring是一个非常好用轻量级IoC和AOP的框架,Spring不仅提供了基础的IoC和AOP服务,还提供了面向业务层和web层的服务和功能,如数据库,事务管理,远程调用,acegi认证管理等。

一、     理解Spring的核心IoC(DI)

掌握基于xml文件的配置和使用Annotation的配置。

a)       IoC(控制反转:Inverse of Control)的另一种说法DI(依赖注入:Dependency Injection);

b)      IoC容器管理Bean的创建,装配(注入)和销毁。

1、          为bean的变量属性赋值

<property name="proName> proValue </property>

2、          为List、Set和Map类型属性赋值

为Map类型赋值:

<property name="proName ">

                     <props>

                            <prop key="key1 ">value1</prop>

                            <prop key="key2">value2      </prop>

                            …

                     </props>

         </property>

为List类型赋值:

<property name="annotatedClasses">

                     <list>

                            <value>value1 </value>

                            <value>value2</value>

                            …

                     </list>

         </property>

3、          引用其他的bean

IoC容器对于DI的三种实现方式:构造函数注入(Constructor Injection),接口注入(Interface Injection),Setter方法注入(Setter Injection);

常用的注入方式:Setter方法注入:

<bean id=”ID” class=”ClassName”>

       <property name=”” ref=””>

</bean>

二、     Spring的AOP编程

(知识点补充:jdk的动态代理中proxy和invocationHandler的用法)

1、AOP:面向切面编程(对面向对象编程的补充)

2、掌握配置AOP编程中的xml的配置过程

  A)、<bean id=”refAOP” class=”aspectClass”></bean>

  B)、

<aop:config>

       <aop:aspect id=”aopID” ref=” refAOP”>

<!—环绕型通知-->

<aop:around method=”” pointcut=”execution()”/>

<!—前置型通知-->

<aop:before method=”” pointcut=”execution()”/>

<!—环绕型通知-->

<aop:after method=”” pointcut=”execution()”/>

</aop:aspect>

</aop:config>

3、了解AOP编程中Annotation的配置过程

A)、了解spring的织入时使用的织入点语法(Aspectj的织入点语法)

B)、前置通知(before advice)、后置通知(after advice)和环绕性通知(around advice)

 

三、     一个Spring配置文件

applicationContext.xml
  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <beans
  3     xmlns="http://www.springframework.org/schema/beans"
  4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5     xmlns:p="http://www.springframework.org/schema/p"
  6     xmlns:context="http://www.springframework.org/schema/context"
  7     xmlns:aop="http://www.springframework.org/schema/aop"
  8     xmlns:tx="http://www.springframework.org/schema/tx"
  9     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 10                         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
 11                         http://www.springframework.org/schema/context
 12                            http://www.springframework.org/schema/context/spring-context-3.0.xsd 
 13                         http://www.springframework.org/schema/aop
 14                            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
 15                            http://www.springframework.org/schema/tx 
 16                            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
 17 
 18 <!--     <context:annotation-config/>
 19     <context:component-scan base-package="com.leejie.transaction"></context:component-scan> -->
 20     
 21     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 22         <property name="locations">
 23             <value>classpath:jdbc.properties</value>
 24         </property>
 25     </bean>
 26 
 27     <bean id="dataSource"
 28         class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
 29         <property name="driverClassName"
 30             value="${jdbc.driverClassName}">
 31         </property>
 32         <property name="url" value="${jdbc.url}"></property>
 33         <property name="username" value="${jdbc.username}"></property>
 34         <property name="password" value="${jdbc.password}"></property>
 35     </bean>
 36     <bean id="sessionFactory"
 37         class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
 38         <property name="dataSource">
 39             <ref bean="dataSource" />
 40         </property>
 41         <property name="hibernateProperties">
 42             <props>
 43                 <prop key="hibernate.dialect">
 44                     org.hibernate.dialect.MySQLDialect
 45                 </prop>
 46                 <prop key="hibernate.current_session_context_class">
 47                     thread
 48                 </prop>
 49                 <prop key="hibernate.show_sql">
 50                     true
 51                 </prop>
 52                 <prop key="hibernate.format_sql">
 53                     true
 54                 </prop>
 55             </props>
 56         </property>
 57         <property name="packagesToScan">
 58             <list>
 59                 <value>com.leejie.po</value>
 60             </list>
 61         </property>
 62         <!-- <property name="annotatedClasses">
 63             <list>
 64                 <value>com.leejie.po.CourseInfo</value>
 65                 <value>com.leejie.po.CourseTime</value>
 66             </list>
 67         </property> -->
 68         </bean>
 69         
 70         <bean id="myJTM" class="com.leejie.transaction.TransactionInterceptor">
 71             <property name="sessionFactory" ref="sessionFactory"></property>
 72         </bean>
 73         <aop:config>
 74             <aop:aspect id="txManager" ref="myJTM">
 75                 <aop:around method="invoke" pointcut="execution(public * com.leejie.service..*.*(..))"/>
 76             </aop:aspect>
 77         </aop:config>
 78         
 79         <bean id="courseInfoDAO" class="com.leejie.dao.CourseInfoDAOImpl">
 80             <property name="sessionFactory" ref="sessionFactory"></property>
 81         </bean>
 82         <bean id="courseTimeDAO" class="com.leejie.dao.CourseTimeDAOImpl">
 83             <property name="sessionFactory" ref="sessionFactory"></property>
 84         </bean>
 85         <bean id="courseService" class="com.leejie.service.CourseServiceImpl">
 86             <property name="courseInfoDAO" ref="courseInfoDAO"></property>
 87             <property name="courseTimeDAO" ref="courseTimeDAO"></property>
 88         </bean>
 89         <bean id="insertAction" class="com.leejie.action.InsertAction">
 90             <property name="courseService" ref="courseService"></property>
 91         </bean>
 92         <bean id="searchAction" class="com.leejie.action.SearchAction">
 93             <property name="courseService" ref="courseService"></property>
 94         </bean>
 95         
 96         
 97         <!-- <bean id="advisorJta" class="org.springframework.aop.framework.ProxyFactoryBean">
 98             <property name="interceptorNames">
 99                 <list>
100                     <value>myJTM</value>
101                 </list>
102             </property>
103             <property name="target" ref="courseInfoDAO"></property>
104             <property name="proxyTargetClass" value="true"></property>
105         </bean>
106         
107         <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
108             <property name="sessionFactory" ref="sessionFactory"></property>
109         </bean>
110         
111         <bean id="courseInfoDAOTx" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
112             <property name="transactionManager" ref="txManager"></property>
113             <property name="target" ref="courseInfoDAO"></property>
114             <property name="transactionAttributes">
115                 <props>
116                     <prop key="*">PROPAGATION_REQUIRED</prop>
117                 </props>
118             </property>
119         </bean> -->
120         
121         <!-- <tx:advice id="txMAdvice" transaction-manager="txManager">
122             <tx:attributes>
123                 <tx:method name="*" propagation="REQUIRED" />
124             </tx:attributes>
125         </tx:advice>
126         
127         <aop:config>
128             <aop:pointcut expression="execution(public * com.leejie.dao..*.*(..))" id="bussinessService"/>
129             <aop:advisor pointcut-ref="bussinessService" advice-ref="txMAdvice"/>
130         </aop:config> -->
131         
132         <!-- <bean id="myLogInterceptor" class="com.leejie.transaction.LogInterceptor">    
133         </bean>
134         
135         <aop:config>
136             <aop:aspect id="logAspect" ref="myLogInterceptor">
137                 <aop:around method="aroundMethod" pointcut="execution(public * com.leejie.dao..*.*(..))" />
138             </aop:aspect>
139         </aop:config> -->
140         
141         
142         </beans>

 

posted on 2012-07-30 10:39  leejie1001  阅读(682)  评论(0编辑  收藏  举报