Java學習筆記(十一)-Spring(四)

**

Spring的一次實例

1.新建一個空的項目命名test
2.導入必要的jar包
3.在pakge下新建一個Source類

package pojo;

public class Source {
	  private String fruit;   // 類型
	    private String sugar;   // 糖分描述
	    private String size;    // 大小杯
	    public String getFruit(){	 
	   	 return fruit;
	    }
	    public String getSugar(){	 
	   	 return sugar;
	    }
	    public String getSize(){	 
	   	 return size;
	    }
	    
	    public void setFruit(String fruit){	 
		   	 this.fruit=fruit;
		    }
		    public void setSugar(String sugar){	 
		   	 this.sugar=sugar;
		    }
		    public void setSize(String size){	 
		   	 this.size=size;
		    }
}

4.在src下目錄下新建一個applicationContext.xml文件通過 xml 文件配置的方式裝配我們的 bean

<?xml version="1.0" encoding="UTF-8"?>
<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 name="source" class="pojo.Source">
        <property name="fruit" value="橙子"/>
        <property name="sugar" value="多糖"/>
        <property name="size" value="超大杯"/>
    </bean>
    
</beans>

5.在packge下新建一個testspring

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import pojo.Source;
import pojo.juiceMaker;

public class testspring {
	@SuppressWarnings("resource")
	public static void  main(String args[]){
		
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
		Source source=(Source)context.getBean("source");
		System.out.println(source.getFruit());
        System.out.println(source.getSugar());
        System.out.println(source.getSize());
        
	}

}

5.運行結果如下
在這裏插入圖片描述
**
**

DI依賴注入實例

指 Spring 創建對象的過程中,將對象依賴屬性(簡單值,集合,對象)通過配置設值給該對象
繼續上述代碼
1.在packge下新建一個juiceMaker類

package pojo;

public class juiceMaker {
	private Source source=null;
	public Source getSource(){
		return source;
	}
	//set必須小寫
	public void setSource(Source source){
		
		this.source=source;
	}
	public String makeJuice(){
		String juice="sp點了一杯"+source.getFruit()+source.getSugar()+source.getSize();
		return juice;		
	}

}

2.在 xml 文件中配置 JuiceMaker 對象

<?xml version="1.0" encoding="UTF-8"?>
<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 name="source" class="pojo.Source">
        <property name="fruit" value="橙子"/>
        <property name="sugar" value="多糖"/>
        <property name="size" value="超大杯"/>
    </bean>
    
     <bean name="juiceMaker" class="pojo.juiceMaker">
        <property name="source" ref="source" />
    </bean>
</beans>

3.testspring的main改成

 public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[]{"applicationContext.xml"}
        );

        Source source = (Source) context.getBean("source");
        System.out.println(source.getFruit());
        System.out.println(source.getSugar());
        System.out.println(source.getSize());

        JuiceMaker juiceMaker = (JuiceMaker) context.getBean("juickMaker");
        System.out.println(juiceMaker.makeJuice());
    }

結果如下
在這裏插入圖片描述

**

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章