Spring的Hello World工程
通过Spring的Hello World工程研究以下几个点:
0、如何创建工程及引入依赖。
1、通过Spring的beans.xml实现依赖注入,动态创建实例。
2、了解Spring的工作原理。
具体实现步骤:
1、创建工程
创建工程有两种,命令行和IDE(Eclipse),如果通过命令行创建最后还是要引入Eclipse中去开发,所以下面的创建主要是通过Eclipse去实现。
提示:由于是基于Application的测试,所以这里选择quickstart。
2、配置POM,引入Spring的依赖包
打开pom.xml,加入如下配置:
<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>com.jsoft.test</groupId>
<artifactId>HelloSpring</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <name>HelloSpring</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 Core -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> <!-- Spring Context -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> </dependencies>
</project>
3、开始编写项目工程代码
整理项目结构如下:
具体代码实现:
HelloWorld.java(创建接口):
package com.jsoft.test.hellospring.helloworld; public interface HelloWorld {
public void sayHello();
}
SpringHelloWorld.java(接口实现):
package com.jsoft.test.hellospring.helloworld.impl; import com.jsoft.test.hellospring.helloworld.HelloWorld; public class SpringHelloWorld implements HelloWorld { @Override
public void sayHello() {
// TODO Auto-generated method stub
System.out.println("Spring say HelloWorld!");
} }
StrutsHelloWorld.java(接口实现):
package com.jsoft.test.hellospring.helloworld.impl; import com.jsoft.test.hellospring.helloworld.HelloWorld; public class StrutsHelloWorld implements HelloWorld { @Override
public void sayHello() {
// TODO Auto-generated method stub
System.out.println("Struts say HelloWorld!");
} }
提示:在Java开发中,通常将后台分成几层,常见的是三层mvc:model、view、controller,模型视图控制层三层,而impl通常处于controller层的service下,用来存放接口的实现类,impl的全称为implement,表示实现的意思。所以这里的包名叫做impl。
HelloWorldService.java(创建实例):
package com.jsoft.test.hellospring.helloworld; public class HelloWorldService {
private HelloWorld helloWorld; public HelloWorldService(){ } public void setHelloWorld(HelloWorld helloWorld){
this.helloWorld = helloWorld;
} public HelloWorld getHelloWorld(){
return this.helloWorld;
}
}
beans.xml:
<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="com.jsoft.test.hellospring.helloworld.impl.SpringHelloWorld"></bean>
<bean id="strutsHelloWorld" class="com.jsoft.test.hellospring.helloworld.impl.StrutsHelloWorld"></bean> <bean id="helloWorldService" class="com.jsoft.test.hellospring.helloworld.HelloWorldService">
<property name="HelloWorld" ref="strutsHelloWorld"/>
</bean> </beans>
注意:新建beans.xml时,是放置在src/main/resources目录下,如果没有这个目录需要自行新建。
App.java(程序入口):
package com.jsoft.test.hellospring; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.jsoft.test.hellospring.helloworld.HelloWorld;
import com.jsoft.test.hellospring.helloworld.HelloWorldService; /**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
//IoC获取beans的上下文
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//通过上下文获取beans创建的Service,此时已经注入了是创建哪个的接口实现
HelloWorldService helloWorldService = (HelloWorldService)context.getBean("helloWorldService");
//Service调用统一接口方法获取注入的接口实现
HelloWorld helloWorld = helloWorldService.getHelloWorld();
//输出接口实现的内容
helloWorld.sayHello();
}
}
4、运行工程
【项目右键】->【Run As】->【Java Application】
输出如下:
5、工作原理总结:
1、beans.xml是Spring的一切,通过beans.xml的依赖注入去实例化类,已经传入指定的参数。
2、可以看出在App.java类中的main方法,先是获取beans实现上下文,然后通过上下文获取指定的Bean,而此时这个Bean已经在beans.xml中注入了一个StrutsHelloWorld的类实现。
3、在beans.xml中,通过<property name="HelloWorld" ref="strutsHelloWorld"/>属性去注入HelloWorldService.java中的setHelloWorld方法。经过测试,HelloWorld的开头字母大小写不区分。
测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test1/HelloSpring
以上参考:http://www.yiibai.com/spring/spring-tutorial-for-beginners.html
Spring的Hello World工程的更多相关文章
- mac os版本Intellij IDEA 搭建spring mvc的maven工程(新手教学)
由于近期换了新公司,又换mac pro作为新电脑,打算把用了很多年的eclipse换成IDEA(IDEA比eclipse的好处我就不多说了),由于mac os和IDEA刚开始用不久,所以专门用一篇博客 ...
- MAC环境下idea:maven+Spring+Dubbo+Zookeeper简单工程搭建
: 一:安装软件:tomcatZookeeperDubbo+admin 二:工程: 总工程 API Pom.xml:不用引用任何东西 Provider Pom.xml:要denpend ...
- 将Spring源码转换为工程 + 导入Eclipse时缺失jar包
将源码转换为工程: 比如查看Spring事务部分的源码. 打开cmd窗口,切换到Spring-tx文件夹下,执行命令 “gradle cleanidea eclipse” . 缺失jar包: 第一步: ...
- Spring MVC——搭建HelloWeb工程
1.确保环境配置配置正确(Myeclipse(eclipse)+Tomcat) 2.新建web project 3.将Spring MVC所需的jar包粘贴到WebRoot/WEB-INF/lib下 ...
- JPA hibernate spring repository pgsql java 工程(二):sql文件导入数据,测试数据
使用jpa保存查询数据都很方便,除了在代码中加入数据外,可以使用sql进行导入.目前我只会一种方法,把数据集中在一个sql文件中. 而且数据在导入中常常具有先后关系,需要用串行的方式导入. 第一步:配 ...
- OSGI与Spring结合开发web工程
简介: 作为一个新的事实上的工业标准,OSGi 已经受到了广泛的关注, 其面向服务(接口)的基本思想和动态模块部署的能力, 是企业级应用长期以来一直追求的目标.Spring 是一个著名的 轻量级 J2 ...
- Spring中多个工程停多个资源文件ignoreUnresolvablePlaceholders配置
http://www.imooo.com/ruanjiangongcheng/software-architecture-design/667686.htm
- step6----->往工程中添加spring boot项目------->修改pom.xml使得我的project是基于spring boot的,而非直接基于spring framework
文章内容概述: spring项目组其实有多个projects,如spring IO platform用于管理external dependencies的版本,通过定义BOM(bill of mater ...
- Springboot(一):使用Intellij中的Spring Initializr来快速构建Spring Boot工程
使用Intellij中的Spring Initializr来快速构建Spring Boot工程 New---Project 可以看到图所示的创建功能窗口.其中Initial Service Url指向 ...
随机推荐
- class extension、class category、class-continuation category
class extension Objective-C 2.0增加了class extensions用于解决两个问题: 允许一个对象可以拥有一个私有的interface,且可由编译器验证. 支持一个公 ...
- 661. Image Smoother@python
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...
- centos7内核优化
#sysctl -p 参数: net.ipv6.conf.all.disable_ipv6 = 1net.ipv6.conf.default.disable_ipv6 = 1net.ipv4.icmp ...
- (24)zabbix触发器表达式详解
概述 触发器中的表达式使用很灵活,我们可以创建一个复杂的逻辑测试监控,触发器表达式形式如下: 1 {<server>:<key>.<function>(< ...
- C++实现简易单向链表
#include <iostream> #include <stdlib.h> #include <stdbool.h> using namespace std; ...
- PyQt5(2)、垃圾分类小程序(2)——初代窗口程序可执行文件
又是一天时间(又没做大作业).今天的心路历程:(1)前端后端怎么连接?(2)后端数据库插数据(3)完全没用上之前的字典反查法(4)突然发现面向对象编程其实很好用,甚至越用越上瘾(5)QLineEdit ...
- linux 搭建apache 服务器
1.查看apache服务器 /etc/init.d/httpd status 若没有,则使用yum -y install httpd 安装软件 2.设置开机启动 chkconfig httpd o ...
- CSS+DIV网页样式与布局:第二章:CSS的基本语法
第二章:CSS的基本语法 一 CSS选择器(所有的HTML语言中的标记都是通过不同的css选择器进行控制的).用户只需要 通过选择器对不同的HTML标签进行控制,并赋予各种样式声明,即可实现各种效果. ...
- 【LeetCode】Integer to Roman(整数转罗马数字)
这道题是LeetCode里的第12道题. 吐了,刚做完"罗马数字转整数",现在又做这个.这个没什么想法,只能想到使用if语句嵌套,或者使用哈希表.但哈希表我还不熟练啊.先拿if嵌套 ...
- shell的if-else的基本用法
if 语句通过关系运算符判断表达式的真假来决定执行哪个分支.Shell 有三种 if ... else 语句: if ... fi 语句: if ... else ... fi 语句: if ... ...