zoukankan      html  css  js  c++  java
  • Spring(HelloWorld)

    一、Spring是什么

    Spring是一个开源框架。

    Spring为简化企业级应用开发而生的,使用Spring可以使简单的JavaBean实现以前只有EJB才能实现的功能。

    Spring是一个IOC(DI)和AOP容器框架。

    具体描述Spring:

    --轻量级:Spring是非侵入性的,基于Spring开发的应用中的对象可以不依赖于Spring的API。

    --依赖注入(DI---dependency injection、IOC)。

    --面向切面编程(AOP---aspect oriented programming)。

    --容器:Spring是一个容器,因为它包含并且管理应用对象的生命周期。

    --框架:Spring实现了使用简单的组件配置组合成一个复杂的应用,在Spring中可以使用XML和Java注解组合这些复杂对象。

    --一站式:在IOC和AOP的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(实际上Spring自身也提供了展现层的Spring MVC和持久层的 Spring JDBC)。

    Spring模块:

     1、搭建Spring开发环境

      首先需要下载Spring框架spring-framework-4.0.0.RELEASE-dist,4.0.0版官方地址:https://repo.spring.io/libs-release-local/org/springframework/spring/4.0.0.RELEASE/  。

      此外,还需要下载一个必要的组件commons-logging-1.1.1.jar,可以在http://commons.apache.org/下载,spring依赖的日志包。

    将其有用jar包导入工程中。

    建一个Java工程,把以下 jar 包加入到工程的classpath下:

     commons-logging-1.1.1.jar:spring依赖的日志包。

     2、示例

    JavaBean:

    package com.atguigu.spring.beans;
    
    /**
     * @Author 谢军帅
     * @Date2019/11/30 21:41
     * @Description
     */
    public class HelloWorld {
    
        private String name;
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void hello(){
            System.out.println("hello:"+name);
        }
    }

    原始的使用:

             //创建对象
            HelloWorld helloWorld = new HelloWorld();
            //赋值
            helloWorld.setName("xjs");
            //调用方法
            helloWorld.hello();

    使用Spring的依赖注入:

    1)、在类路径下建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"
           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.0.xsd">
    
        <!--配置bean-->
        <bean id="helloWorld" class="com.atguigu.spring.beans.HelloWorld">
            <property name="name" value="spring"/>
        </bean>
    </beans>

    2)、IOC容器使用:

            //1、创建Spring的IOC容器
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    
            //2、从IOC容器中获取Bean实例
            HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
    
            helloWorld.hello();

    是根据反射,调用类的无参构造函数,为其创建实例,还调用其set方法赋值。

  • 相关阅读:
    ansible-palybook剧本
    ansible服务的部署与使用
    keepalived实现nginx高可用
    Linux下通过uptime判断负载情况
    2个无线路由器怎么连接
    Delphi FastReport动态加载图片 (转载)
    SQLite 日期 & 时间
    SuperObject使用
    php int 与 datetime 转换
    sqlserver 2005/2008 导入超大sql文件
  • 原文地址:https://www.cnblogs.com/xjs1874704478/p/11963510.html
Copyright © 2011-2022 走看看