天天看点

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"

未完待更~