天天看點

Spring 學習筆記(一)

eclipse 配置spring tool 

位址 https://spring.io/tools3/sts/all  下載下傳 

springsource-tool-suite-3.9.8.RELEASE-e4.10.0-updatesite.zip

eclipse Help -> Install-> work with 選擇add Archive... 選擇下一步安裝

配置完成之後

1.建立Java Project 建立lib 檔案夾 導入所需的spring 核心包

Spring 學習筆記(一)

導入完成之後選中 add build path

2.建立 Java 檔案 HelloWorld

package xyz.yangjian.spring.demo;

public class HelloWorld {

	private String name;

	public HelloWorld() {
		System.out.println("我是無參構造器!");
	}

	public void setName(String name) {
		System.out.println("hello"+name);
		this.name = name;
	}
	
	public void hello() {
		System.out.println("Hello:"+name);
	}
	
}
           

3.建立 spring xml 檔案 applicationcontext.xml

<?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 id="helloworld" class="xyz.yangjian.spring.demo.HelloWorld">
		<property name="name" value="spring"></property>
	</bean>
</beans>
           

4.建立main 方法進行測試

package xyz.yangjian.spring.demo;

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

public class TestHelloWorld {
	public static void main(String[] args) {
		
		//1.建立Spring IOC容器
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
		
		//2.從執行個體中擷取bean
		HelloWorld helloworld = (HelloWorld) ac.getBean("helloworld");
		
		//3.調用hello 方法
		helloworld.hello();
		
	}
}
           

5.概述 spring通過IOC實作了控制反轉 将類交給spring 建立和管理 main 方法中無需再對類進行實力花 spring bean 的property配置了類的屬性

二 上述描述了類的屬性配置參數 下面通過構造器進行參數配置

1首先建立一個Car 類

package xyz.yangjian.spring.demo;

public class Car {
	private String name;
	private String brand;
	private int size;
	private double price;
	
	public Car(String name, String brand, int size) {
		this.name = name;
		this.brand = brand;
		this.size = size;
	}

	public Car(String name, String brand, double price) {
		this.name = name;
		this.brand = brand;
		this.price = price;
	}

	@Override
	public String toString() {
		return "Car [name=" + name + ", brand=" + brand + ", size=" + size + ", price=" + price + "]";
	}
	
}
           

2.然後配置spring 兩種構造器注入方式 第一種index指定位置 第二種 type類型指定順序

<?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 id="helloworld" class="xyz.yangjian.spring.demo.HelloWorld">
		<property name="name" value="spring"></property>
	</bean>
	
	<!-- 通過構造器方式 第一種方式 index 指定順序 -->
	<bean id="car1" class="xyz.yangjian.spring.demo.Car">
		<constructor-arg value="小車" index ="0"/>
		<constructor-arg value="奔馳" index ="1"/>
		<constructor-arg value="5" index ="2"/>
	</bean>
	
	<!-- 通過構造器方式 第二種方式 type 指定順序 -->
	<bean id="car2" class="xyz.yangjian.spring.demo.Car">
		<constructor-arg value="大車" type="java.lang.String"/>
		<constructor-arg value="普拉多" type ="java.lang.String"/>
		<constructor-arg value="2000" type ="double"/>
	</bean>
</beans>
           

3.建立main 方法進行測試

package xyz.yangjian.spring.demo;

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

public class TestHelloWorld {
	public static void main(String[] args) {
		
		//1.建立Spring IOC容器
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
		
		//2.從執行個體中擷取bean
		HelloWorld helloworld = (HelloWorld) ac.getBean("helloworld");
		
		//3.調用hello 方法
		helloworld.hello();
		
		
		Car car1 = (Car) ac.getBean("car1");
		System.out.println(car1.toString());
		
		Car car2 = (Car) ac.getBean("car2");
		System.out.println(car2.toString());
	}
}
           

4 測試結果

Car [name=小車, brand=奔馳, size=5, price=0.0]

Car [name=大車, brand=普拉多, size=0, price=2000.0]

根據結果可以看出 構造器注入的方式

5 綜上所述 Spring 的屬性注入 和 構造器注入就已經完成

另外注意一點 從執行個體中擷取bean 的方式可以是 getBean("id") 還可以 通過 getBean(HelloWorld.class) 擷取

第二點需要注意的是 spring配置檔案中的 class隻能有一個 class="xyz.yangjian.spring.demo.HelloWorld"

未完待更~