Spring界的HelloWorld

本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!

本文GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了6个月总结的一线大厂Java面试总结,本人已拿大厂offer,欢迎star

原文链接:blog.ouyangsihai.cn >> Spring界的HelloWorld

点击蓝字“程序员考拉”欢迎关注!

Spring界的HelloWorld

1.Spring依赖的jar包下载网址:


https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-local/org/springframework/spring/4.0.0.RELEASE/spring-framework-4.0.0.RELEASE-dist.zip

下载之后得到:

Spring界的HelloWorld

解压在libs目录中找到如下的四个jar包:

Spring界的HelloWorld

除此之外还需要一个commons-logging.jar包。

2.例如要用spring实现如下的普通的java代码实现的功能:

HelloWorld.java


package com.java.spring;
public class HelloWorld {
  private String name;
  public void setName(String name){
    this.name=name;
  }
  public void hello(){
    System.out.println("hello:"+name);
  }
}

Main.java


package com.java.spring;
public class Main {
  public static void main(String[] args){   
    HelloWorld helloWorld=new HelloWorld();
    helloWorld.setName("koala");
    helloWorld.hello(); 
  }
}

2.1 新建一个普通的java工程,工程目录下新建lib目录,拷贝所需的jar包:

Spring界的HelloWorld

之后build path:

Spring界的HelloWorld

2.2 在src目录下创建spring bean的配置文件:applicationContext.xml。选中项目,右击–Configure Facets–Install Spring Facet。

Spring界的HelloWorld

3.示例代码

HelloWorld.java


package com.java.spring;
public class HelloWorld {  
  private String name;
  
  public void setName(String name){
    this.name=name;
  }  
  public void hello(){
    System.out.println("hello:"+name);
  }
}

 Main.java


package com.java.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
  public static void main(String[] args){
    //1.创建spring的IOC对象
    ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
    //2.从IOC容器中获取bean实例
    HelloWorld helloWorld=(HelloWorld) ctx.getBean("helloworld");
    helloWorld.hello();
  }
}

配置文件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:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"

bean id="helloworld" class="com.java.spring.HelloWorld"
  property name="name" value="koala"/property
/bean

/beans

①class的值为Bean所在的全类名;

②创建spring的IOC对象,ApplicationContext ctx=new ClassPathXmlApplicationContext(“applicationContext.xml”);参数为spring bean的配置文件的名称;

③从IOC容器中获取bean实例,HelloWorld helloWorld=(HelloWorld) ctx.getBean(“helloworld”); 配置文件中bean id=”helloworld”的值与代码中getBean(String str);方法中的str一致;

④property name=”name” value=”koala”/property 是实现public void setName(String name){ this.name=name; },将koala赋值给name属性;

 

Spring界的HelloWorld
本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!

本文GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了6个月总结的一线大厂Java面试总结,本人已拿大厂offer,欢迎star

原文链接:blog.ouyangsihai.cn >> Spring界的HelloWorld


  转载请注明: 好好学java Spring界的HelloWorld

 上一篇
深入源码分析SpringMVC底层原理(二) 深入源码分析SpringMVC底层原理(二)
深入分析SpringMVC请求处理过程文章目录 - - - - - - -   在上一篇文章中我们讲到了SpringMVC的初始化,分别初始化两个ApplicationContext,并且初始化一些处理器,然后将ur
2021-04-05
下一篇 
Bean的自动装配及作用域 Bean的自动装配及作用域
点击蓝字“程序员考拉”欢迎关注! 1.XML配置里的Bean自动装配 Spring IOC 容器可以自动装配 Bean,需要做的仅仅是在 bean 的 autowire 属性里指定自动装配的模式。自动装配方式有: byType(
2021-04-05