. 默认情况下,容器是禁止自动装配的。
. 如果要自动装配,必须设置autowire为以下三个值:
byName:查找id等于属性名称的bean,然后调用
set方法完成注入。
注:
有无参构造器。
有set方法。
如果找不到对应的bean,注入null。
byType:查找与属性类型一致的bean,然后调用
set方法完成注入。
注:
有无参构造器。
有set方法。
如果找不到对应的bean,注入null。
如果找到多个,会出错。
constructor:查找与属性类型一致的bean,然后调用
构造器完成注入。
注:
有对应的构造器。
如果找不到对应的bean,注入null。
. 自动装配尽量少用,如果要用,可以使用byName。
使用value属性来注入。
用来读取bean的属性值,语法上类似于el表达式。
案例演示:
工程案例目录结构
spring环境搭建必备jar:
pom.xml:
4.0.0
com.study
springcase-day02
0.0.1-SNAPSHOT
war
junit
junit
4.12
javax
javaee-api
6.0
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 cities;
private Set interest;
private Map 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 getCities() {
return cities;
}
public void setCities(List cities) {
this.cities = cities;
}
public Set getInterest() {
return interest;
}
public void setInterest(Set interest) {
this.interest = interest;
}
public Map getScores() {
return scores;
}
public void setScores(Map 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
app2.xml
北京
武汉
岳阳
岳阳
钓鱼
做饭
看电视
看电视
Tom
tiger
长沙
南昌
重庆
钓鱼
做饭
看电视
Tom
tiger
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.