spring一些知识点总结,包括spring和hibernate,spring和struts2的整合

最近学习spring的一些知识点的总结


Spring总结

Spring分为六大模块:

                    Spring—core :核心支持,包括对象创建

                    Spring—orm :对Hibernate的支持

                    Spring—aop :面向切面编程

                    Spring—EE :对javaEE其他模块的支持

                    Spring—web :对web模块的支持,与Struts整合

                    Spring—dao :对Jdbc的支持,JdbcTemplate

核心包有五个:

            Spring—core.jar

            Spring—context.jar

            Spring—expression.jar

            Spring—beans.jar

             Common—logging.jar

使用流程:

1.    引入核心jar包

2.    Application.xml / bean.xml

 <bean id=””  class=””  init_method=””  destroy_method=””  lazy_init=”true”  scope=””></bean>

   id:    名字

  class:  类全名

  init_method: 初始化方法

   destroy_method: 销毁方法

  lazy_init: true时懒加载,只对singleton有效

  scope:singleton / prototype

 

3.    获取Ioc容器

ApplicationContext ac = new ClassPathXmlApplicationContext(“xml地址”);

ac.getBean(“”);

 

 

 

IOC

    控制反转:是什么被反转了呢?

获得对象的过程由程序管理变为由IoC容器主动注入,叫做依赖注入。控制反转后,由ioc容器来处理对象之间的依赖关系

创建对象,及处理对象之间的依赖关系

DI:dependency injection

是ioc的一种表现形式

 

创建对象有3种方法

1.    默认无参构造方法创建

<bean  id=””  class=””> </bean>

 

2.    有参构造方法创建

<bean  id=””  class=””>

   <constructor—arg index=””type=””  value=””></>

</bean>

 

3.    使用工厂类创建

<bean  id=”beanFactory” class=”工厂类全名” ></bean>

<bean  id=””  factory_bean=” beanFactory”  factory_method=”方法”/>

 

使用静态方法时

<bean  id=””  class=”工厂类全名” factory_method=” 静态方法”/>

 

XML给属性fu值5种方法

1.    使用构造方法

<bean  id=””  class=””>

  <constructor—arg index=””type=”” value=””>

</bean>

2.    使用set方法

<bean  id=””  class=””>

  <property name=””  value=””></>

</bean>

3.    使用<bean>标签嵌套

4.    使用p命名空间

<bean  id=”userDao”  class=””/>

<bean  id=”userService” class=””  p:userDao—ref=”userDao/>” 

userDao要和属性名一致

  <bean  id=”user”  class=”” p:name=”jack”/>

jack要和属性名一致

5.    自动装配

<bean  id=””  class=”” autowire=”byName”/>

<bean  id=””  class=”” autowire=”byType”/>

自动装配是根据姓名或类型,自动在ioc容器里找一致的注入,当使用类型时,ioc容器里不能有两个一样类型的bean,否则会报错。

可以配置全局自动装配 default—autowire=”byXXX”

 

注解给属性fu值

1.    引入context名称空间

2.    开启注解扫描

<context:component—scan ></context:component—sacn>

3.    使用注解

@Component(“”)

    @Repository

    @Service

    @Contoller

  @Resource(name=””)

当括号里有名字的时候是根据名字自动装配,当括号里什么都不写时,是按类型自动装配。

 

 

AOP

代理模式:是一种设计模式,核心是代理对象和目标对象,为目标对象创建一个代理对象,用户通过代理对象,对请求进行过滤后访问到目标对象。

  用户=====代理对象=====目标对象

两种代理模式:静态代理、动态代理

1.    静态代理:

  代理对象需要实现目标对象所实现的接口

2.    动态代理:

Jdk代理、cglib代理

JDK代理

代理类proxyFactory

//创建目标对象

private  Object  target;

setTarget()

 

public  Object  getProxyInstance(){

        return  new Proxy.newProxyInstance(

target.getClass.getClassLoader(), //类加载器

target.getClass.getInterfaces(),   //目标方法实现的接口

new InvocationHandler(){        //事件处理器

 

        public  Object invoke(Object proxy,Mehtod,method,Object[] args){

                     

//执行目标方法之前的操作

Object returnValue = target.invoke(target,args); //执行目标方法

//执行目标方法之后的操作

}

}

);

}

