后端开发基础-Spring框架学习-002——基础概念

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Coder_Boy_/article/details/82829286

自动装配(了解)

. 默认情况下,容器是禁止自动装配的。
. 如果要自动装配,必须设置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 + "]";
	}
	
}

app.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="wt" class="ioc.auto.Waiter"/>
	 <bean id="wt2" class="ioc.auto.Waiter"/>
	
	 <!-- <bean id="wt2" class="ioc.auto.Waiter"  factory-method="getInstance"/> -->
	
	<!-- 
		byName属性值:查找id等于属性名称的bean,然后调用
		set方法完成注入。
		注:
			有无参构造器。
		  	有set方法。
		  	如果找不到对应的bean,注入null。
	 -->
	
	<!-- <bean id="rest" 
	class="ioc.auto.Restaurant" 
	autowire="byName"/> -->
	
	<!-- 
		byType属性值: 查找与属性类型一致的bean,
		然后调用set方法完成注入。
		注:
			有无参构造器。
		  	有set方法。
		  	如果找不到对应的bean,注入null。
		  	如果找到多个,会出错。
	 -->
	
	<!-- <bean id="rest2" 
	class="ioc.auto.Restaurant"
	autowire="byType"/> -->
	
		
	<!-- 
		constructor属性值:查找与属性类型一致的bean,
		然后调用构造器完成注入。
		注:
			有对应的带参构造器。
			
	 -->
	<bean id="rest3" 
	class="ioc.auto.Restaurant2" 
	autowire="constructor"/>
	
	
	
	
	
	
	
</beans>

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.

猜你喜欢

转载自blog.csdn.net/Coder_Boy_/article/details/82829286