Spring

Spring

标签: JAVA框架


spring

一 简介

Spring框架
作用:Spring Core (IOC功能) Spring AOP功能
管理组件对象,维护对象关系
目的:降低耦合度
Spring Web MVC功能
MVC设计,
目的:架构一个MVC结构的Web程序
Spring整合其他技术,例如JDBC,MyBatis,Hibernate,Struts等

二 搭建环境

a、搭建spring(核心)的开发环境

下载对应的包 spring对应的包  commons-logging对应的包

b、String就是一个对象容器

在应用的构建路径中建立一个applicationContext.xml的配置文件

三 spring容器中创建对象的方式

1 spring容器自动加载无参的构造方法
2 静态工厂类的方式
3 实例工厂类的方式

四、别名

五、创建对象的时间

1默认是在spring容器加载的过程中加载
2懒加载 在Context.getBean的时候加载  lazy-init

六、多实例和单实例

通过scope属性设置
默认是单实例  singleton

可以设置为prototype  加载一次bean加载一次构造方法

七 init-method 和 destroy-method

init-method 可以给该bean类配置初始化时执行的方法
destroy-method spring容器关闭的时候 执行的方法  

举例

   
<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="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
          
        <property name="driverClass">  
            <value>com.mysql.jdbc.Drivervalue>  
        property>  
          
        <property name="jdbcUrl">  
            <value>jdbc:mysql://localhost:3306/eportal?useUnicode=  
                true&characterEncoding=gbkvalue>  
        property>  
          
        <property name="user">  
            <value>rootvalue>  
        property>  
          
        <property name="password">  
            <value>rootvalue>  
        property>  
          
        <property name="maxPoolSize">  
            <value>20value>  
        property>  
          
        <property name="minPoolSize">  
            <value>2value>  
        property>  
          
        <property name="initialPoolSize">  
            <value>2value>  
        property>  
          
        <property name="maxIdleTime">  
            <value>20value>  
        property>  
    bean>  
      
    <bean id="sessionFactory"  
        class="org.springframework.orm.  
hibernate3.LocalSessionFactoryBean">  
          
        <property name="dataSource" ref="dataSource" />  
          
        <property name="mappingResources">  
            <list>  
                <value>com/eportal/ORM/News.hbm.xmlvalue>  
                <value>com/eportal/ORM/Category.hbm.xmlvalue>  
                <value>com/eportal/ORM/Memberlevel.hbm.xmlvalue>  
                <value>com/eportal/ORM/Cart.hbm.xmlvalue>  
                <value>com/eportal/ORM/Traffic.hbm.xmlvalue>  
                <value>com/eportal/ORM/Newsrule.hbm.xmlvalue>  
                <value>com/eportal/ORM/Merchandise.hbm.xmlvalue>  
                <value>com/eportal/ORM/Admin.hbm.xmlvalue>  
                <value>com/eportal/ORM/Orders.hbm.xmlvalue>  
                <value>com/eportal/ORM/Cartselectedmer.hbm.xmlvalue>  
                <value>com/eportal/ORM/Newscolumns.hbm.xmlvalue>  
                <value>com/eportal/ORM/Member.hbm.xmlvalue>  
            list>  
        property>  
          
        <property name="hibernateProperties">  
            <props>  
                  
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>  
                  
                <prop key="show_sql">trueprop>  
                  
                <prop key="hibernate.jdbc.batch_size">50prop>  
                <prop key="show_sql">50prop>  
            props>  
        property>  
    bean>  
      
    <bean id="transactionManager"  
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
          
        <property name="sessionFactory" ref="sessionFactory" />  
    bean>  
      
    <bean id="transactionInterceptor"  
        class="org.springframework.transaction.interceptor.TransactionInterceptor">  
          
        <property name="transactionManager" ref="transactionManager" />  
          
        <property name="transactionAttributes">  
            <props>  
                  
                <prop key="browse*">PROPAGATION_REQUIRED,readOnlyprop>  
                <prop key="list*">PROPAGATION_REQUIRED,readOnlyprop>  
                <prop key="load*">PROPAGATION_REQUIRED,readOnlyprop>  
                <prop key="get*">PROPAGATION_REQUIRED,readOnlyprop>  
                <prop key="is*">PROPAGATION_REQUIRED,readOnlyprop>  
                  
                <prop key="*">PROPAGATION_REQUIREDprop>  
            props>  
        property>  
    bean>  
      
    <bean  
        class="org.springframework.aop.framework.autoproxy.    
BeanNameAutoProxyCreator">  
          
        <property name="beanNames">  
            <list>  
                <value>adminServicevalue>  
                <value>columnsServicevalue>  
                <value>newsServicevalue>  
                <value>crawlServicevalue>  
                <value>memberLevelServicevalue>  
                <value>memberServicevalue>  
                <value>categoryServicevalue>  
                <value>merServicevalue>  
                <value>cartServicevalue>  
                <value>ordersServicevalue>  
                <value>trafficServicevalue>  
            list>  
        property>  
          
        <property name="proxyTargetClass">  
            <value>truevalue>  
        property>  
          
        <property name="interceptorNames">  
            <list>  
                <value>transactionInterceptorvalue>  
            list>  
        property>  
    bean>  
      
    <bean id="dao" class="com.eportal.DAO.BaseDAOImpl">  
        <property name="sessionFactory" ref="sessionFactory" />  
    bean>  
      
    <bean id="adminService" class="com.eportal.service.AdminServiceImpl">  
        <property name="dao" ref="dao" />  
    bean>  
      
    <bean id="columnsService" class="com.eportal.service.ColumnsServiceImpl">  
        <property name="dao" ref="dao" />  
    bean>  

      
    <bean id="ordersService" class="com.eportal.service.OrderServiceImpl">  
        <property name="dao" ref="dao" />  
    bean>  
      
    <bean id="trafficService" class="com.eportal.service.TrafficServiceImpl">  
        <property name="dao" ref="dao" />  
    bean>  
      
    <bean id="adminAction" class="com.eportal.struts.action.  
AdminAction"  
        scope="prototype">  
        <property name="service" ref="adminService" />  
    bean>  
      
    <bean id="columnsAction" class="com.eportal.struts.action.  
ColumnsAction"  
        scope="prototype">  
        <property name="service" ref="columnsService" />  
    bean>  
      
    <bean id="newsAction" class="com.eportal.struts.action.  
NewsAction"  
        scope="prototype">  
        <property name="service" ref="newsService" />  
        <property name="columnsService" ref="columnsService" />  
    bean>  
      
    <bean id="crawlAction" class="com.eportal.struts.action.  
CrawlAction"  
        scope="prototype">  
        <property name="service" ref="crawlService" />  
        <property name="columnsService" ref="columnsService" />  
    bean>  

beans>

你可能感兴趣的:(java学习笔记,ssh)