使用JDK代理要求目标对象必须实现接口,当目标对象没有实现接口时使用Cglib代理

 

Cglib代理

 为目标对象创建一个子类对象,当目标对象是final类时不能用Cglib代理,当方法是final / static时不能拦截方法

 ProxyFactory类 implements MethodInterceptor

     private Object target;

    setTarget()

    public Object getProxyInstance(){

              Enhanceren  = new Enhancer();

       en.setSuperClass(target.getClass());

       en.setCallBack(this);

       return  en.create();

}

    @Override

     PublicObject incercep(Object obj,Method method,Object[] args){

//执行目标方法之前的操作

Object returnValue = target.invoke(target,args); //执行目标方法

//执行目标方法之后的操作

 

}

 

AOP使用的是动态代理,当目标对象实现接口时,使用JDK代理,当目标对象没有实现接口时自动使用Cglib代理

 

XML使用AOP

1.    引入相关jar

               Spring—aop.jar

               Aopalliance.jar

               Aspectweaver.jar

               Aspectjrt.jar

2.    引入aop名称空间

3.    配置切面类

<bean  id=”aop”  class=””/>

<aop:config>

        <aop:pointcutexpression=”execution(* com.dao.UserDao.save(..))”  id=”pt”/> //切入点表达式

    <aop:aspect  ref=”aop”>

        <aop:before method=””  point—ref=”pt”/>

<aop:after method=””  point—ref=”pt”/>

<aop:around method=””  point—ref=”pt”/>

<aop:after—returning  method=”” point—ref=”pt”/>

<aop:after—throwing  method=”” point—ref=”pt”/>

      </aop:aspect>

   </aop:config>

 

注解使用AOP

1.    引入相关jar

2.    引入aop名称空间

3.    开启注解扫描

<aop:aspectj—atuoproxy></aop:aspect—autoproxy>

@Aspect         //切面类

@Pointcut       //切入点表达式

@Before         //前置通知

@After          //后置通知

@Around        //环绕通知

@AfterReturning   //返回后通知

@AfterThrowing    //异常通知

 

执行顺序:

        环绕前

        前置

        目标方法

        环绕后

        后置

        返回后通知

对JDBC支持

提供了JdbcTemplate工具类

 

1.    引入Spring—jdbc.jar

    Spring—tx.jar

     C3p0.jar

2.    使用C3P0连接池当作数据源,并配置JdbcTemplate

<bean  id=”dataSource”  class=”com.mchange.v2.c3p0.ComboPooledDataSource”>

              <property  name=”driverClass”  value=””/>

<property  name=”jdbcUrl”  value=””/>

<property  name=”user”  value=””/>

<property  name=”password”  value=””/>

<property  name=”initialPoolSize”  value=””/>

<property  name=”maxPoolSize”  value=””/>

<property  name=”maxStatements”  value=””/>

<property  name=”acquireIncrement”  value=””/>

</bean>

<bean id=”jdbcTemplate”  class=”org.springframework.jdbc.core.JdbcTemplate”>

              <property  name=”dataSource”  ref=”dataSource”/>

</bean>

3.    Dao类中

Private  JdbcTemplate  jdbcTemplate;

setJdbcTemplate()

 

jdbcTemplate.update(sql);

List<User>  list  = jdbcTemplate.query(sql,

new  RowMapper<User>(){

    public  User mapRow(Result rs,Int index){

User  u  = new  User():

                     u.setUsername(rs.getString(“username”));

                     return u;

}    

});

 

 

事务控制

事务控制通过aop来实现

声明式事务管理

    粗粒度管理,对方法作用,因为aop只拦截方法

    Jdbc技术:DataSourceTransactionManager

   Hibernate技术:HibernateTransactionManager

XML声明事务

    //配置事务管理器类

<bean id=”txManager”  class=”org.springframework.jdbc.datasource.DataSourceTransactionManager”>

              <property  name=”dataSource”  ref=” dataSource”/>

</bean>
    //配置事务增强

<tx:advice  id=”txAdvice ”  transaction—manager=”txManager”>

    <tx:attributes>

                <tx:method name=”get*”  read—only=”true”>

                <tx:method name=”get*”  read—only=”true”>

    <tx:attributes>

