Spring示例工程
来源:互联网 发布:数据库系统实现 ppt 编辑:程序博客网 时间:2024/06/06 20:15
---------------siwuxie095
创建一个基于Spring IoC的小程序的步骤:
- 建立Spring工程
- 编写Java文件
- 编写配置文件
- 运行示例工程
示例:
一个人,在中国时用中文问候大家;在外国时,用英语问候大家
人的具体位置,由Spring的配置环境来决定的:
·当配置为中国时,则问候:"大家好"
·当配置为外国时,则问候:"Hello everybody"
工程名:TestHelloMessage
包名:com.siwuxie095.spring
类名:HelloMessage.java、HelloChina.java、HelloWorld.java、
Person.java、Main.java(主类)
打开资源管理器,在工程TestHelloMessage 文件夹下,创建一个
文件夹:lib,在其中放入三个必需的 jar 包:
(1)spring-core-4.3.7.RELEASE.jar
(2)spring-beans-4.3.7.RELEASE.jar
(3)commons-logging-1.2.jar
选择三个jar 包,右键->Build Path->Add to Build Path
工程结构目录如下:
HelloMessage.java:
package com.siwuxie095.spring;
//接口 HelloMessage,用于定义输出问候信息
public interface HelloMessage {
/**
*方法的简写,因为接口中只允许存在抽象方法和全局常量
*所以 public和 abstract可以省略掉或只省略 abstract
*/
public String sayHello();
}
HelloChina.java:
package com.siwuxie095.spring;
// HelloChina是接口的实现类,用于输出大家好
public class HelloChinaimplements HelloMessage {
@Override
public String sayHello() {
return"大家好";
}
}
HelloWorld.java:
package com.siwuxie095.spring;
// HelloWorld是接口的实现类,用于输出 Hello everybody
public class HelloWorldimplements HelloMessage {
@Override
public String sayHello() {
return"Hello everybody";
}
}
Person.java:
package com.siwuxie095.spring;
//Person类,用于调用 HelloMessage 接口,输出问候信息
public class Person {
//将HelloMessage作为一个属性,用于向大家输出问候信息
private HelloMessage helloMessage;
public HelloMessage getHelloMessage() {
return helloMessage;
}
publicvoid setHelloMessage(HelloMessage helloMessage) {
this.helloMessage = helloMessage;
}
/**
*用于调用HelloMessage接口向用户输出问候信息,
*具体的问候信息,由Spring的配置文件来分配和决定:
* 1.当配置文件中分配给person的是HelloChina的实体时,则输出"大家好"的信息
* 2.当配置文件中分配给person的是HelloWorld的实体时,则输出"Hello everybody"的信息
*/
public String sayHello() {
return this.helloMessage.sayHello();
}
}
Main.java(主类):
package com.siwuxie095.spring;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
public class Main {
/**
*代码段(1)和(2)等效
*因为 Spring 3.1之后已经废弃 XmlBeanFactory
*所以这里注释掉(2),使用(1)
*但(2)依然可用,导入相关包和类即可
*/
public staticvoid main(String[] args) {
//(1)
//使用 FileSystemResource 来读取配置文件
Resource resource=new FileSystemResource("helloMessage.xml");
DefaultListableBeanFactory factory=new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader=new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(resource);
//从IOC容器中获取Person类的实例
//Person person=(Person)b.getBean("Person");
Person person= (Person) factory.getBean("person");
//使用person类示例来输出问候信息
String str=person.sayHello();
System.out.println("The Person is currently saying:"+str);
// //(2)
// //使用 FileSystemResource() 读取配置文件 helloMessage.xml
// Resource resource=new FileSystemResource("helloMessage.xml");
// //使用 XmlBeanFactory() 加载配置文件,启动 IoC容器
// BeanFactory factory=new XmlBeanFactory(resource);
// //从 IoC 容器中获取 Person类的实例
// Person person=(Person) factory.getBean("person");
// String str=person.sayHello();
// System.out.println("The person is currently saying:"+str);
}
}
创建配置文件helloMessage.xml:
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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">
<!-- id和 class是 bean的属性,id是唯一标识,class是对应的类名(完全限定名) -->
<beanid="helloChina"class="com.siwuxie095.spring.HelloChina">
</bean>
<beanid="helloWorld"class="com.siwuxie095.spring.HelloWorld">
</bean>
<!--为 person添加属性,该属性引用了bean:helloWorld -->
<!--此时,person就和 helloWorld建立了依赖关系 -->
<!--也可以引用 helloChina,只需要修改 ref 属性即可 -->
<!--它们之间的依赖关系,根据配置文件决定,具体配置谁,控制权由原来的对象本身转移到配置文件中 -->
<beanid="person"class="com.siwuxie095.spring.Person">
<propertyname="helloMessage"ref="helloWorld"></property>
</bean>
</beans>
此时,工程结构目录一览:
运行一览:
Spring Bean 和 JavaBean 的区别:
参考链接1,参考链接2,参考链接3,参考链接4
【made by siwuxie095】
- spring+webservice工程示例
- Spring示例工程
- Spring环境搭建和示例工程
- Spring Quartz Java工程版和Web工程版示例
- Spring Quartz Java工程版和Web工程版示例
- Eclipse创建WEB工程Maven+Spring+SpringMVC+MyBatis示例
- Spring开发环境搭建以及创建示例工程
- Hibernate逆向工程示例
- EJB逆向工程示例
- live2d+cocos2dx示例工程
- Hibernate逆向工程示例
- Spring工程转成Spring Boot工程
- VS2005工程批处理编译示例
- LibGdx 如何导入示例工程
- spring3学习入门示例工程
- mfc嵌入cocos2dx示例工程
- Eclipse 导入git工程示例
- spring spring scheduled配置示例
- Python scikit-learn包 决策树和随机森林实例代码
- hibernate教程--持久化类状态详解
- 数列求和(void 函数)
- 利用ICESat角点数据通过最小二乘精化DEM
- 数据库设计的三大范式
- Spring示例工程
- zzuli 2131: Can Win(最大流 公平分配)@
- unity wav 格式音频转 写入本地
- StringBuilder、StringBuffer和String三者的联系和区别
- 你真的了解JAVA中的main方法吗?
- Appium+python实现App自动化登录
- uva 325 Identifying Legal Pascal Real Constants
- Python入门之循环分支
- 图像处理、计算机视觉、算法相关资源