1. 程式人生 > >後端開發基礎-Spring框架學習-002——基礎概念

後端開發基礎-Spring框架學習-002——基礎概念

自動裝配(瞭解)

. 預設情況下,容器是禁止自動裝配的。 . 如果要自動裝配,必須設定autowire為以下三個值: byName:查詢id等於屬性名稱的bean,然後呼叫 set方法完成注入。 注: 有無參構造器。 有set方法。 如果找不到對應的bean,注入null。 byType:查詢與屬性型別一致的bean,然後呼叫 set方法完成注入。 注: 有無參構造器。 有set方法。 如果找不到對應的bean,注入null。 如果找到多個,會出錯。 constructor:查詢與屬性型別一致的bean,然後呼叫 構造器完成注入。 注: 有對應的構造器。 如果找不到對應的bean,注入null。 . 自動裝配儘量少用,如果要用,可以使用byName。

注入基本型別的值

使用value屬性來注入。

注入集合型別的值

將集合型別當做一個bean來配置

spring表示式

用來讀取bean的屬性值,語法上類似於el表示式。

案例演示:

工程案例目錄結構

spring環境搭建必備jar:

 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.study</groupId>
  <artifactId>springcase-day02</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <dependencies>
  
	  <dependency>
	  		<groupId>junit</groupId>
	  		<artifactId>junit</artifactId>
	  		<version>4.12</version>
	  	</dependency>
	  	<dependency>
	  		<groupId>javax</groupId>
	  		<artifactId>javaee-api</artifactId>
	  		<version>6.0</version>
	  	</dependency>
  
  </dependencies>
</project>

Restaurant.java

package ioc.auto;

public class Restaurant {
	private Waiter wt;

	public Restaurant() {
		System.out.println("Restaurant無參構造器...");
	}

	public Waiter getWt() {
		return wt;
	}

	public void setWt(Waiter wt) {
		System.out.println("Restaurant的set方法...");
		this.wt = wt;
	}

	@Override
	public String toString() {
		return "Restaurant [wt=" + wt + "]";
	}
	

}

Restaurant2.java

package ioc.auto;

public class Restaurant2 {
	private Waiter wt;

	public Restaurant2() {
		System.out.println("Restaurant2的無參構造器...");
	}

	public Restaurant2(Waiter wt) {
		System.out.println("Restaurant2的帶參構造器...");
		this.wt = wt;
	}

	@Override
	public String toString() {
		return "Restaurant2 [wt=" + wt + "]";
	}
	
}

Waiter.java

package ioc.auto;

public class Waiter {

	public Waiter(){
		System.out.println("Waiter的無參構造器...");
		
	}
	
	/*public static Waiter getInstance(){
		System.out.println("靜態工廠方式");
		return new Waiter();
	}*/
}

ExampleBean.java

