使用Spring编写第一个程序-spring初级体验
来源:互联网 发布:类似p2psearcher的软件 编辑:程序博客网 时间:2024/04/30 11:34
对最原始的HelloWorld.java写法分析:
一个HelloWorld有两个职责,一个是获得外部参数,另一个决定如何显示
第一次改进:分离职能
//消息显示者接口
MessageDisplayer.java
package prodream.com
public interface MessageSupplice{
public void setMessageSupplice(Message supplier);
public void display();
}
//实现MessageDisplayer接口
ConcreteMessageDisplayer.java
package prodream.com
public class ConreteMessageDisplayer{
private MessageSupplier suppier;
public void setMessageSupplier(MessageSupplier supplier){
this.supplier=supplier;
}
public void display(){
if(supplier==nul)
throw new RuntimeException();
System.out.println(supplier.getMessage());
}
}
//消息提供者接口
MessageSupplier.java
package prodream.com;
public interface MessageSupplier{
String getMessage();
}
//实现MessageSupplier接口
HelloWorldMessageSupplier.java
public class HelloWorldMessageSupplier{
public String getMessage(){
return "Hello World";
}
}
//主程序
HelloWorld.java:
public class HelloWorld{
public static void main(String[] args){
//创建消息提供者
MessageSupplier msgSupplier=new HelloWorldMessageSupplier();
//创建消息显示者
MessageDisplayer msgDisplayer=new ConcreteMessageDisplayer();
//注入消息提供者
msgDisplayer.setMessageSupplier(msgSupplier);
//显示相应信息
msgDisplayer.display();
}
分析:如果希望替换MessageDisplayer或者MessageSupplier的实现类,那么需要修改主程序中手动更改,那么运用程序代码又将经历编译测试的风险。
解决方法是通过外部的属性文件来读取相应实例类的名字
//我觉得这样不能体现什么大的价值,正如有写对spring的注入式的分析中写的一样,这种依赖转向对配置文件的依赖
再次改进:引入单例工厂
MessageSupportFactoruy.java:
package prodream.com;
import java.io.FileInputStream;
import java.util.Properties;
public class MessageSupportFactory{
private static MessageSupportFactory instance=null;
private Properties props=null;
public synchronized static MessageSupportFactory getInstance(){
if(instance==null)
instance=new MessageSupportFactory();
return instance;
public MessageSupportFactory(){
props=new Properties();
try{
props.load(new FileInputStream("conf/msgbean.properties");
}
catch(Exception ex){
ex.printStackTrace();
}
}
public MessageDisplayer makeDisplayer(){
String displayClass=props.getProperty("displayer.class");
try{
return (MessageDisplayer) Class.forName(disaplayerClass).newInstance();
}
catch(ClassNotFoundException e){
System.out.println("Could't found class:" +displayClass);
}
catch(InstaniationException e){
System.out.println("Couldn't instaniate an object of type:" +displayerClass);
}
catch(IllegalAccessException e){
System.out.println("couldn't access class:"+displayerClass);
}
return null;
}
public MessageSupplier makeSupplier(){
String supplierClass=props.getProperty("supplier.class");
try{
return (MessageSupplierr) Class.forName(supplierClass).newInstance();
}
catch(ClassNotFoundException e){
System.out.println("Could't found class:" +supplierClass);
}
catch(InstaniationException e){
System.out.println("Couldn't instaniate an object of type:" +supplierClass);
}
catch(IllegalAccessException e){
System.out.println("couldn't access class:"+supplierClass);
}
return null;
}
}
vmsbean.properties的内容:
displayer.class=prodream.com.ConcreteMessageDiaplayer
supplier.class=prodream.com.HelloWorldSupplier
//主程序
HelloWorldWithFactory.java:
public class HelloWorldWithFactory{
public static void main(String[] args){
//创建消息提供者
MessageSupplier msgSupplier=MessageSupportFactory.getInstance().makeSupplier();
//创建消息显示者
MessageDisplayer msgDisplayer=MessageSupportFactory.getInstance().makeDisplayer();
//注入消息提供者
msgDisplayer.setMessageSupplier(msgSupplier);
//显示相应信息
msgDisplayer.display();
}
引入了工厂抽离了部分类实例化的硬编码(hard code,简单说就是一种代码不灵活的体现看他们往往可以通过外部设置,设计模式,重构等手段来解决)
。。
。
。
。
。
- 使用Spring编写第一个程序-spring初级体验
- 第一个Spring程序
- Spring第一个程序
- 第一个spring程序
- 第一个spring程序
- 第一个Spring程序
- Spring第一个程序
- Spring 第一个程序
- 第一个Spring程序
- 第一个Spring程序
- Spring入门之编写第一个Spring
- spring第一个程序笔记
- Spring的第一个程序
- spring 第一个程序 hello
- spring security:第一个程序
- Spring第一个 helloworld 程序
- 第一个 Spring Boot 程序
- spring 第一个helloWord程序
- 又买了两本《黑》,可惜没买到《防线》
- 实现记住我的帐号的功能
- 程序员职业规划
- 加密与解密
- IT人患上职业病的机率令人堪忧
- 使用Spring编写第一个程序-spring初级体验
- 无法播放flv视频文件(MIME设置问题)(转帖)
- ANN vs BNN
- java 实现的队列
- Java JDBC链接数据库实例
- 停止GridView,FormView的inserting事件
- document.getElementById技巧
- vector动态二维数组
- 设计模式总结