ssh学习笔记

环境:eclipse+spring+springmvc+hibernate

用分层思想实现程序结构会化提供更大的的灵活性

目录

环境:eclipse+spring+springmvc+hibernate

面向接口编程:

1.增加DAO接口成层

    2.service接口层

    3自动装配(只能是setter的方式装配,不能以构造函数的形式自动装配)

    4注入方式

通过注解方式实现IOC

AOP基础

注解配置和xml配置aop学习注解配置

Spring AOP 的一些术语

项目需求分析


面向接口编程:


让程序不依赖具体的实现
使用接口编程,易于程序扩展,降低程序耦合性


1.增加DAO接口成层


 把用户信息存入文本文件中(改变hi本人纳特配置文件是行不通的)
 接口:只关心功能,不关系实现
 
在xml对Ioc进行配置,让spring完成bean的初始化和装配工作


    2.service接口层


    userservice 的实现类 UserSwerviceImpl里调用Dao来与数据库打交道
    DAO层->service层->action->jsp

    IOC=inverse of control =控制反转
    DI=dependency injection =依赖注入:类运行是需要另一个对象,spring可以将这个对象给装配进来
    在xml


    3自动装配(只能是setter的方式装配,不能以构造函数的形式自动装配)


    autowire:byType,(默认是byType,可以设置为byName)

    @Resource byName自动装配
    default-autowire:默认的自动装配


    4注入方式


    1).setter方式注入property
    2) .构造方法注入
    5不同属性的注入方式,只做了解
    简单属性注入:int,String,float
    集合属性注入:Map,List,Set
    6,单例和多例
      scope = singleton,prototype,request,session,global session
    默认是singleton 单例,prototype 原型,模板

    单例:只实例化一次,构造函数为private,由静态方法调用私有构造函数实例化一个对象    7.延时加载
    lazy-init 容器加载时不初始化
    default-lazy-init
    8.初始化方法和销毁方法
    init-method 和 destroy-method
    


    <?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.xsd">
    <!-- 通过类生成对象  mydao,起到了new的作用 -->
    <bean id="mydao" class="net.xinqushi.dao.impl.UserDaoFileImpl"
        init-method="start" destroy-method="end">
    </bean>
    <!-- id,name 要与setterv里面的的参数名字一致 -->
    <!-- 自动装配 autowire  byName ,byType-->
    <bean id="userService"
        class="net.xinqushi.service.impl.UserServiceImpl" scope="">
        <constructor-arg name="dao" ref="mydao"></constructor-arg>
    </bean>

</beans>

通过注解方式实现IOC

1,首先配置spring,支持注解
配置xml命名空间 ns=namespace
xsd文件,用来约束xml文件的语法和格式
约束xml文件有两种标准
dtd(.dtd) ;schema(.xsd)
xmlns:context="http://www.springframework.org/schema/context"

http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
 
<context:annotation-config/>
2.初始化和装配
<bean id="" class=""/>

 singleton / prototype

 spring注解写在set方法上,hibernate注解写在get方法上。


@component:在spring容器中初始化一个bean

<context:component-scan base-package="net.xinqushi.dao.impl,net.xinqushi.service.impl"/>

@resource:装配bean
@scope:设置单实例与多实例
配置ioc选择注解还是xml,关键看是否有项目源码


AOP基础

之一:扩展程序的功能的办法之一----继承方法
会生成很多的代理,不方便管理
之二:组合的方式来扩展

实现接口来说达到组合,比继承好一些,但是代理类还是挺多
之三 动态代理

代理对象 和 被代理对象
被代理对象实现基础功能   :user
代理对象在被代理的基础上,实现附加功能 userLog

步骤:
1,代理类实现接口InvocationHandler
2,成员属性:被代理类对象,object
3,invoke方法里执行被代理对象的方法,同时可以在里面添加功能

动态代理优点
作用:可以在现有的方法之上添加代码,增加功能
1.将与业务无关的通用功能抽取出来,单独编写
    开发人员可以专注于业务逻辑的编写
2,通用功能的增加和删除,可以通过配置文件进行配置
3,一种编程方式的革命
    引入AOP:面向切面的编程
AOP=面向切面编码=Aspect Oriented Programming
技术原理:动态代理

OOP
OOP=Object Oriented Programming
AOP 类似jsp里面的filter(过滤器),struts2的intercepror(拦截器)

注解配置和xml配置aop
学习注解配置

以log日志为例
切面日志类

/**
 * 切面类
 * @author Administrator
 *
 */
@Aspect
@Component(value="log")//表明需要spring来初始化这个类,@Conponent是指容器会初始化对象log
public class Log {

    public Log() {
        
    }
    //指定mybefore加到某个方法之前
    @Before("execution(public void net.xinqushi.service.impl.UserServiceImpl.add(net.xinqushi.model.User))")
    public void mybefore()
    {
        System.out.println("方法执行开始");
    }
    //指定myend加到某个方法之后
    @AfterReturning("execution(public void net.xinqushi.service.impl.UserServiceImpl.add(net.xinqushi.model.User))")
   public void myend() {
       System.out.println("方法执行结束");
   }
}

applicationContext.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"
    <!--aop部分-->
    xmlns:aop="http://www.springframework.org/schema/aop"
    
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        <!--aop部分-->
         http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
        
    <context:annotation-config/>
    <context:component-scan base-package="net.xinqushi.dao.impl,net.xinqushi.service.impl,net.xinqushi.aop"></context:component-scan>
   <!--aop部分-->
   <aop:aspectj-autoproxy/>
