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. Exception in thread "main" java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Ljava/lang/String;I)Z

    1.window操作系统的eclipse运行wordcount程序出现如下所示的错误: Exception in thread "main" java.lang.Unsatisfi ...

  2. net-snmp 安装与trap调试

    https://sourceforge.net/projects/net-snmp/files/net-snmp/5.7.3/

  3. C++ 复制构造函数 与 赋值运算符

    在C++中,将一个对象赋给另外一个对象,编译器将提供赋值运算符的定义. 有两种情况,下面假设catman是Monster的一个实例 第一种:初始化 Monster golblen= catman; 第 ...

  4. Jmeter入门(压力测试)

    Jmeter介绍 Jmeter是一款使用Java开发的,开源免费的,测试工具, 主要用来做功能测试和性能测试(压力测试/负载测试). 而且用Jmeter 来测试 Restful API, 非常好用. ...

  5. nginx反向代理uwsgi django服务器搭建总结

    1.安装python.django.虚拟环境 参考帖子:安装python django https://blog.csdn.net/a249900679/article/details/5152720 ...

  6. Codeforces 912C Perun, Ult!

    Perun, Ult! 恶心题, 好多细节... 啊, 好恶心啊. #include<bits/stdc++.h> #define LL long long #define fi firs ...

  7. 获取Form表单数据转化成JSON对象

    $.fn.serializeObject = function() { var o = {}; var a = this.serializeArray(); $.each(a, function() ...

  8. BZOJ4319 cerc2008 Suffix reconstruction 字符串 SA

    原文链接http://www.cnblogs.com/zhouzhendong/p/9016336.html 题目传送门 - BZOJ4319 题意 给出一个$1,2,\cdots,n$的排列,第$i ...

  9. Linux安装Tomcat-Nginx-FastDFS-Redis-Solr-集群——【第一集之安装VMware】

    [Java全技术路线开发]:https://jq.qq.com/?_wv=1027&k=5knQcPc   由于自己已经安装了VMware,本想转载一篇百度经验的教程,但是我对安装过程有些异议 ...

  10. Hive的配置| 架构原理

    Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张表,并提供类SQL查询功能. 本质是:将HQL转化成MapReduce程序 1)Hive处理的数据存储在HDFS 2)Hi ...