zoukankan      html  css  js  c++  java
  • Spring第一个helloWorld

    Spring

    简介:

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

    依赖注入(DI—dependdency injection、IOC)

    面向切面编程:(AOP—aspect oriented programming)

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

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

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

    Spring 设计理念

          Spring是面向Bean的编程

    Spring 两大核心技术

         控制反转(IoC:)将组件对象的控制权从代码本身转移到外部容器

                组件化对象的思想:分离关注点,使用接口,不再关注实现

    目的:解耦合.实现每个组件时只关注组件内部的事情

    编写第一个HelloWorld程序

    安装Spring tool suite

         Spring tool suite是一个Eclipse插件,利用该插件可以更方便的在Eclipse平台上开发基于Spring的应用

    安装后将Eclipse进行重启

    搭建Spring开发环境

    把以下jar包加入到工程的classpath下:

    Spring的配置文件:一个典型的Spring项目需要创建一个或多个Bean的配置文件,这些配置文件用于在Spring IOC容器里配置Bean.Bean的配置文件可以放在classpath下,也可以放在其它目录下。

    现在可以写我们的HelloWorld,结构如下:

    applicationContext.xml是Spring的配置文件

    file-->new-->other

    创建以后

    效果如下:

    会为我们生成Spring配置文件的头信息,这是我们之前安装Spring tool suite插件为我们做的

    编写javaBean

    我们编写的javaBean代码为

    package cn.bdqn.spring;

    public class HelloSpring {

           //定义who属性,该属性的值通过Spring框架进行设置

           private String who=null;

           /**

            * 打印方法

            */

           public  void print(){

                  System.out.println("hello,"+this.getWho()+"!");

           }

           /**

            * 获得who

            * @return who

            */

           public String getWho(){

                  return who;

           }

           /**

            * 设置who

            * @param who

            */

           public void setWho(String who){

                  this.who=who;}    

    }

    编写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 

           class属性值是我们javaBean的全类名

       id是唯一标识

       property标签中的name属性的值是我们javaBean中的属性

       value  给name属性值对应的javaBean中的属性 设置的值

           -->

           <bean id="HelloSpring" class="cn.bdqn.spring.HelloSpring">

                  <property name="who"  value="spring"></property>

           </bean>

    </beans>

    编写测试类

    编写测试类,代码如下:

    package cn.bdqn.test;

    import org.springframework.context.support.ClassPathXmlApplicationContext;

    import cn.bdqn.spring.HelloSpring;

    public class Test {

           public static void main(String[] args) {

    //           创建Spring 的IOC容器对象

                  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

    /*          从IOC容器中获取Bean实例 "HelloSpring"是我们applicationContext.xml中配置的id属性的值*/

                  HelloSpring bean = (HelloSpring)context.getBean("HelloSpring");

    //           调用print方法

                  bean.print();   

           }

    }

  • 相关阅读:
    你好,2021!
    庚子走,辛丑来,愿一切安好!
    花魂鸟魂总难留,鸟自无言花自羞
    熟悉的小胡同
    夜半听鼾声
    写在儿子22岁生日
    vue配置Azure Artifacts npm包源
    RabbitMQ出错:AMQP close-reason, initiated by Peer, code=530的解决办法
    .NET MVC存储图片到数据库的解决方案
    EF Core解决报错’EntityTypeBuilder未包含“ToTable”的定义‘的方法
  • 原文地址:https://www.cnblogs.com/zqr99/p/7616243.html
Copyright © 2011-2022 走看看