您的位置:首页 > 编程语言 > Java开发

Spring快速入门

2016-12-19 19:04 405 查看
什么是Spring
  Spring是分层的JavaSE/EE full-stack(一站式) 轻量级开源框架
    分层
      SUN提供的EE的三层结构:web层、业务层、数据访问层(持久层/集成层)
      Struts2是web层基于MVC设计模式框架
      Hibernate是持久层的一个ORM的框架
    一站式
      Spring框架有对三层的每层解决方案
        web层:Spring MVC
        持久层:JDBC Template
        业务层:Spring的Bean管理
Spring的核心
  IOC[b](Inverse of Control 控制[b]反转)[/b]:将对象的创建权,交由Spring完成[/b]
  AOP([b]Aspect Oriented Programming): 是面向对象的功能延伸.不是替换面向对象,是用来解决OO中一些问题.[/b]
Spring优点
  方便解耦,简化开发
    Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理
  AOP编程的支持
    Spring提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能
  声明式事务的支持
    只需要通过配置就可以完成对事务的管理,而无需手动编程
  方便程序的测试
    Spring对Junit4支持,可以通过注解方便的测试Spring程序
  方便集成各种优秀框架
    Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts、Hibernate、MyBatis、Quartz等)的直接支持
  降低JavaEE API的使用难度
    Spring 对JavaEE开发中非常难用的一些API(JDBC、JavaMail、远程调用等),都提供了封装,使这些API应用难度大大降低
Spring的入门的程序
  下载Spring的开发包
    spring-framework-3.2.0.RELEASE-dist.zip ---Spring开发包
      docs :spring框架api和规范
      libs :spring开发的jar包
      schema :XML的约束文档.
    spring-framework-3.0.2.RELEASE-dependencies.zip ---Spring开发中的依赖包
  创建web工程引入相应jar包
    spring-beans-3.2.0.RELEASE.jar
    spring-context-3.2.0.RELEASE.jar
    spring-core-3.2.0.RELEASE.jar
    spring-expression-3.2.0.RELEASE.jar
    开发的日志记录的包:
      com.springsource.org.apache.commons.logging-1.1.1.jar --- 用于整合其他的日志的包(类似Hibernate中slf4j)
      com.springsource.org.apache.log4j-1.2.15.jar
  创建Spring的配置文件
    在src下创建一个applicationContext.xml
    引入XML的约束:[b]找到xsd-config.html.引入beans约束[/b]

<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">


  编写实现类

public class HelloServiceImpl implements HelloService {
  private String info;
  public void setInfo(String info) {
    this.info = info;
  }
  public void sayHello() {
    System.out.println("Hello Spring..."+info);
  }
}


  在配置中配置类

<!-- 通过一个<bean>标签设置类的信息,通过id属性为类起个标识. -->
<bean id="userService" class="cn.yzu.spring3.demo1.HelloServiceImpl">
  <!-- 使用<property>标签注入属性 -->
  <property name="info" value="你好"/>
</bean>


  创建测试类

@Test
// Spring开发
public void demo2() {
  // 创建一个工厂类.
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  HelloService helloService = (HelloService) applicationContext.getBean("userService");
  helloService.sayHello();
}


IOC和DI(*****)区别?
  IOC:控制反转:将对象的创建权,由Spring管理.
  DI:依赖注入:在Spring创建对象的过程中,把对象依赖的属性注入到类中.
Spring框架加载配置文件
  ApplicationContext 应用上下文,加载Spring 框架配置文件
    加载classpath:[b]new ClassPathXmlApplicationContext("applicationContext.xml"); :加载classpath下面配置文件[/b]
    加载磁盘路径:[b]new FileSystemXmlApplicationContext("applicationContext.xml"); :加载磁盘下配置文件[/b]
BeanFactory与ApplicationContext区别?
  ApplicationContext类继承了BeanFactory
  BeanFactory在使用到这个类的时候,getBean()方法的时候才会加载这个类,[b]ApplicationContext类加载配置文件的时候,创建所有的类[/b]
  ApplicationContext对BeanFactory提供了扩展:[b]国际化处理,[b]事件传递,[b]Bean自动装配,[b]各种不同应用层的Context实现[/b][/b][/b][/b]
  早期开发使用BeanFactory,用例:

@Test
public void demo4(){
  // ClassPathResource FileSystemResource
  BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("applicationContext.xml"));
  HelloService helloService = (HelloService) beanFactory.getBean("userService");
  helloService.sayHello();
}


MyEclipse配置XML提示
  Window--->xml catalog--->add 找到schema的位置 ,将复制的路径 copy指定位置,选择schema location.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: