spring容器就是常说的Ioc容器,即控制翻转,做为程序员,当咱们去实例化一个 对象时,一般须要手动new建立一个对象,而且去跟踪该对象的生命周期,内存分配,内存释放等状况,这样对咱们写代码会形成很大不方便,而使用spring容器,spring的实例化new交给容器,咱们使用 getBean获取该对象便可,不须要去管理对象。java
DI 是指依赖注入,即容器建立对象后,已经处理了对象的依赖关系。mysql
二、依赖注入的两种方式程序员
public class People{
public People(Axe axe)
{
}
}
则文件中配置注解以下web
<bean id="people" class="">
<constructor-arg ref="axe"/>
</bean>
<bean id="axe" class=""/>
<bean id="people" class="">
<property name="axe" ref="axe"/> <!-- set参数名字 axe-->
</bean>
<bean id="axe" class=""/>
service层spring
@Service("") //注明类的名字
public class MyServiceImpl {
@Autowired //注入Dao层,利用注解ppDao,必须在注入的类声明一致
private PeopleDao ppDao;
}
Dao层sql
@Repository("ppDao")
public class PeopleDapImpl implements PeopleDa0{}
Dao层通常依赖于sessionFactory,而sessionFactory,能够直接
在文件里面配置便可。数据库
为了让注解生效,须要在文件里面设置,注入context标签express
<xmlns:context="http://www.springframework.org/schema/context"/>
<!--哪些包利用注解-->
<context:component-scan base-package="">
<context:include-filter type="regex" expression=""/>
<context:exclude-filter type="regex" expression=""/>
</context:component-scan>
经常使用注解
@Component 指定名称的对象
@Service 服务层的组件
@Repository 数据层Dao的组件
@Controller 控制层的组件
@Autowired 注入属性,会从容器中自动查找对象并注入到@Autowired对象上面
@Scope 指定做用域
@Resource 在setter 方法上面定义
@PostConstuct 即定义init-method
@PreDestroy 即定义destroy-method
@Lazy
@DependsOn({“”,”“})编程
经常使用容器BeanFactory为最基本的容器接口, 经常使用方法 getBean() containsBean() getType() BeanFactory实现类通常为DefaultListableBeanFactory 而applicationContext除了实现BeanFactory接口外,另外实现其余接口 一、事件机制 ApplicationEvent ApplicationListener 二、message接口 三、资源访问Resource 四、以声明方式启动spring容器 ContextLoaderListener 五、国际化支持 六、bean实现ApplicationContextAware,回调机制获取容器,自动注入spring容器
四、spring中的Bean安全
一、bean的定义 全部的bean都被包含在bean下面,咱们能够在beans里面定义通用的属性,若在单个bean中再次定义,则可直接覆盖掉。
<beans default-lazy-init="true/false" defult-merge="true/false"
default-autowire="byType/byName/constuctor/autodetect"
default-autowire-candidate="com.*.*"
default-init-method=""
default-detroy-method="">
二、单个bean声明
<bean id="" class=""/>
三、bean的做用域
singleton、protoType、request、session、global session
四、配置依赖
普通属性 <property name="" value="">
ref指 <property name="" ref="">
自动装配 autowire="byType/byName/constructor/autodetect"
嵌套bean
<property name="">
<bean class=""/>
</property>
集合值
<property name="">
<set>
<value/>
<ref bean=""/>
<bean class=""/>
</set>
<list>
<value></value>
</list>
<map>
<entry key="" value-ref=""/>
<entry key="" value=""/>
</map>
</property>
五、建立Bean实例
一、直接构造器建立
二、静态工厂构建
<bean id="" class="" factory-method=""/>
三、实例工厂方法
<bean id="BeanFactory" class=""/>
<bean id="" factory-bean="" factory-method=""/>
六、特殊的bean
一、抽象bean,做为模板,通常用做父类,提供公共参数
二、工厂bean,实现FacotyBean的接口,其中getObject为返回所须要的bean
三、bean的回调,实现BeanNameAware
七、Bean的生命周期
一、建立实例 二、注入依赖值 三、实现接口InitializingBean即afterPropertiesSet() 四、实现init-method 五、逻辑方法 六、实现接口DisposableBean 即destroy方法 七、实现destroy-method方法
八、协同声明周期,即singleton依赖protoType
将singleton的bean的类做为抽象类,protoType对应的字段定义抽象get方法,
则定义singleton的bean时,使用lookup-method方法
public Abstract class People {
private Axe axe;
public Abstract Axe getAxe();
}
public Class iconAxe implements Axe
{
}
<bean id="people" class="">
<lookup-method name="getAxe" bean=""/>
</bean>
其底层实现,使用动态代理的方法实现。cglib实现抽象类。
九、高级依赖关系配置
一、获取其余bean的属性,即getter方法
<bean id="people" class="">
<property name="son" ref="son"/>
</bean>
获取people中son的属性,使用
<bean id="son2" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetBeanName" value=""/>
<property name="PropertyPath" value=""/>
</bean>
二、注入其余bean的Field属性
<bean id="" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="targetClass" value=""/> class类名
<property name="targetField" value=""/>
</bean>
三、注入方法返回值
<bean id="" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref=""/>
<property name="targetMethod" value=""/>
<property name="arguments"> 方法执行的参数
<list>
<bean/>
<ref/>
</list>
</property>
</bean>
五、声明环绕通知 <aop:around> 使用ProceedingJoinPoint joinPoint 定义 public void watch(ProceedingJoinPoint joinPoint) { //前置逻辑 joinPoint.proceed(); //目标方法执行 //后置逻辑 } 六、advice传递参数 在通知中表示参数属性 arg-names <aop:before pointcut-ref="" method="" arg-names=""/> 则参数名字将传递给切面。 七、introduction给对象添加新功能。 AOP能够实现新功能,相应的能够给目标对象引入新的方法 若咱们对 目标类 people 引入 eat()方法,则只须要定义一个Eater()接口,而且用 具体的类实现该接口的方法 便可 <aop:aspect> <aop:declare-parents type-matching="类" implements-interface="接口名" default-impl="实现类名" /> </aop:aspect> 或者用delegate-ref方法引用一个spring bean做为引入的委托 <aop:aspect> <aop:declare-parents type-matching="类" implements-interface="接口名" delegate-ref="id" /> </aop:aspect> 后者好处能够用来,依赖其余的bean,配置到容器中 八、注解切面 4中通知同样 @Aspect public class People() { @PointCut("execution("") && args()") 注意切点的名称来源于注解所应用的方法名称 @Before(“切点名称”) After AfterReturning AfterThrowing public void applaud() } 而后定义切面bean <bean id="people" class=""/> 为了使切面生效定义 <aop:aspcetj-autoproxy />,它将在上下文中建立一个AnnotationAwareAspectJAutoProxyCreator类,它会 自动代理一些bean,这些Bean的方法须要与使用@aspect注解的bean的所定义的切点相匹配,而这些切点是使用 pointcut注解定义起来的 引入aop空间 <xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemalocation="" /> 环绕通知 @Around("方法名") 标注引入introducing <aop:aspect> <aop:declare-parents type-matching="" implements-interface="" default-impl="" delegate-ref=""/> </aop:aspect> 注解 @DeclareParents @Aspect 注解接口的引入类 public class InterfaceIntroduce { @DeclareParents( value="目标类名" defaultImpl="接口实现方法") public Static Interface interface; } 配置文件引入切面AspectJ 一、before一般定义一个value值,用@Before("execute()")表示切入点 二、afterReturning须要定义一个切入掉,value或者pointcut,定义一个返回值returning @AfterReturning(returning="rvt",pointcut="execute()") 该rvt能够做为加强方法的参数 三、afterThrowing须要定义一个切入点(value,pointcut)和throwing @AfterReturning(throwing="ex",pointcut="execute()") 该ex能够做为加强方法的参数Throwable ex 四、after不论方法成功与否都会执行加强,只须要定义一个切入点 五、around的强大 一、决定方法什么时候执行 jp.proceed(); 二、改变执行方法的参数 jp.proceed(args); 三、改变执行方法返回的参数 rt = jp.proceed(args); return rt + "test"; 四、注意线程安全 六、访问目标对象的方法 经过链接点joinPoint,此时说明加强已经被织入目标方法,经过jp 能够获取该方法的名字,参数,目标对象 方法名字:jp.getSignature().getName(); 参数个数:jp.getArgs(); 目标对象:jp.getTarget(); 七、优先级 同一切面的优先级 before - around -afterReturning - after 不一样切面的优先级 一、实现Order()接口 二、注解@Order(),属性值越小,优先级越高
一、事务是什么? 事务是一个操做序列,要么所有提交,要么所有回退,事务有四个特性,ACID, 二、spring事务策略实现的方法 spring的事务策略是经过,PlateformTransactionManager接口实现的,对于不一样的底层,spring能够配置不一样的该接口的实现类,事务策略与事务资源相分离 三、PlateformTransactionManager的方法 一、TransactionStatus getTransaction(TransactionDefinition definition) 二、commit(TransactionStatus status) 三、rollback(status) 四、TransactionDefinition 定义了事务的规则,事务的规则,五角星 一、事务隔离 isolation 隔离级别 二、事务超时 timeout 三、事务可读 readonly 四、事务传播 propagation 五、回滚规则 rollback 对特定的异常进行回滚 no-rollback 五、TransactionStatus 表明事务自己,提供了控制事务执行和查询事务状态的方法 一、判断是否为新建的事务 isNewTransaction() 二、设置事务回滚 setRollbackOnly() 三、查询事务是否有回滚标志 isRollBackOnly() 六、底层TransactionManager,规则为 前缀 org.springframework 类型 jdbc orm transaction等spring的七大组件 小类为 datasource hibernate4 jta 类名开头 名字为 DataSourceTransactionManager HibernateTransactionManager JtbTransactionManager ``` <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactory"> <property name="dataSource" ref=""/> <property name="mappingSources" > <!-- 映射文件 --> <list> <value></value> </list> </property> <property name="hibernateProperties"> <props> <prop key="" value=""/> </props> </property> </bean> <bean id="transaction" class="org.springframework.orm.hibernate4.HibernateTransactionManager" > <property name="sessionFactory" ref=""/> </bean> ``` 基于注解的sessionFactory的配置
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactory">
<property name="dataSource" ref=""/>
<property name="annotaedClasses" > <!-- 映射文件 -->
<list>
<value></value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="" value=""/>
</props>
</property>
</bean>
利用tx进行advice加强,即切面的加强,
定义切面的advice,即事务管理做用的范围,边界
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" rollback-for="" propagation="" isolation="" timout=""/>
<tx:method name=""/>
</tx:attributes>
</tx:advice>
如何让advice生效,定义一个通知器advisor,在调用该些方法,则调用加强
<aop:config>
<aop:pointcut id="myPoint" expression="execute(* *.*()) && args()"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPoint"/>
</aop:config>
使用注解配置事务
@Transactonal(isolation="" noRollbackFor="" noRoolbackForClassName="" propagation=""readOnly="" rollbackFor="" timeout="")
配置文件启动事务
<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean <!--注解启动--> <tx:annotation-driven transaction-manager="transactionManger"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driver">com.mysql.jdbc.Driver</property> <property name="url">jdbc:mysql://localhost:3306/数据库名字</property> <property name="user"/> <property name="password"/> </bean> <bean id="sessionFactory" class=""> <property name="dataSource" ref="dataSource"/> <property name="annotatedClasses"> <list> <value>...class</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect= </value </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean>
配置事务加强
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="" 属性配置/>
</tx:attributes>
</tx:advice>
将advice使用adivsor生效
<aop:config>
切点
<aop:pointcut id="" expression="execute()"/>
<aop:advisor advice-ref="" pointcut-ref=""/>
</aop:config>
一、spring启动,配置servletContextListener,该监听器能够在web应用启动的时候回调自定义的方法,此时能够启动spring,而spring的ContextLoaderListener配置Listener 若须要读取多个配置文件,则在web.xml里面定义ContextLocation
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>dao.xml,applicationContext.xml</param-value>
</context-param>
<Listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</Listener>
二、使用自动装配,struts的控制组件自动装备service组件。