Spring-context 实现Hello World

本文作为Spring入门笔记,用Spring-context实现控制台的hello world

Spring简介

Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。

项目结构

➜  main tree
.
├── java
│   └── hello
│   ├── impl
│   │   ├── SpringHelloWorld.java
│   │   └── StrutsHelloWorld.java
│   └── myspring
│   ├── HelloProgram.java
│   └── helloworld
│   ├── HelloWorld.java
│   └── HelloWorldService.java
└── resources
└── beans.xml 6 directories, 6 files

POM文件

可以直接参考Spring官网

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>fanghao</groupId>
<artifactId>myspring</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <name>myspring</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!--Spring Context-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency> </dependencies>
</project>

一、定义接口

package hello.myspring.helloworld;

public interface HelloWorld {
public void sayHello();
}

二、定义JavaBean

package hello.myspring.helloworld;

public class HelloWorldService {
private HelloWorld helloWorld; public HelloWorldService() {
}
public void setHelloWorld(HelloWorld helloWorld){
this.helloWorld = helloWorld;
} public HelloWorld getHelloWorld() {
return helloWorld;
}
}

三、定义实现类

package hello.impl;

import hello.myspring.helloworld.HelloWorld;

public class SpringHelloWorld implements HelloWorld {
public void sayHello() {
System.out.println("Spring Say Hello!!!");
}
}
package hello.impl;

import hello.myspring.helloworld.HelloWorld;

public class StrutsHelloWorld implements HelloWorld {
public void sayHello() {
System.out.println("Struts Say Hello!!");
}
}

四、定义Java Bean配置文件

beans.xml会搜索项目中的类,其中

<property name="helloWorld" ref="strutsHelloWorld"/>会调用引用类strutsHelloWorld的get和set方法,要保持命名一致(与属性名相同)

<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 id="springHelloWorld"
class="hello.impl.SpringHelloWorld"/>
<bean id="strutsHelloWorld"
class="hello.impl.StrutsHelloWorld"/> <bean id="helloWorldService"
class="hello.myspring.helloworld.HelloWorldService">
<property name="helloWorld" ref="strutsHelloWorld"/>
</bean> </beans>

五、定义应用文件

读取beans.xml 文件来创建一个应用程序上下文对象

package hello.myspring;

import hello.myspring.helloworld.HelloWorld;
import hello.myspring.helloworld.HelloWorldService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class HelloProgram {
public static void main(String[] args){
ApplicationContext context =
new ClassPathXmlApplicationContext("beans.xml");
HelloWorldService service =
(HelloWorldService) context.getBean("helloWorldService");
HelloWorld hw = service.getHelloWorld();
hw.sayHello();
}
}

六、运行结果

四月 08, 2018 10:49:52 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@46ee7fe8: startup date [Sun Apr 08 10:49:52 HKT 2018]; root of context hierarchy
四月 08, 2018 10:49:52 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
Struts Say Hello!!

Spring-context 实现Hello World的更多相关文章

  1. [转载]vs2012中使用Spring.NET报错:Spring.Context.Support.ContextRegistry 的类型初始值设定项引发异常

    学习使用Spring.NET中的时候,写了一个Demo,在运行时报了一个错误:Spring.Context.Support.ContextRegistry 的类型初始值设定项引发异常. 重新整理思绪, ...

  2. Spring context:component-scan中使用context:include-filter和context:exclude-filter

    Spring context:component-scan中使用context:include-filter和context:exclude-filter XML: <?xml version= ...

  3. Spring context:component-scan代替context:annotation-config

    Spring context:component-scan代替context:annotation-config XML: <?xml version="1.0" encod ...

  4. Spring <context:component-scan>标签属性 use-default-filters 以及子标签 include-filter使用说明

    Spring <context:component-scan>标签作用有很多,最基本就是 开启包扫描,可以使用@Component.@Service.@Component等注解: 今天要作 ...

  5. 使用web.xml方式加载Spring时,获取Spring context的两种方式

    使用web.xml方式加载Spring时,获取Spring context的两种方式: 1.servlet方式加载时: [web.xml] <servlet> <servlet-na ...

  6. 【报错】spring整合activeMQ,pom.xml文件缺架包,启动报错:Caused by: java.lang.ClassNotFoundException: org.apache.xbean.spring.context.v2.XBeanNamespaceHandler

    spring版本:4.3.13 ActiveMq版本:5.15 ======================================================== spring整合act ...

  7. Spring Context 你真的懂了吗

    今天介绍一下大家常见的一个单词 context 应该怎么去理解,正确的理解它有助于我们学习 spring 以及计算机系统中的其他知识. 1. context 是什么 我们经常在编程中见到 contex ...

  8. Spring context的refresh函数执行过程分析

    今天看了一下Spring Boot的run函数运行过程,发现它调用了Context中的refresh函数.所以先分析一下Spring context的refresh过程,然后再分析Spring boo ...

  9. spring context上下文(应用上下文webApplicationContext)(转载)

    (此文转载:http://www.cnblogs.com/brolanda/p/4265597.html) 一.先说ServletContext javaee标准规定了,servlet容器需要在应用项 ...

  10. spring <context:component-scan>使用说明(转)

    在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类 ...

随机推荐

  1. 【C++ Primer | 07】泛型算法

    定制操作 #include <iostream> #include <string> #include <vector> #include <algorith ...

  2. Leetcode刷题第20天

    一.找树左下角的值 题目:513. Find Bottom Left Tree Value C++ Soution 1: /** * Definition for a binary tree node ...

  3. exec函数族

    进程程序替换 进程程序替换原理 fork创建子进程执行的是和父进程相同的程序(也有可能是某个分支),通常fork出的子进程是为了完成父进程所分配的任务,所以子进程通常会调用一种exec函数(六种中的任 ...

  4. WPF 下两种图片合成或加水印的方式(转载)

    来源:http://www.cnblogs.com/lxblog/ 最近项目中应用多次应用了图片合成,为了今后方便特此记下. 在WPF下有两种图片合成的方式,一种还是用原来C#提供的GDI+方式,命名 ...

  5. 利用redis统计信息对CPU使用率进行收集

    http://dy.163.com/v2/article/detail/DQT2ROO10511RVML.html

  6. webpack学习笔记--按需加载

    为什么需要按需加载 随着互联网的发展,一个网页需要承载的功能越来越多. 对于采用单页应用作为前端架构的网站来说,会面临着一个网页需要加载的代码量很大的问题,因为许多功能都集中的做到了一个 HTML 里 ...

  7. WebApi参数传递实例

    Get 1.基础数据类型 1.1方法只含有一个形参 (1)Get传值的本质是通过url字符串拼接(2)Get传递参数本质是url字符串拼接,Request-Head头部传递,Request-Body中 ...

  8. vsftp为不同用户设置不同的ftp的根目录

    需求 要求ftp登录后的根目录是/var/test/,但是又不能影响其他用户的登录路径,因为有些程序是直接在根目录进行操作的,而没有目录切换的过程.操作过程新建用户 useradd test1user ...

  9. jenkins(8): 实战jenkins+gitlab持续集成发布php项目(代码不需要编译)

    一. jenkins 的配置 1.前提条件安装了GitLab Plugin (源码管理使用),GitLab Hook(gitlab webhook需要) Manage Jenkins--->Ma ...

  10. Codeforces Gym100543G Virus synthesis 字符串 回文自动机 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF-100543G.html 题目传送门 - CF-Gym100543G 题意 你可以对一个字符串进行以下两种操 ...