赞
踩
spring容器就是常说的Ioc容器,即控制翻转,作为程序员,当我们去实例化一个 对象时,通常需要手动new创建一个对象,并且去跟踪该对象的生命周期,内存分配,内存释放等情况,这样对我们写代码会造成很大不方便,而使用spring容器,spring的实例化new交给容器,我们使用 getBean获取该对象即可,不需要去管理对象。
DI 是指依赖注入,即容器创建对象后,已经处理了对象的依赖关系。
2、依赖注入的两种方式
public class People{
public People(Axe axe)
{
}
}
则文件中配置注解如下
<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层
@Service("") //注明类的名字
public class MyServiceImpl
{
@Autowired //注入Dao层,利用注解ppDao,必须在注入的类声明一致
private PeopleDao ppDao;
}
Dao层
@Repository("ppDao")
public class PeopleDapImpl implements PeopleDa0{}
Dao层一般依赖于sessionFactory,而sessionFactory,可以直接
在文件里面配置即可。
为了让注解生效,需要在文件里面设置,注入context标签
<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接口外,另外实现其他接口 1、事件机制 ApplicationEvent ApplicationListener 2、message接口 3、资源访问Resource 4、以声明方式启动spring容器 ContextLoaderListener 5、国际化支持 6、bean实现ApplicationContextAware,回调机制获取容器,自动注入spring容器
4、spring中的Bean
1、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="">
2、单个bean声明
<bean id="" class=""/>
3、bean的作用域
singleton、protoType、request、session、global session
4、配置依赖
普通属性 <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>
5、创建Bean实例
1、直接构造器创建
2、静态工厂构建
<bean id="" class="" factory-method=""/>
3、实例工厂方法
<bean id="BeanFactory" class=""/>
<bean id="" factory-bean="" factory-method=""/>
6、特殊的bean
1、抽象bean,作为模板,一般用作父类,提供公共参数
2、工厂bean,实现FacotyBean的接口,其中getObject为返回所需要的bean
3、bean的回调,实现BeanNameAware
7、Bean的生命周期
1、创建实例
2、注入依赖值
3、实现接口InitializingBean即afterPropertiesSet()
4、实现init-method
5、逻辑方法
6、实现接口DisposableBean 即destroy方法
7、实现destroy-method方法
8、协同声明周期,即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实现抽象类。
9、高级依赖关系配置
1、获取其他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>
2、注入其他bean的Field属性
<bean id="" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="targetClass" value=""/> class类名
<property name="targetField" value=""/>
</bean>
3、注入方法返回值
<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>
5、声明环绕通知 <aop:around> 使用ProceedingJoinPoint joinPoint 定义 public void watch(ProceedingJoinPoint joinPoint) { //前置逻辑 joinPoint.proceed(); //目标方法执行 //后置逻辑 } 6、advice传递参数 在通知中表示参数属性 arg-names <aop:before pointcut-ref="" method="" arg-names=""/> 则参数名字将传递给切面。 7、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,配置到容器中 8、注解切面 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 1、before通常定义一个value值,用@Before("execute()")表示切入点 2、afterReturning需要定义一个切入掉,value或者pointcut,定义一个返回值returning @AfterReturning(returning="rvt",pointcut="execute()") 该rvt可以作为增强方法的参数 3、afterThrowing需要定义一个切入点(value,pointcut)和throwing @AfterReturning(throwing="ex",pointcut="execute()") 该ex可以作为增强方法的参数Throwable ex 4、after不论方法成功与否都会执行增强,只需要定义一个切入点 5、around的强大 1、决定方法何时执行 jp.proceed(); 2、改变执行方法的参数 jp.proceed(args); 3、改变执行方法返回的参数 rt = jp.proceed(args); return rt + "test"; 4、注意线程安全 6、访问目标对象的方法 通过连接点joinPoint,此时说明增强已经被织入目标方法,通过jp 可以获取该方法的名字,参数,目标对象 方法名字:jp.getSignature().getName(); 参数个数:jp.getArgs(); 目标对象:jp.getTarget(); 7、优先级 同一切面的优先级 before - around -afterReturning - after 不同切面的优先级 1、实现Order()接口 2、注解@Order(),属性值越小,优先级越高
1、事务是什么? 事务是一个操作序列,要么全部提交,要么全部回退,事务有四个特性,ACID, 2、spring事务策略实现的方法 spring的事务策略是通过,PlateformTransactionManager接口实现的,对于不同的底层,spring可以配置不同的该接口的实现类,事务策略与事务资源相分离 3、PlateformTransactionManager的方法 1、TransactionStatus getTransaction(TransactionDefinition definition) 2、commit(TransactionStatus status) 3、rollback(status) 4、TransactionDefinition 定义了事务的规则,事务的规则,五角星 1、事务隔离 isolation 隔离级别 2、事务超时 timeout 3、事务可读 readonly 4、事务传播 propagation 5、回滚规则 rollback 对特定的异常进行回滚 no-rollback 5、TransactionStatus 代表事务本身,提供了控制事务执行和查询事务状态的方法 1、判断是否为新建的事务 isNewTransaction() 2、设置事务回滚 setRollbackOnly() 3、查询事务是否有回滚标志 isRollBackOnly() 6、底层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>
1、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>
2、使用自动装配,struts的控制组件自动装备service组件。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。