Spring:IOC or DI
<context:annotation-config/>
annotation:@autowire --default find by Type.
if annotation have two or more beans match ,use @qualifier();
parameter:require
@required --is similar @override
@Resource -- using the setter methods.
how to use: javax.annotation.resource
default by name ,if you want to by name use parameter(name="")
shortcoming:it must user have code.
<context:component-scan base-package=""/>
@Component --By default,classes annotated with @Component
@PostConstruct @Scope @PreDestroy
Hibernate used in spring
设计模式:Template Method(模板)
1.在sping中初始化HibernateTemplate,注入sessionFactory
2.DAO里注入HibernateTemplate
3.save里写getHibernateTemplate.save();
eg.
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.hibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>--注入sessionFactory
</bean>
通过类继承HibernateDaoSupport
通过抽象出一个类继承HibernateDaoSupport,bean只要配置一次把sessionFactory注入
实现类继承这个抽象类,即可完成Tmplate模板编程
AOP:aspect oriented Programing.
<aop:aspectj-autoproxy />
aspectj 专门使用动态代理的框架。
导入两个包:aspectjrt.jar aspectjwearer.jar
@Asjectj---@Before("execution(public void methodimpl)")
接着@component
AOP的XML编程(使用的多)
spring 和 hibernate的整合
dbcp:连接
<bean id="" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="" value=""/>
<bean/>
sessionFactory
<bean id="" class="org.springframeword.orm.hibernate3.LocalSessionFactory">不支持annotation
<bean id="" class="org.springframeword.orm.hibernate3.annotation.SessionFactory">支持annotation
<property name="dataSource" ref="dataSource"/>--必须设置dataSource属性
<property name="annotatedClasses">
<list>
<value>entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="">value</prop>
<prop key="">value</prop>
</props>
</property>
声明式的事务管理
spring中的hibernate的transaction
事务的边界 ----
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"/>
<property name="sessionFactory" ref="sessionFactory"/>
<tx:annotation-driver transaction-manager="txManager"/>
default:runtime异常自动回滚。
Transactional(propagetion="Propagetion.Required) :
property : Required(需要的) Mandatory(强制的) Nested(内嵌) Never Not_Supported
Required_New Supporis
XML中的实现:
<aop:config>
<aop:pointcut id="txAdvice" expression="execuion(public * com.cml.service..*.*(..))">
<aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="add*" propagetion="REQUIRED"/>
</tx:attributes>
</tx:advice>
//扫描指定包
<property name="packageToScan">
<list>
<value>package</value>
</list>
</property>