一、前言
上一章记录了Spring的概念、Spring容器及IOC相关的知识点。这一章将继续记录一些Spring相关知识点。
二、Spring基础
1. 注入基本数据类型的值
注入基本数据类型,使用property元素,name为属性名,value为需要注入的属性值。代码如下。
Val.java
package base;
public class Val {
private String name;
private int age;
private boolean flag;
private byte num;
private double sum;
public Val() {
System.out.println("val()");
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public void setNum(byte num) {
this.num = num;
}
public void setSum(double sum) {
this.sum = sum;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Val [name=" + name + ", age=" + age + ", flag=" + flag + ", num=" + num + ", sum=" + sum + "]";
}
}
val.xml
<bean id="v" class="base.Val">
<property name="name" value="张三"></property>
<property name="age" value="18"></property>
<property name="flag" value="true"></property>
<property name="num" value="12"></property>
<property name="sum" value="18.7776"></property>
</bean>
启动容器并测试:
package mytest;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import base.Val;
import ioc.B;
public class MyTest3 {
@Test
public void t1(){
ApplicationContext ac = new ClassPathXmlApplicationContext("val.xml");
Val val = ac.getBean("v",Val.class);
System.out.println(val);
}
}
测试结果:
val()
Val [name=张三, age=18, flag=true, num=12, sum=18.7776]
2. 注入集合类型的值
有四种集合类型,分别为List,Set,Map,Properties
Val2.java
package base;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Val2 {
private List city;
private Set interest;
private Map<String,Double> score;
private Properties db;
public Val2() {
super();
}
public void setCity(List city) {
this.city = city;
}
public void setInterest(Set interest) {
this.interest = interest;
}
public void setScore(Map<String, Double> score) {
this.score = score;
}
public void setDb(Properties db) {
this.db = db;
}
@Override
public String toString() {
return "Val2 [city=" + city + ", interest=" + interest + ", score=" + score + ", db=" + db + "]";
}
}
val.xml
<bean id="v2" class="base.Val2">
<property name="city">
<list>
<value>成都</value>
<value>上海</value>
<value>北京</value>
</list>
</property>
<property name="interest">
<set>
<value>唱歌</value>
<value>跳舞</value>
<value>弹琴</value>
</set>
</property>
<property name="score">
<map>
<entry key="语文" value="99"></entry>
<entry key="数学" value="87.5"></entry>
<entry key="外语" value="91"></entry>
</map>
</property>
<property name="db">
<props>
<prop key="useruame">zs</prop>
<prop key="password">123</prop>
</props>
</property>
</bean>
启动容器并测试:
@Test
public void t2(){
ApplicationContext ac = new ClassPathXmlApplicationContext("val.xml");
Val2 val2 = ac.getBean("v2",Val2.class);
System.out.println(val2);
}
结果:
val()
Val2 [city=[成都, 上海, 北京], interest=[唱歌, 跳舞, 弹琴], score={语文=99.0, 数学=87.5, 外语=91.0}, db={useruame=zs, password=123}]
3. 引用的方式注入集合类型
如果像上面的方式注入集合类型,当出现多个bean共用一个数据的时候,就会出现很多重复代码,如果我们能把这些数据封装成一个共用的bean,就不会出现重复的情况。代码如下。
<util:list id="cityBean">
<value>重庆</value>
<value>贵阳</value>
</util:list>
<util:set id="interestBean">
<value>编程</value>
<value>游泳</value>
</util:set>
<util:map id="scoreBean">
<entry key="政治" value="100"></entry>
<entry key="地理" value="90"></entry>
</util:map>
<util:properties id="dbBean">
<prop key="user">ls</prop>
<prop key="password">321</prop>
</util:properties>
<bean id="v3" class="base.Val2">
<property name="city" ref="cityBean"></property>
<property name="interest" ref="interestBean"></property>
<property name="score" ref="scoreBean"></property>
<property name="db" ref="dbBean"></property>
</bean>
测试结果:
Val2 [city=[重庆, 贵阳], interest=[编程, 游泳], score={政治=100.0, 地理=90.0}, db={user=ls, password=321}]
3.1 读取properties文件内容
此处使用了location属性来定位文件位置,需要注意的是必须要加classpath:
其他的三个参数都是沿用上面的,未作改变。
<util:properties id="dbBean2" location="classpath:db.properties"></util:properties>
<bean id="v3" class="base.Val2">
<property name="city" ref="cityBean"></property>
<property name="interest" ref="interestBean"></property>
<property name="score" ref="scoreBean"></property>
<property name="db" ref="dbBean2"></property>
</bean>
测试结果:
Val2 [city=[重庆, 贵阳], interest=[编程, 游泳], score={政治=100.0, 地理=90.0}, db={password=222, username=zdl}]
Spring表达式
Spring引入了一种表达式语言,和EL表达类似,该表达式可以读取一个bean对象/集合中的值。
name是获取一个id为v的bean的name属性的值。
city是获取id为cityBean的List集合的第一个值
interest是获取id为interestBean的Set集合的第二个值
score是获取id为scoreBean的Map集合中key为zhengzhi的值
score2是获取id为scoreBean的Map集合中key为地理的值(key为中文需要如此写)
username是获取id为dbBean2的properties的username的值
<bean id="v4" class="base.Val4">
<property name="name" value="#{v.name}"></property>
<property name="city" value="#{cityBean[0]}"></property>
<property name="interest" value="interestBean[1]"></property>
<property name="score" value="#{scoreBean.zhengzhi}"></property>
<property name="score2" value="#{scoreBean['地理']}"></property>
<property name="username" value="#{dbBean2.username}"></property>
</bean>

本文详细介绍了Spring框架中依赖注入的基本概念及其实现方式,包括基本数据类型、集合类型的注入方法,以及通过引用方式注入集合类型等内容。
301





