本文总结一下spring2.5.6的知识点。
一、spring IOC的配置与使用
1、IOC
简而言之,IOC为控制反转。将自己new出来的对象,改为有容器进行赋值。具体有以下两种情况
(1)初始化具体值 (2)灵活、动态的装配。
2、XML的配置
(1)、创建容器,命名为beans.xml,放在classpath下,大致配置如下
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="xx" class="com.test.xx"> </bean> </beans>
(2)注入类型
Setter注入方式
<bean id="x" class="com.test.xxx"> <property name="xxx" value="8"></property> </bean>
构造方法注入方式
<bean id="x" class="com.test.xxx"> <constructor-arg> <ref bean="u"/> </constructor-arg> </bean>//注意这种方式要进行构造方法的定义
接口注入方式
代码略
(3)简单属性的注入(int ,long,double)
<property name="xxx" value="8"></property>
(4)bean 中的scope属性
singleton表示的是单例,prototype表示的是多例
(5)集合注入<!--EndFragment-->
<!--EndFragment--><!--EndFragment-->
<bean name="x" class="com.test.xxx"> <property name="sets"> <set> <value>1</value> <value>2</value> </set> </property> <property name="lists"> <list> <value>1</value> <value>2</value> <value>3</value> </list> </property> <property name="maps"> <map> <entry key="1" value="1"></entry> <entry key="2" value="2"></entry> <entry key="3" value="3"></entry> <entry key="4" value="4"></entry> </map> </property> </bean>
(6)自动装配
autoWire bean中的一个属性,常用的有byname,bytype
(7)生命周期
lazy-init=“true”:当容器初始化的时候,对应的bean不进行初始化
init-method destroy-method不要和prototype一起用
3、Annotation的配置
(1)容器的配置
<!--EndFragment-->
(2)@autoWire的配置
默认按类型by type ,如果想用byName,使用@Qulifier, 如果写在set上,@qualifier需要写在参数上
(3)@Resource<!--EndFragment-->
需要加入 j2ee/common-annotations.jar,默认按名称,如果名称找不到,就按类型,可以 指定先按名称,不足:如果没有源码,就无法运用annotation,只能用xml
(4)@Component
初始化的名字默认为类名首字母小写,可以指定初始化bean的名字
(5)@scope,@PostConstruct,@preDestroy
用法略
二、spring AOP的配置与使用
1、annotation的配置
(1)xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package="com.bjsxt"/> <aop:aspectj-autoproxy /> <!-- <aop:aspectj-autoproxy />这句的作用是:容器可以解析aop对应的annotation了。我们可以用@Aspect注解来建立我们的拦截类,可以用@Before等注解某个方法来建立处理方法。 @component 可以让容器来管理我们的拦截类 --> </beans>
(2)annotation的具体例子
@Aspect @Component public class LogInterceptor { @Pointcut("execution(public * com.test.service..*.add(..))") public void myMethod(){}; @Before("myMethod()") public void before() { System.out.println("method before"); } @Around("myMethod()") public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable { System.out.println("method around start"); pjp.proceed(); System.out.println("method around end"); } }
(3)常见的annotation
@Pointcut @Before @AfterReturning @AfterThrowing @After @Around
2、xml的配置
(1)xml的具体例子
<bean id="interceptor" class="com.test.aop.Interceptor"></bean> <aop:config> <aop:aspect id="logAspect" ref="interceptor"> <aop:before method="before" pointcut="execution(public * com.test.service..*.add(..))" /> </aop:aspect> </aop:config>
三、Spring整合Hibernate
1、整合步骤
(1)创建datasource,将连接数据库的属性注入到datasource中
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <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>
(2)创建sessionFactory,将datasource注入到sessionFactory中
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="annotatedClasses"> <list> <value>com.test.Model.User</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean>
(3)将sessionFactory注入到dao层。
(4)jar包问题自己动手解决!
2、声明式事务管理
(1)事务一般加在service层。
(2)xml的配置
xmlns:tx="http://www.springframework.org/schema/tx" http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
(3)annotation的配置
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:annotation-driven transaction-manager="txManager"/>
然后再某个方法上加上: @Transactional(propagation=Propagation.REQUIRED)
注意不要使用opensession,要使用getcurrentsession
(4)xml配置
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="getUser" read-only="true"/> <tx:method name="add*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="busynessService" expression="execution(public * com.test.service.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="busynessService"/> </aop:config>
(4)HibernateTemplate和HibernateDaoSupport
通过向HibernateTemplate注入datasource,就可以用HibernateTemplate的save()等方法进行数据库数据的存取
HibernateDaoSupport的用法为:先用一个类A继承它,然后向A的父类注入HibernateTemplate或者sessionFactory,然后用自己的DAO层的实现类继承它,然后就可以使用了!
<!--EndFragment-->
<!--EndFragment--><!--EndFragment-->
分享到:
相关推荐
Spring学习总结文档,帮助快速入门。
Spring 学习总结 Spring 是一个广泛应用于企业级 Java 应用程序的开源框架,提供了一个灵活的、可扩展的、可维护的架构。Spring 框架主要分为七个模块:Core Container、AOP、ORM、Web、Web Services、JMS 和 Test...
struts2、hibernate、spring学习总结文档,struts2、hibernate、spring学习总结文档
Spring最新资料总结,包含SpringMVC,IOC,AOP面向切面编程,以及与Struts2,Hibernate的集成.
Spring学习笔记,包含了sping学习所有知识点,和例子一个综合的学习总结
文档对Spring的知识做了一个全面的总结,具体讲解Spring的控制反转、依赖注入、面向切面编程、Spring中的Bean配置、Spring对JDBC的支持以及Spring的声明式事务
Spring2学习总结Spring2学习总结Spring2学习总结Spring2学习总结Spring2学习总结Spring2学习总结Spring2学习总结Spring2学习总结
NULL 博文链接:https://uule.iteye.com/blog/811476
Spring Security学习总结
主要介绍了详解Spring学习总结——Spring实现AOP的多种方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
SpringSecurity学习总结源代码
spring学习笔记
课程学习时候的笔记总结,这里面没有代码部分,全是知识点的总结,方便大家去面试回答
Spring Security学习总结二
Spring MVC 学习记录总结Spring MVC 是目前主流的实现MVC设计模式的框架,是Spring 框架的一个分支产品,以 Spring IoC 容器
【狂神说】spring PDF学习总结笔记 Spring5.pdf
Spring核心技术学习总结 Spring核心技术学习总结