美文网首页
从0开始的Spring(01)-HelloWorld

从0开始的Spring(01)-HelloWorld

作者: 住阳台的猫 | 来源:发表于2019-07-13 14:39 被阅读0次

    1 Spring是什么?

    • Spring是一个开源框架
    • Spring为简化企业级应用开发而生,使用Spring可以使用简单的JavaBean实现以前只有EJB才能实现的功能
    • Spring是一个IOC(DI)和AOP容器框架

    1.1 具体描述Spring

    • 轻量级 Spring是非侵入性的,基于Spring开发的应用中的对象可以不依赖于Spring的API
    • 依赖注入 (DI---Dependency injection、IOC)
    • 面向切片编程(AOP---Aspect Oriented Programming)
    • 容器 Spring是一个容器,因为它包含并且管理应用对象的生命周期
    • 框架 Spring实现了使用简单的组件配置组合成一个复杂的应用,在Spring中可以使用XML和Java注解组合这些对象
    • 一站式 在IOC和AOP的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(实际上Spring自身也提供了展现层的SpringMVC和持久层的Spring JDBC)

    2 Spring模块

    Spring模块

    2.1 核心容器(Core Container)

    • spring-core:核心类库,其他模块大量使用此jar包;
    • spring-beans:Spring定义Bean的支持;
    • spring-context:运行时Spring容器;
    • spring-context-support:Spring容器对第三方包的集成支持,比如邮件服务、视图解析
    • spring-expression:Spring表达式语言

    2.2 AOP

    • spring-aop:基于代理的AOP支持;
    • spring-aspects:基于AspectJ的AOP支持;
    • spring-instrument:提供一些类级的工具支持和ClassLoader级的实现,用于服务器;
    • spring-instrument-tomcat:针对tomcat的instrument实现;

    2.3 Data Access/Integration

    • spring-jdbc:提供以jdbc访问数据库的支持;
    • spring-tx:提供编程式和声明式事务支持;
    • spring-orm:提供对象/关系映射支持;
    • spring-oxm:提供对象/xml映射支持;
    • spring-jms:提供对JMS(java消息服务)的支持;

    2.4 Web

    • spring-web:提供基础的web集成功能;
    • spring-webmvc:基于servlet的MVC;
    • spring-webmvc-portlet:基于portlet的mvc实现;
    • spring-websocket:提供websocket功能;

    2.5 Test&Messaging

    • spring-test:spring测试,提供junit与mock测试功能;
    • spring-messaging:对消息架构和协议的支持;

    3 简单的HelloWorld实现

    这里使用的环境是Intellij IDEA,相比较Eclipse可以省去环境的配置

    首先新建一个项目,这里因为是第一个项目,所以直接命名为Spring01了 建立完成后,我们可以在项目左边看到,IDEA已经为我们自动导入了可能会需要用到的jar包,很大的简化了我们的流程 首先,我们来看一下在以前的Java项目中的HelloWorld,在项目中新建一个HelloWorld类

    它具有一个私有的String变量name,通过调用hello方法就可以打印出name的值

    public class HelloWorld {
    
        private String name;
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void hello(){
            System.out.println("Hello: " + name);
        }
    
    //1.创建HElloWorld对象
    HelloWorld helloWorld = new HelloWorld();
    //为name属性赋值
    2.HelloWorld.setName("trainee");
    //3.调用hello方法
    helloWorld.hello();
    
    但这并没有使用到Spring的功能,要想使用Spring,我们需要先创建一个配置文件

    配置文件中已经存在部分写好的内容,我们只需要在其中添加我们所需要的部分,它的作用会在后面讲到

    <?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">
    
        <!--  配置bean  -->
        <bean id="helloWorld" class="com.trainee.HelloWorld">
            <property name="name" value="Spring"></property>
        </bean>
    
    </beans>
    

    配置文件写好后,我们的main函数也需要进行更新

    //1.创建Spring的IOC容器
    ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
    //2.从IOC容器中获取Bean实例
    HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
    //3.调用hello方法
    helloWorld.hello();
    
    点击运行,结果如图所示,正确输出了Hello: Spring

    这样我们的第一个Spring项目就运行起来了。

    相关文章

      网友评论

          本文标题:从0开始的Spring(01)-HelloWorld

          本文链接:https://www.haomeiwen.com/subject/rxgpkctx.html