</beans>


Spring AOP 的一些术语


    切面(Aspect)
    要增加什么功能? 事务管理,日志管理,权限管理
    连接点(Joinpoint)
    那个类的哪个方法上增加功能
    @Before("execution(public void net.xinqushi.service.impl.UserServiceImpl.add(net.xinqushi.model.User))")
    通知(Advice)
    加在方法的什么位置?前面?后面?产生异常时?环绕?
    切入点(Pointcut)
     给连接点命名,便于在多处使用)
     目标对象(Target Object)
     被代理对象,自己的功能不够强大,要通过aop加强
     AOP代理(AOP Proxy)
      功能已被加强的哪个对象
      织入(Weaving)
       把新功能和本身的功能组织在一起执行
1.Jointpoint语法:用一些通配符来定义哪些类和方法上增加aop功能

  语法:
  1).execution(public * *(..))
    所有public方法 
 
  2).execution(* set*(..))
    所有以set作为方法名开头的方法

  3).exuection(* com.xzy.service.AccountService.*(..))
   com.xzy.service.AccountService类中的所有方法

  4).execution(* com.xyz.service.*.*(..))
   com.xyz.service包中所有类的所有方法

  5).execution(* com.xyz.service..*.*(..))
   com.xyz.service包及子包下的所有类的所有方法

2.pointcut:给joinpoint取个名字。
  @pointcut("execution( * com.xzy.*.*(..))")
  public void businessService(){}

3.advice 

 @Before
 @AfterReturning
 @AfterThrowing
 @After(finally)
 @Around


4.JoinPoint jp
  ProceedingJointPoint pjp

5.xml配置AOP

<aop:config>
   <aop:aspect ref="log">
     <aop:pointcut 
            expression="execution(* net.xinqushi.dao.impl.*.*(..))" id="pt"/>
    <aop:before method="before" pointcut-ref="pt"/>
    <aop:after method="after" pointcut-ref="pt"/>
   </aop:aspect>
</aop:config>


XML配置实现AOP

.spring和struts的整合
  加入struts2-spring-plugin-2.3.24.jar插件包即可,配置文件不需要整合,整合过程比较简单。

  web.xml

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

   ContextLoaderListener实现ServletContextListener接口,在服务器启动时加载,
   默认自动载入置于WEB-INF目录下的Spring配置文件applicationContext.xml

2.spring和hibernate的整合

  参考文档

  http://www.open-open.com/lib/view/open1414743802278.html
  http://blog.csdn.net/qq7342272/article/details/7928814

 (1)引入申明式事务支持的XML命名空间

  xmlns:tx="http://www.springframework.org/schema/tx"

  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

  (2)dataSource配置
   
    占位符
    
    常用的连接池

    (i)apache的数据源 org.apache.commons.dbcp.BasicDataSource           
    (ii)阿里巴巴的数据源 com.alibaba.druid.pool.DruidDataSource
    (iii)c3p0 com.mchange.v2.c3p0.ComboPooledDataSource
    (iv)Proxool

   

<!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <!-- 初始化连接数量; -->
        <property name="initialSize" value="0" />
        <!-- 最大并发连接数 -->
        <property name="maxActive" value="20" />
        <!-- 最大空闲连接数 -->
        <property name="maxIdle" value="20" />
        <!-- 最小空闲连接数 -->
        <property name="minIdle" value="0" />
        <!-- 最大等待时长 -->
        <property name="maxWait" value="60000" />
        <!-- 超过时间限制是否回收 -->
        <property name="removeAbandoned" value="true" />
        <!-- 超过时间限制多长; -->
        <property name="removeAbandonedTimeout" value="180"/>     
          
          
        <!-- 数据源连接参数配置; -->
        <property name="username" value="${db.username}"/>
        <property name="url" value="${db.url}"/>
        <property name="password" value="${db.password}"/>
        <property name="driverClassName" value="${db.driverClassName}"/>
          
    </bean> 


   
   (3)读取外部属性文件,获取数据源参数

  <context:property-placeholder location="classpath:dataSource.properties/>

   (4)sessionFactory配置
   

 <!-- 配置SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>    
        <property name="packagesToScan" value="net.xinqushi.model.model"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
            </props>
        </property>
    </bean>

  (5)配置事务管理器
   

 <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean> 

 (6)初始化hibernateTemplate

  <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
   <property name="sessionFactory" ref="sessionFactory"></property>
  </bean>

  (7)配置事务管理
        
 

  <!-- 定义切面 -->
    <aop:config>
        <aop:pointcut expression="execution(* net.xinqushi.service.impl.*.* (..))" id="txPointCut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>
      
    <!-- 声明式事务 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">            
        <tx:attributes>
          <tx:method name="add*" propagation="REQUIRED"/>
          <tx:method name="get*" read-only="true" propagation="REQUIRED"/>
        </tx:attributes>        
    </tx:advice>

项目需求分析


注册:
注册是判断用户是否已注册
登录用户可以选择注销

数据结构分析
实体:
1,用户
User(id,userName,userPwd)
2,相册
Album(id,uid,name,cover)
关系:
一个用户对应多个相册
类设计:
站在用户角度,每个用户可以获得所有相册,通过
getAlbums()拿到所有相册
在user这边有一个属性 Set<Album> albums=new HashSet<Album>();
在album角度,可以通过getUser()拿到对应的用户

猜你喜欢

转载自blog.csdn.net/qq_36922927/article/details/81277176