天天看點

認識spring及環境搭建

一、spring是什麼?

      spring是一個開源架構,為簡化企業級應用而生,使用spring可以使簡單的javabean實作以前隻有ejb才能實作的功能

      spring是一個IOC(DI)和AOP容器架構

具體描述spring:

1.輕量級:spring是非侵入性,基于spring開發應用的對象可以不依賴springAPI

2.依賴注入:(dependency inject、IOC)

3.面向切面程式設計(APO)

4.容器:Spring是一個容器,因為它包含并管理應用對象的生命周期

5.架構:spring實作了使用簡單的元件配置組合成一個複雜的應用,在spring中可以使用xml和java注解組合這些對象

6.一站式:在IOC和AOP的基礎上可以整合各種企業應用的開源架構和優秀的第三方類庫(實際上spring自身也提供了展現層的springmvc和持久層的JDBC)

例子:一個對象,調用時建立對象,并為對象指派

package com.jff.springdemo;
public class HelloWorld {
	private String name;

	public HelloWorld() {
	}
	public void setName(String name) {
		this.name = name;
	}
	public void hello() {
		System.out.println("hello" + name);
	}
	public static void main(String[] args) {
		//建立一個
		HelloWorld hw=new HelloWorld();
        //為name屬性指派
		hw.setName("feifei");
		//調用hello方法
		hw.hello();
	}
}
           

二、環境搭建

maven工程下pom.xml引入jar包

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>5.2.1.RELEASE</version>
</dependency>
           

spring必須要用的4個包

認識spring及環境搭建

我們要對上面的例子進行優化,使用spring建立對象并為屬性指派

首先,建立一個資源檔案路徑,存放spring的xml檔案,這裡注意選擇的是資源檔案包,起名為“src/main/resources”

認識spring及環境搭建
認識spring及環境搭建

其次,要建立一個springxml,選擇file檔案,起名為“applicationContext.xml”

認識spring及環境搭建
<?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">
<!-- 填寫beans -->	
</beans>
           

 下面我們就用spring改造上面的代碼,使用spring建立對象,并為屬性指派

package com.jff.springdemo;
public class Person {
	private String name;
	private int age;
	private Car car;
	public Person() {
		// TODO Auto-generated constructor stub
	}
	public Person(String name, int age, Car car) {
		super();
		this.name = name;
		this.age = age;
		this.car = car;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Car getCar() {
		return car;
	}
	public void setCar(Car car) {
		this.car = car;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";
	}
}
           

編寫springxml檔案

<?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">
    <!-- 配置beans,class通過反射的方式建立執行個體,要求必須類中有一個無參的構造器 -->
    <!-- 通過set方法進行注入 -->
	<bean id="helloWorld" class="com.jff.springdemo.HelloWorld">
		<property name="name" value="feifei"></property>
	</bean>
</beans>
           

編寫test類

@Test
//屬性注入的方法
public void shuxingTest(){
   //1.建立spring ioc的容器對象
    //ApplicationContext是一個接口代表ioc容器
    //ClassPathXmlApplicationContext接口的實作類,該實作類從類路徑下來加載配置檔案
     ApplicationContext atx=new ClassPathXmlApplicationContext("applicationContext.xml");
    //2.從ioc容器中擷取bean執行個體,使用id擷取,getBean必須和xml中的id保持一緻
    //利用id定位到容器中的bean非常明确的指定需要的内容
    HelloWorld hw=(HelloWorld) atx.getBean("helloWorld");
    //另種從ioc容器中擷取bean執行個體,使用class擷取;利用類型傳回ioc中的bean要求bean在ioc容器中是唯一的
    //缺點:就是xml中有兩個id都是helloworld類他就不知道具體傳回那個了
    HelloWorld hs=atx.getBean(HelloWorld.class);
    hw.hello();
}
           

步驟:建立spring ioc的容器對象——》從ioc容器中擷取bean執行個體,

說明:從ioc容器中擷取bean執行個體有兩種方式

1)第一種:使用id擷取,getBean()必須和xml中的id保持一緻,利用id定位到容器中的bean非常明确的指定需要的内容

HelloWorld hw=(HelloWorld) atx.getBean("helloWorld");

2)第二種:使用class擷取;利用類型傳回ioc中的bean要求bean在ioc容器中是唯一的缺點:就是xml中有兩個id都是helloworld類他就不知道具體傳回那個了

HelloWorld hs=atx.getBean(HelloWorld.class);

運作成功