spring的一些基础知识回顾

来源:互联网 发布:mac book air 输入法 编辑:程序博客网 时间:2024/05/23 13:52
1.依赖注入方式:
属性注入
构造函数(类型,索引,联合)
工厂方法(非静态,静态)
泛型


2.参数注入
基本类型
注入bean
内部bean
null
级联属性(原始类中初始化new)
集合(list,set,map,property)


3.自动装配(在配置文件中default-autowire)
byName:通过名称
byType:通过类型
constructor:和类型类似,根据构造方法
建议:少用自动装配,设计不当潜在错误。


4.设置bean作用域 scope设置


5.方法注入(获取每次的依赖对象都是新的)
lookup-method,在添加属性的时候使用这个标签


6.方法替换
replace-method,替换原有对象中的方法,要去替换的对象应该实现MethodReplacer


7.bean之间的关系
继承:在父bean配置时设置abstract=true,子配置bean中class用parent替换实现
依赖:在bean中配置depends-on属性依赖的bean
引用:bean中属性或者构造属性中ref实现


8.作用域
singleton,prototype,request,session,global session,application




9.aop
前置通知
后置通知
环绕通知
返回通知
异常通知


<bean id="studentServiceAspect" class="com.java1234.advice.StudentServiceAspect"></bean>
<bean id="studentService" class="com.java1234.service.impl.StudentServiceImpl"></bean>
<aop:config>
<aop:aspect id="studentServiceAspect" ref="studentServiceAspect">
<aop:pointcut expression="execution(* com.java1234.service.*.*(..))" id="businessService"/>
<aop:before method="doBefore" pointcut-ref="businessService"/>
<aop:after method="doAfter" pointcut-ref="businessService"/>
<aop:around method="doAround" pointcut-ref="businessService"/>
<aop:after-returning method="doAfterReturning" pointcut-ref="businessService"/>
<aop:after-throwing method="doAfterThrowing" pointcut-ref="businessService" throwing="ex"/>
</aop:aspect> 
</aop:config>




package com.java1234.advice;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;


public class StudentServiceAspect {


public void doBefore(JoinPoint jp){
System.out.println("类名:"+jp.getTarget().getClass().getName());
System.out.println("方法名:"+jp.getSignature().getName());
System.out.println("开始添加学生:"+jp.getArgs()[0]);
}

public void doAfter(JoinPoint jp){
System.out.println("类名:"+jp.getTarget().getClass().getName());
System.out.println("方法名:"+jp.getSignature().getName());
System.out.println("学生添加完成:"+jp.getArgs()[0]);
}

public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("添加学生前");
Object retVal=pjp.proceed();
System.out.println(retVal);
System.out.println("添加学生后");
return retVal;
}

public void doAfterReturning(JoinPoint jp){
System.out.println("返回通知");
}

public void doAfterThrowing(JoinPoint jp,Throwable ex){
System.out.println("异常通知");
System.out.println("异常信息:"+ex.getMessage());
}
}




public interface StudentService {


public void addStudent(String name);
}


public class StudentServiceImpl implements StudentService{


@Override
public void addStudent(String name) {
// System.out.println("开始添加学生"+name);
System.out.println("添加学生"+name);
System.out.println(1/0);
// System.out.println("完成学生"+name+"的添加");
}


}




10.spring对JDBC的支持
配置数据源dbcp
使用jdbcTemplate
使用jdbcDaoSupport,通过集成方式消除jdbcTemplate
使用NamedParameterJdbcTemplate支持命名参数变量




11.事务
编程式事务管理
spring提供的树屋模板类:TransactionTemplate
事务管理器:DataSourceTransactionManger


声明式事务管理
使用xml配置声明
使用注解配置声明


事务传播行为


标准配置模板
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    
    <!-- jdbc事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 配置事务通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>  
            <tx:method name="insert*" propagation="REQUIRED" />  
            <tx:method name="update*" propagation="REQUIRED" />  
            <tx:method name="edit*" propagation="REQUIRED" />  
            <tx:method name="save*" propagation="REQUIRED" />  
            <tx:method name="add*" propagation="REQUIRED" />  
            <tx:method name="new*" propagation="REQUIRED" />  
            <tx:method name="set*" propagation="REQUIRED" />  
            <tx:method name="remove*" propagation="REQUIRED" />  
            <tx:method name="delete*" propagation="REQUIRED" />  
            <tx:method name="change*" propagation="REQUIRED" />  
            <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="load*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="*" propagation="REQUIRED" read-only="true" />  
        </tx:attributes>  
    </tx:advice>
    
    <!-- 配置事务切面 -->
    <aop:config>
    <!-- 配置切点 -->
    <aop:pointcut id="serviceMethod" expression="execution(* com.java1234.service.*.*(..))" />
    <!-- 配置事务通知 -->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
    </aop:config> 


<context:property-placeholder location="jdbc.properties"/>
    
    <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
    <constructor-arg ref="dataSource"></constructor-arg>
    </bean>

<bean id="bankDao" class="com.java1234.dao.impl.BankDaoImpl">
<property name="namedParameterJdbcTemplate" ref="namedParameterJdbcTemplate"></property>
</bean>
<bean id="bankService" class="com.java1234.service.impl.BankServiceImpl">
<property name="bankDao" ref="bankDao"></property>
</bean> 


原创粉丝点击