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. 解决AS gradle下载同步卡慢的问题

    国内因为GFW的原因,导致同步谷歌等服务器的插件源非常非常慢,几乎是龟爬,还好有阿里云的镜像源,据说速度很快,还不快试试: 1.build.gradle里的buildscript和allproject ...

  2. Go语言之defer关键字

    类似于java中的finally, 在函数返回来执行, 它用来保证函数一定会作一些事情. package main import "fmt" func main() { defer ...

  3. Spring Boot JPA的Column Table 注解命名字段无效

    @Table(name = "OrderInfo") @Entity public class OrderInfo { @Id @GeneratedValue private Lo ...

  4. .Net core2.0+Mysql5.7部署到CentOS7.5完整实践经验

    本文为本人最近学习将.Net Core部署到Linux的一些经验总结,也提供点也和我一样对Linux接触不多的.Net Core开发者. 一.部署用到的环境和工具 1.Linux采用最新的CentOS ...

  5. C# 之 数字格式化

    格式规范的完整形式:{index [,width][:formatstring]} index是此格式程序引用的格式字符串之后的参数,从零开始计数:width(可选) 是要设置格式的字段的宽度,wid ...

  6. nignx部署Vue单页面刷新路由404问题解决

    官网说明: https://router.vuejs.org/zh/guide/essentials/history-mode.html#%E8%AD%A6%E5%91%8A 在linux下搭建ngi ...

  7. windows云服务器数据迁移

    刚入职,第一个任务是完成windows腾讯云到windows华为云上的MySQL数据库迁移.之前都是在CentOS上搞,感觉没啥难度,一口答应,没想到各种坑接踵而来.次数不再叙述坑都有哪些了,说说怎么 ...

  8. PHP中使用CURL实现GET和POST请求(转载)

    CURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.最爽的是,PHP也支持 CURL 库.使用PHP的CURL 库可以简单和有效地去抓网页.你只 ...

  9. 自定义PDO封装类

    <?php class Db { protected static $_instance = null; // 定义标识符(通过$_instance值得改变情况,来判定Model类是否被实例化) ...

  10. JAVA程序的基本结构

    java程序(类)其实是一个静态方法库,或者是定义了一个数据类型.所以会遵循7种语法:     1.原始数据类型:             ---整型:byte.short.int.long ---浮 ...