</tx:advice>

   //aop配置

<aop:config>

              <aop:pointcut  expression=”” id=”pt”/>

              <aop:advosior  advice—ref=” txAdvice”  point—ref=”pt”/>

</aop:config>

 

注解使用aop要简化许多

用bean定义 dataSource

            jdbcTemplate

            dataSourceTransactionManager

开启注解扫描

<tx:annotation—driven  transaction—manager=”txManager”/>

   @Transactional(

              readOnly= false,

       timeout = -1,

              noRallbackFor= ArithmeticException.class,

              isolation= Isolation.DEFAULT,

              propagation= Propagation.REQUIRED

 

propagation = Propagation.REQUIRED

当前方法必须在事务下执行

如果已经存在事务,就加入到当前事务

propagation = Propagation.REQUIRED_NEW

当前方法必须在事务下执行

如果当前存在事务,则当前事务挂起,自己开始一个新的事务,完毕后挂起事务再继续。

 

@Transactional定义位置

定义在方法上,当前方法应用事务

定义在类上,整个类中的方法应用事务

定义在父类上,执行父类方法的时候应用

 

 

 

 

 

Spring和Struts2整合

1、 引入jar包

          Struts2必须jar包

          Spring—web.jar

          Struts—Spring—plugin.jar

2、 Struts.xml

Bean.xml

Web.xml

 

//配置spring

<context—param>

 <param—name>contextConfigLocation</param—name>

<param—value>/WEB-INF/classes/bean*.xml</>

<context—param>

<listener>

<listener—class>org.springframework.web.context.ContextLoaderListener</>

</listener>

   

//配置Struts

<filter>

   <filter—name>struts2</filter—name>

   <filter—class>

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

</filter—class>

</filter>

<filter—mapping>

   <filter—name>struts2</filter—name>

   <url—pattern>/*</url—pattern>

</filter—mapping>

3、 Struts.xml中,

<action  name=””  class=”IOC容器中bean的名字”  method=””>

 

 

 

Spring和Hibernate整合

Spring和hibernate整合其实就是

Spring总结

Spring分为六大模块:

                    Spring—core :核心支持,包括对象创建

                    Spring—orm :对Hibernate的支持

                    Spring—aop :面向切面编程

                    Spring—EE :对javaEE其他模块的支持

                    Spring—web :对web模块的支持,与Struts整合

                    Spring—dao :对Jdbc的支持,JdbcTemplate

核心包有五个:

            Spring—core.jar

            Spring—context.jar

            Spring—expression.jar

            Spring—beans.jar

             Common—logging.jar

使用流程:

1.    引入核心jar包

2.    Application.xml / bean.xml

 <bean id=””  class=””  init_method=””  destroy_method=””  lazy_init=”true”  scope=””></bean>

   id:    名字

  class:  类全名

  init_method: 初始化方法

   destroy_method: 销毁方法

  lazy_init: true时懒加载,只对singleton有效

  scope:singleton / prototype

 

3.    获取Ioc容器

ApplicationContext ac = new ClassPathXmlApplicationContext(“xml地址”);

ac.getBean(“”);

 

 

 

IOC

    控制反转:是什么被反转了呢?

获得对象的过程由程序管理变为由IoC容器主动注入,叫做依赖注入。控制反转后,由ioc容器来处理对象之间的依赖关系

创建对象,及处理对象之间的依赖关系

DI:dependency injection

是ioc的一种表现形式

 

创建对象有3种方法

1.    默认无参构造方法创建

<bean  id=””  class=””> </bean>

 

2.    有参构造方法创建

<bean  id=””  class=””>

   <constructor—arg index=””type=””  value=””></>

</bean>

 

3.    使用工厂类创建

<bean  id=”beanFactory” class=”工厂类全名” ></bean>

<bean  id=””  factory_bean=” beanFactory”  factory_method=”方法”/>

 

使用静态方法时

<bean  id=””  class=”工厂类全名” factory_method=” 静态方法”/>

 

XML给属性fu值5种方法

1.    使用构造方法

<bean  id=””  class=””>

  <constructor—arg index=””type=”” value=””>

</bean>

2.    使用set方法

<bean  id=””  class=””>

  <property name=””  value=””></>

</bean>

3.    使用<bean>标签嵌套

4.    使用p命名空间

<bean  id=”userDao”  class=””/>

<bean  id=”userService” class=””  p:userDao—ref=”userDao/>” 

userDao要和属性名一致

  <bean  id=”user”  class=”” p:name=”jack”/>

jack要和属性名一致

5.    自动装配

<bean  id=””  class=”” autowire=”byName”/>

<bean  id=””  class=”” autowire=”byType”/>

自动装配是根据姓名或类型,自动在ioc容器里找一致的注入,当使用类型时,ioc容器里不能有两个一样类型的bean,否则会报错。

可以配置全局自动装配 default—autowire=”byXXX”

 

注解给属性fu值

1.    引入context名称空间

2.    开启注解扫描

<context:component—scan ></context:component—sacn>

3.    使用注解

@Component(“”)

    @Repository

    @Service

    @Contoller

  @Resource(name=””)

当括号里有名字的时候是根据名字自动装配,当括号里什么都不写时,是按类型自动装配。

 

 

AOP

代理模式:是一种设计模式,核心是代理对象和目标对象,为目标对象创建一个代理对象,用户通过代理对象,对请求进行过滤后访问到目标对象。

  用户=====代理对象=====目标对象

两种代理模式:静态代理、动态代理

1.    静态代理:

  代理对象需要实现目标对象所实现的接口

2.    动态代理:

Jdk代理、cglib代理

JDK代理

代理类proxyFactory

//创建目标对象

private  Object  target;

setTarget()

 

public  Object  getProxyInstance(){

        return  new Proxy.newProxyInstance(

target.getClass.getClassLoader(), //类加载器

target.getClass.getInterfaces(),   //目标方法实现的接口

new InvocationHandler(){        //事件处理器

 

        public  Object invoke(Object proxy,Mehtod,method,Object[] args){

                     

//执行目标方法之前的操作

Object returnValue = target.invoke(target,args); //执行目标方法

//执行目标方法之后的操作

}

}

);

}

使用JDK代理要求目标对象必须实现接口,当目标对象没有实现接口时使用Cglib代理

 

Cglib代理

 为目标对象创建一个子类对象,当目标对象是final类时不能用Cglib代理,当方法是final / static时不能拦截方法

 ProxyFactory类 implements MethodInterceptor

     private Object target;

    setTarget()

    public Object getProxyInstance(){

              Enhanceren  = new Enhancer();

       en.setSuperClass(target.getClass());

       en.setCallBack(this);

       return  en.create();

}

    @Override

     PublicObject incercep(Object obj,Method method,Object[] args){

//执行目标方法之前的操作

Object returnValue = target.invoke(target,args); //执行目标方法

//执行目标方法之后的操作

 

}

 

AOP使用的是动态代理,当目标对象实现接口时,使用JDK代理,当目标对象没有实现接口时自动使用Cglib代理

 

XML使用AOP

1.    引入相关jar

               Spring—aop.jar

               Aopalliance.jar

               Aspectweaver.jar

               Aspectjrt.jar

2.    引入aop名称空间

3.    配置切面类

<bean  id=”aop”  class=””/>

<aop:config>

        <aop:pointcutexpression=”execution(* com.dao.UserDao.save(..))”  id=”pt”/> //切入点表达式

    <aop:aspect  ref=”aop”>

        <aop:before method=””  point—ref=”pt”/>

<aop:after method=””  point—ref=”pt”/>

<aop:around method=””  point—ref=”pt”/>

<aop:after—returning  method=”” point—ref=”pt”/>

<aop:after—throwing  method=”” point—ref=”pt”/>

      </aop:aspect>

   </aop:config>

 

注解使用AOP

1.    引入相关jar

2.    引入aop名称空间

3.    开启注解扫描

<aop:aspectj—atuoproxy></aop:aspect—autoproxy>

@Aspect         //切面类

@Pointcut       //切入点表达式

@Before         //前置通知

@After          //后置通知

@Around        //环绕通知

@AfterReturning   //返回后通知

@AfterThrowing    //异常通知

 

执行顺序:

        环绕前

        前置

        目标方法

        环绕后

        后置

        返回后通知

对JDBC支持

提供了JdbcTemplate工具类

 

1.    引入Spring—jdbc.jar

    Spring—tx.jar

     C3p0.jar

2.    使用C3P0连接池当作数据源,并配置JdbcTemplate

<bean  id=”dataSource”  class=”com.mchange.v2.c3p0.ComboPooledDataSource”>

              <property  name=”driverClass”  value=””/>

<property  name=”jdbcUrl”  value=””/>

<property  name=”user”  value=””/>

<property  name=”password”  value=””/>

<property  name=”initialPoolSize”  value=””/>

<property  name=”maxPoolSize”  value=””/>

<property  name=”maxStatements”  value=””/>

<property  name=”acquireIncrement”  value=””/>

</bean>

<bean id=”jdbcTemplate”  class=”org.springframework.jdbc.core.JdbcTemplate”>

              <property  name=”dataSource”  ref=”dataSource”/>

</bean>

3.    Dao类中

Private  JdbcTemplate  jdbcTemplate;

setJdbcTemplate()

 

jdbcTemplate.update(sql);

List<User>  list  = jdbcTemplate.query(sql,

new  RowMapper<User>(){

    public  User mapRow(Result rs,Int index){

User  u  = new  User():

                     u.setUsername(rs.getString(“username”));

                     return u;

}    

});

 

 

事务控制

事务控制通过aop来实现

声明式事务管理

    粗粒度管理,对方法作用,因为aop只拦截方法

    Jdbc技术:DataSourceTransactionManager

   Hibernate技术:HibernateTransactionManager

XML声明事务

    //配置事务管理器类

<bean id=”txManager”  class=”org.springframework.jdbc.datasource.DataSourceTransactionManager”>

              <property  name=”dataSource”  ref=” dataSource”/>

</bean>
    //配置事务增强

<tx:advice  id=”txAdvice ”  transaction—manager=”txManager”>

    <tx:attributes>

                <tx:method name=”get*”  read—only=”true”>

                <tx:method name=”get*”  read—only=”true”>

    <tx:attributes>

</tx:advice>

   //aop配置

<aop:config>

              <aop:pointcut  expression=”” id=”pt”/>

              <aop:advosior  advice—ref=” txAdvice”  point—ref=”pt”/>

</aop:config>

 

注解使用aop要简化许多

用bean定义 dataSource

            jdbcTemplate

            dataSourceTransactionManager

开启注解扫描

<tx:annotation—driven  transaction—manager=”txManager”/>

   @Transactional(

              readOnly= false,

       timeout = -1,

              noRallbackFor= ArithmeticException.class,

              isolation= Isolation.DEFAULT,

              propagation= Propagation.REQUIRED

 

propagation = Propagation.REQUIRED

当前方法必须在事务下执行

如果已经存在事务,就加入到当前事务

propagation = Propagation.REQUIRED_NEW

当前方法必须在事务下执行

如果当前存在事务,则当前事务挂起,自己开始一个新的事务,完毕后挂起事务再继续。

 

@Transactional定义位置

定义在方法上,当前方法应用事务

定义在类上,整个类中的方法应用事务

定义在父类上,执行父类方法的时候应用

 

 

 

 

 

Spring和Struts2整合

1、 引入jar包

          Struts2必须jar包

          Spring—web.jar

          Struts—Spring—plugin.jar

2、 Struts.xml

Bean.xml

Web.xml

 

//配置spring

<context—param>

 <param—name>contextConfigLocation</param—name>

<param—value>/WEB-INF/classes/bean*.xml</>

<context—param>

<listener>

<listener—class>org.springframework.web.context.ContextLoaderListener</>

</listener>

   

//配置Struts

<filter>

   <filter—name>struts2</filter—name>

   <filter—class>

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

</filter—class>

</filter>

<filter—mapping>

   <filter—name>struts2</filter—name>

   <url—pattern>/*</url—pattern>

</filter—mapping>

3、 Struts.xml中,

<action  name=””  class=”IOC容器中bean的名字”  method=””>

 

 

 

Spring和Hibernate整合

Spring和hibernate整合其实就是


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值