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. 挂在光盘出现写保护mount: block device /dev/sr0 is write-protected, mounting read-only

    https://blog.csdn.net/yueludanfeng/article/details/60339688

  2. Web前端开发:Sublime Text 常用插件

    在安装这些插件之前,确保你已经安装了Package Control.   安装Package Control方法:   通过菜单栏View->Show Console 或者快捷键Ctrl+` 打 ...

  3. sqlserver中的数据转换与子查询

    数据类型转换 --cast转换 select CAST(1.23 as int) select CAST(1.2345 as decimal(18,2)) select CAST(123 as var ...

  4. ArcGIS Engine 10.x许可代码

    相比9.3,10.x许可代码的书写改变了,ArcObjects SDK 10 Microsoft .NET Framework 帮助文档中,提供了以下两种方式: 1. Calling RuntimeM ...

  5. weblogic弱密码检测

    http://www.secbox.cn/hacker/tools/6252.html http://60.12.168.73:8088/console/login/LoginForm.jsp htt ...

  6. 关于mac远程链接window服务器以及实现共享文件

    要最近换了新电脑mac 虽然运行速度666  但是真的很多地方都使用不习惯 这里记录一下 关于 远程链接window主机的问题 方便以后用 首先是 链接: 在应用里 找到    然后类似于  wind ...

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

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

  8. 51Nod1766 树上的最远点对 ST表 LCA 线段树

    原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1766.html 题目传送门 - 51Nod1766 题意 n个点被n-1条边连接成了一颗树,给出a~ ...

  9. day28 面向对象:反射,内置函数,类的内置方法

    面向对象进阶博客地址链接: http://www.cnblogs.com/Eva-J/articles/7351812.html 复习昨日内容: # 包 # 开发规范 # # hashlib # 登录 ...

  10. Linux下的Mysql数据库备份+还原

    数据库备份: root@debian-mm:/home/debian-mm# mysqldump -u root -p Account > Account.sql Enter password: ...