《Spring In Action》学习笔记之HelloWorld

本文介绍了一个使用Spring框架的经典Hello World示例,展示了如何通过Spring的BeanFactory进行依赖注入,并使用接口和实现类来构建应用程序。

Spring有很多优点:轻量级,非侵入性,面向切面,容器,框架

我们是用spring核心包之BeanFactory构造最经典的helloWorld

接口隔离开来

package HelloWorld;

public interface GreetingHelloworld {
   
public void sayHelloworld();
}




 实现类:

使用spring的IOC依赖注入机制,两种方式:设值注入(成员变量)和构造注入(构造函数参数)

 

package HelloWorld;

public class GreetingHelloworldImpl implements GreetingHelloworld {
   
private String greetingText;
   
private String greetingFromConstructor;
   
public GreetingHelloworldImpl(String greetingFromConstructor){
       
this.greetingFromConstructor=greetingFromConstructor;
   }

   
public GreetingHelloworldImpl(){
       
   }

    
public String getGreetingText() {
       
return greetingText;
   }

    
public void setGreetingText(String greetingText) {
    
this.greetingText = greetingText;
    }

    
public void sayHelloworld() {
       System.out.println(
this.getGreetingText());
       System.out.println(
this.getGreetingFromConstructor());
    }

    
public String getGreetingFromConstructor() {
        
return greetingFromConstructor;
    }

    
public void setGreetingFromConstructor(String greetingFromConstructor) {
        
this.greetingFromConstructor = greetingFromConstructor;
    }


}

 

spring配置文件:

 

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>
 
<bean id="greeting" class="HelloWorld.GreetingHelloworldImpl">
   
<constructor-arg>
      
<value>this is helloworld from construtor</value>
   
</constructor-arg>
   
<property name="greetingText">
     
<value>hello world!</value>
   
</property>
   
 
</bean>
</beans>

 

测试用例:

 

package HelloWorld;

import java.io.File;
import java.net.URISyntaxException;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;



public class TestHelloWorld {

    
public static void main(String[] args) {

        String filePath
=System.getProperty("user.dir")+File.separator+"HelloWorld"+File.separator+"hello.xml";
        
        BeanFactory factory
=new XmlBeanFactory(new FileSystemResource(filePath));
        GreetingHelloworldImpl greetingSerview
=(GreetingHelloworldImpl)factory.getBean("greeting");
        greetingSerview.sayHelloworld();

    }

    
//    public static String getRealPath(String filename){
//        String path="";
//        Class theClass=TestHelloWorld.class;   
//        java.net.URL  u= theClass.getResource(filename);   
//        return u.getPath();
//        
//        
//    }
    
    


}

 

运行结果:

hello world!
this is helloworld from construtor

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值