spring

1.Spring是什么?

Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许您 选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框架。

 

2.Spring特点?

(1)IOC(控制反转)或DI(依赖注入):明确定义组件的接口,独立开发各个组件,然后根据组件的依赖关系组装运行;即将创建及管理对象的权利交给Spring容器。Spring是一个轻型容器(light-weight Container),其核心是Bean工厂(Bean Factory),用以构造我们所需要的M(Model)。能够让相互协作的软件组件保持松散耦合。降低了业务对象替换的复杂性,提高了组件之间的解耦。

(2)AOP(面向切面编程):通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术。即系统级的服务从代码中解耦出来。例如:将日志记录,性能统计,安全控制,事务处理,异常处理等代码从业务逻辑代码中划分出来。允许你把遍布应用各处的功能分离出来形成可重用组件。

 

3.Spring框架好处?

(1)控制反转:Spring通过控制反转实现了松散耦合,对象们给出它们的依赖,而不是创建或查找依赖的对象们。

(2)面向切面的编程(AOP):Spring支持面向切面的编程,并且把应用业务逻辑和系统服务分开

(3)MVC框架:Spring的WEB框架是个精心设计的框架,是Web框架的一个很好的替代品。

(4)低侵入式设计,代码污染极低,独立于各种应用服务器,基于Spring框架的应用,可以真正实现Write Once,Run Anywhere的承诺。

(5)集成能力强:集成多种优秀的开源框架。(Hibernate、Struts、Hessian等)。

(6)异常处理:Spring 提供方便的API把具体技术相关的异常(比如由JDBC,Hibernate or JDO抛出的)转化为一致的unchecked 异常。

(7)容器:Spring 包含并管理应用中对象的生命周期和配置。

(8)轻量:Spring 是轻量的,基本的版本大约2MB。

4.Spring基本组成模块?

(1)CoreContain模块:Core、bean、context、Expression Language。

(2) Data Access/integration(集成)模块:JDBC、ORM、OXM、JMS、Transaction.

(3)Web模块:WEB、Web-Servle、Web-Struts、Web-Portlet。

(4)AOP、Aspects、Instrumentation、Test.

 

5.Spring核心组件详解

 

Spring核心组件只有Core、Context、Beans三个。core包侧重于帮助类,操作工具,beans包更侧重于bean实例的描述。context更侧重全局控制,功能衍生。

 

spring测试

 

复制代码
package test;


import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.pang.dao.UserMapper;
import com.pang.domain.User;

public class MyBatisSpringTest {

    public static void main(String[] args) {
        
    }
    @Test
    public void test() {
        //加载spring配置
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取对象org.mybatis.spring.mapper.MapperFactoryBean.class
        UserMapper userMapper=(UserMapper)ac.getBean("userMapper");
        //调用方法
        User user=new User();
        user.setName("joe");
        user.setPassword("6788");
        userMapper.saveUser(user);
        System.out.println("添加已经成功");
    }
}
复制代码

applicationContext.xml

复制代码
<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"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">
        
        <!--读取jdbc.proerties-->
        <context:property-placeholder location="classpath:jdbc.properties"/>
        
        <!--创建DateRource-->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="url" value="${jdbc.url}"/>
            <property name="driverClassName" value="${jdbc.driverClass}"/>
            <property name="username" value="${jdbc.user}"/>
            <property name="password" value="${jdbc.password}"/>
            <property name="maxActive" value="10"/>
            <property name="maxIdle" value="5"/>
        </bean>
        
        <!--创建SqlSessionFactory对象-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
                <!--关联连接池-->
                <property name="dataSource" ref="dataSource"/>
                <!--加载sql映射文件-->
                <property name="mapperLocations" value="classpath:UserMapper.xml"/>
        </bean>
        <!--创建UserMapperImpl对象,注入SqlSessionFactory-->
        <bean id="userMapper1" class="com.pang.dao.impl.UserMapperImpl">
            <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        </bean>
        
        
        <!--配置Mapper接口-->
        <bean id="userMapper2" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!--关联mapper接口-->
            <property name="mapperInterface" value="com.pang.dao.UserMapper"/>
            <!--关联sqlSessionFactory-->
            <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
                        
        </bean>
        
        <!--Mapper接口扫描-->
        <!--注意:如果使用mapper接口包扫描,那么每个mapper接口在spring容器中的id名称为类名:例如:UserMapper写为userMapper-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.pang.dao"/>
    
        </bean>
</beans>
复制代码

 

 web.xml

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
  <display-name>springmvc</display-name>
  
    <!-- 配置拦截路径 -->
    <!-- 同样道理,也对其他的后缀名进行了设置 -->
    <servlet-mapping>
     <servlet-name >default </servlet-name >         
        <url-pattern >*.js</url-pattern>      
     </servlet-mapping >
     <servlet-mapping >
         <servlet-name >default </servlet-name >             
         <url-pattern >*.css</url-pattern>        
    </servlet-mapping >
     <servlet-mapping >
         <servlet-name >default </servlet-name >             
         <url-pattern >*.htm</url-pattern>        
    </servlet-mapping >
     <servlet-mapping >
         <servlet-name >default </servlet-name >             
         <url-pattern >*.gif</url-pattern>        
    </servlet-mapping >
  
    <servlet>
      <servlet-name>DispatcherServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring-mvc.xml</param-value>
      </init-param>
  </servlet>
  
  <servlet-mapping>
      <servlet-name>DispatcherServlet</servlet-name>
      <url-pattern>*.action</url-pattern>
  </servlet-mapping>
  
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>
复制代码
posted @ 2019-05-14 10:35  Kn4vE  阅读(167)  评论(0编辑  收藏  举报