package ioc.basic;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class ExampleBean {
	
	private String name;
	private int age;
	private List<String> cities;
	private Set<String> interest;
	private Map<String,Double> scores;
	private Properties db;
	

	public ExampleBean() {
		System.out.println("ExampleBean的無參構造器...");
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public List<String> getCities() {
		return cities;
	}
	public void setCities(List<String> cities) {
		this.cities = cities;
	}
	public Set<String> getInterest() {
		return interest;
	}
	public void setInterest(Set<String> interest) {
		this.interest = interest;
	}
	public Map<String, Double> getScores() {
		return scores;
	}
	public void setScores(Map<String, Double> scores) {
		this.scores = scores;
	}
	public Properties getDb() {
		return db;
	}
	public void setDb(Properties db) {
		this.db = db;
	}

	@Override
	public String toString() {
		return "ExampleBean [name=" + name + ", age=" + age + ", cities=" + cities + ", interest=" + interest
				+ ", scores=" + scores + ", db=" + db + "]";
	}
	
}

SomeBean.java

package ioc.basic;

public class SomeBean {
	private String name;
	private String city;
	private double score;
	private String pageSize;
	
	public SomeBean() {
		System.out.println("SomeBean的無參構造器...");
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public double getScore() {
		return score;
	}

	public void setScore(double score) {
		this.score = score;
	}

	public String getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	@Override
	public String toString() {
		return "SomeBean [name=" + name + ", city=" + city + ", score=" + score + ", pageSize=" + pageSize + "]";
	}
	
}

app2.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"
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
	 <bean id="eb1" class="ioc.basic.ExampleBean">
	 	<property name="name" value="黛玉"/>
	 	<property name="age" value="16"/>
	 	<property name="cities">
	 		<list>
	 			<value>北京</value>
	 			<value>武漢</value>
	 			<value>岳陽</value>
	 			<value>岳陽</value>
	 		</list>
	 	</property>
	 	<property name="interest">
	 		<set>
	 			<value>釣魚</value>
	 			<value>做飯</value>
	 			<value>看電視</value>
	 			<value>看電視</value>
	 		</set>
	 	</property>
	 	<property name="scores">
	 		<map>
	 			<entry key="english" value="59.5"/>
	 			<entry key="math" value="80"/>
	 		</map>
	 	</property>
	 	<property name="db">
	 		<props>
	 			<prop key="username">Tom</prop>
	 			<prop key="password">tiger</prop>
	 		</props>
	 	</property>
	 </bean>
	 
	<!-- 
		將集合型別當做一個bean來配置,這樣,
		集合型別的值就可以重用。
	 -->
	 
	<util:list id="citiesBean">
		<value>長沙</value>
		<value>南昌</value>
		<value>重慶</value>
	</util:list>
	<util:set id="interestBean">
		<value>釣魚</value>
	 	<value>做飯</value>
	 	<value>看電視</value>
	</util:set>
	<util:map id="scoresBean">
		<entry key="english" value="59.5"/>
	 	<entry key="math" value="80"/>
	</util:map>
	<util:properties id="dbBean" >
		<prop key="username">Tom</prop>
	 	<prop key="password">tiger</prop>
	</util:properties>
	<bean id="eb2" class="ioc.basic.ExampleBean">
		<property name="cities" ref="citiesBean"/>
		<property name="interest" 
		ref="interestBean"/>
		<property name="scores" ref="scoresBean"/>
		<property name="db" ref="dbBean"/>
	</bean>
	
	<!-- 
		讀取location屬性指定位置的檔案的內容,
		並將內容存放到Properties物件裡面。
	 -->
	<util:properties id="jdbc" 
	location="classpath:config.properties"/>
	
	<bean id="sb1" class="ioc.basic.SomeBean">
		<!-- 讀取id為eb1的bean的name屬性值 -->
		<property name="name" value="#{eb1.name}"/>
		<!-- 讀取id為eb1的bean的cities屬性值(
		該屬性是一個List,讀取下標為1的元素的值)。 -->
		<property name="city" 
		value="#{eb1.cities[1]}"/>
		<!-- 讀取id為eb1的bean的scores屬性值(
		該屬性是一個Map,讀取的key為english的值)  -->
		<!-- <property name="score" 
		value="#{eb1.scores.english}"/> -->
		<property name="score" value="#{eb1.scores.math}"/>
		<!-- 讀取id為jdbc的bean的pageSize屬性值(
		該屬性是一個Properties,讀取key為pageSize的值) -->
		<property name="pageSize" 
		value="#{jdbc.pageSize}"/>
	</bean> 
	
	
	
	
	
</beans>

config.properties

pageSize=10

TestCase.java

package test;

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

import ioc.auto.Restaurant2;
import ioc.basic.ExampleBean;
import ioc.basic.SomeBean;

public class TestCase {
	
	@Test
	//自動裝配
	public void test1(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("app.xml");
		Restaurant2 rest = ac.getBean("rest3",Restaurant2.class);
		System.out.println(rest);

	}
	
	@Test
	//測試基本型別值的注入,集合型別值的注入
	public void test2(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("app2.xml");
		ExampleBean eb= ac.getBean("eb2", ExampleBean.class);
		System.out.println(eb);
	}
	
	@Test
	//測試spring表示式
	public void test3(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("app2.xml");
		SomeBean sb1 = ac.getBean("sb1", SomeBean.class);
		System.out.println(sb1);
	}
}

依次執行test1,test2,test3,後臺執行結果:

A.

 B.

C.