一:项目中如何体现spring中的切面编程,举例说明
面向切面编程:主要是横切一个关注点,将一个关注点模块化成一个切面。在切面上声明一个通知(advice)和切入点(Pointcut);通知:是指在切面的某个特定的连接点(代表一个方法的执行。通过声明一个org.aspectj.lang.JoinPoint类型的参数可以使通知Advice的主体部分获得连结点信息。)上执行的动作。通知中定义了要插入的方法。切入点:切入点的内容是一个表达式,以描述需要在哪些对象的哪些方法上插入通知中定义的方法。
项目中用到的spring中的切面编程最多的地方:声明式事务管理。
a、定义一个事务管理器
b、配置事务特征(相当于声明通知。一般在业务层的类的一些方法上定义事务)
c、配置哪些类的哪些方法需要配置事务(相当于切入点。一般是业务类的方法上)
二:spring中的事务如何配置:
根据代理机制的不同,总结了五种Spring事务的配置方式,配置文件如下:
第一种方式:每个Bean都有一个代理
01 |
<?xml version= "1.0" encoding= "UTF-8" ?> |
06 |
xsi:schemaLocation="http: |
12 |
<bean id= "sessionFactory" |
13 |
class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" > |
14 |
<property name= "configLocation" value= "classpath:hibernate.cfg.xml" /> |
15 |
<property name= "configurationClass" value= "org.hibernate.cfg.AnnotationConfiguration" /> |
18 |
<!-- 定义事务管理器(声明式的事务) --> |
19 |
<bean id= "transactionManager" |
20 |
class = "org.springframework.orm.hibernate3.HibernateTransactionManager" > |
21 |
<property name= "sessionFactory" ref= "sessionFactory" /> |
25 |
<bean id= "userDaoTarget" class = "com.bluesky.spring.dao.UserDaoImpl" > |
26 |
<property name= "sessionFactory" ref= "sessionFactory" /> |
30 |
class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" > |
32 |
<property name= "transactionManager" ref= "transactionManager" /> |
33 |
<property name= "target" ref= "userDaoTarget" /> |
34 |
<property name= "proxyInterfaces" value= "com.bluesky.spring.dao.GeneratorDao" /> |
36 |
<property name= "transactionAttributes" > |
38 |
<prop key= "*" > PROPAGATION_REQUIRED </prop> |
第二种方式:所有Bean共享一个代理基类
01 |
<?xml version= "1.0" encoding= "UTF-8" ?> |
06 |
xsi:schemaLocation="http: |
12 |
<bean id= "sessionFactory" |
13 |
class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" > |
14 |
<property name= "configLocation" value= "classpath:hibernate.cfg.xml" /> |
15 |
<property name= "configurationClass" value= "org.hibernate.cfg.AnnotationConfiguration" /> |
18 |
<!-- 定义事务管理器(声明式的事务) --> |
19 |
<bean id= "transactionManager" |
20 |
class = "org.springframework.orm.hibernate3.HibernateTransactionManager" > |
21 |
<property name= "sessionFactory" ref= "sessionFactory" /> |
24 |
<bean id= "transactionBase" |
25 |
class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" |
26 |
lazy-init= "true" abstract = "true" > |
28 |
<property name= "transactionManager" ref= "transactionManager" /> |
30 |
<property name= "transactionAttributes" > |
32 |
<prop key= "*" >PROPAGATION_REQUIRED </prop> |
38 |
<bean id= "userDaoTarget" class = "com.bluesky.spring.dao.UserDaoImpl" > |
39 |
<property name= "sessionFactory" ref= "sessionFactory" /> |
42 |
<bean id= "userDao" parent= "transactionBase" > |
43 |
<property name= "target" ref= "userDaoTarget" /> |
第三种方式:使用拦截器
01 |
<?xml version= "1.0" encoding= "UTF-8" ?> |
06 |
xsi:schemaLocation="http: |
12 |
<bean id= "sessionFactory" |
13 |
class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" > |
14 |
<property name= "configLocation" value= "classpath:hibernate.cfg.xml" /> |
15 |
<property name= "configurationClass" value= "org.hibernate.cfg.AnnotationConfiguration" /> |
18 |
<!-- 定义事务管理器(声明式的事务) --> |
19 |
<bean id= "transactionManager" |
20 |
class = "org.springframework.orm.hibernate3.HibernateTransactionManager" > |
21 |
<property name= "sessionFactory" ref= "sessionFactory" /> |
24 |
<bean id= "transactionInterceptor" |
25 |
class = "org.springframework.transaction.interceptor.TransactionInterceptor" > |
26 |
<property name= "transactionManager" ref= "transactionManager" /> |
28 |
<property name= "transactionAttributes" > |
30 |
<prop key= "*" >PROPAGATION_REQUIRED </prop> |
35 |
<bean class = "org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" > |
36 |
<property name= "beanNames" > |
41 |
<property name= "interceptorNames" > |
43 |
<value> transactionInterceptor </value> |
49 |
<bean id= "userDao" class = "com.bluesky.spring.dao.UserDaoImpl" > |
50 |
<property name= "sessionFactory" ref= "sessionFactory" /> |
第四种方式:使用tx标签配置的拦截器
01 |
<?xml version= "1.0" encoding= "UTF-8" ?> |
07 |
xsi:schemaLocation="http: |
14 |
<context:annotation-config /> |
15 |
<context:component-scan base- package = "com.bluesky" /> |
17 |
<bean id= "sessionFactory" |
18 |
class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" > |
19 |
<property name= "configLocation" value= "classpath:hibernate.cfg.xml" /> |
20 |
<property name= "configurationClass" value= "org.hibernate.cfg.AnnotationConfiguration" /> |
23 |
<!-- 定义事务管理器(声明式的事务) --> |
24 |
<bean id= "transactionManager" |
25 |
class = "org.springframework.orm.hibernate3.HibernateTransactionManager" > |
26 |
<property name= "sessionFactory" ref= "sessionFactory" /> |
29 |
<tx:advice id= "txAdvice" transaction-manager= "transactionManager" > |
31 |
<tx:method name= "*" propagation= "REQUIRED" /> |
36 |
<aop:pointcut id= "interceptorPointCuts" |
37 |
expression= "execution(*com.bluesky.spring.dao.*.*(..))" /> |
38 |
<aop:advisor advice-ref= "txAdvice" |
39 |
pointcut-ref= "interceptorPointCuts" /> |
第五种方式:全注解
01 |
<?xml version= "1.0" encoding= "UTF-8" ?> |
07 |
xsi:schemaLocation="http: |
14 |
<context:annotation-config /> |
15 |
<context:component-scan base- package = "com.bluesky" /> |
17 |
<tx:annotation-driven transaction-manager= "transactionManager" /> |
19 |
<bean id= "sessionFactory" |
20 |
class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" > |
21 |
<property name= "configLocation" value= "classpath:hibernate.cfg.xml" /> |
22 |
<property name= "configurationClass" value= "org.hibernate.cfg.AnnotationConfiguration" /> |
25 |
<!-- 定义事务管理器(声明式的事务) --> |
26 |
<bean id= "transactionManager" |
27 |
class = "org.springframework.orm.hibernate3.HibernateTransactionManager" > |
28 |
<property name= "sessionFactory" ref= "sessionFactory" /> |
此时在DAO上需加上@Transactional注解,如下:
01 |
package com.bluesky.spring.dao; |
03 |
import java.util.List; |
05 |
import org.hibernate.SessionFactory; |
06 |
import org.springframework.beans.factory.annotation.Autowired; |
07 |
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; |
08 |
import org.springframework.stereotype.Component; |
10 |
import com.bluesky.spring.domain.User; |
13 |
@Component ( " userDao " ) |
14 |
public class UserDaoImpl extends HibernateDaoSupport implements UserDao { |
16 |
public List <User> listUsers() { |
17 |
return this .getSession().createQuery( " from User " ).list(); |
三:spring是什么?根据你的理解详细谈谈你的见解
Spring是一个开源的轻量级Java SE(Java 标准版本)/Java EE(Java 企业版本)开发应用框架,其目的是用于简化企业级应用程序开发。
应用程序是由一组相互协作的对象组成。而在传统应用程序开发中,一个完整的应用是由一组相互协作的对象组成。
所以开发一个应用除了要开发业务逻辑之外,最多的是关注如何使这些对象协作来完成所需功能,而且要低耦合、高内聚。
四:用两种方式根据门号从高到低,工资从低到高列出每个员工的信息:
select * from employee order by deptid desc,salary asc; select eid,ename,salary,deptid,from employee orderby deptid desc,salary asc