IoC 构造器注入
<!-- 声明TelePhone -->
<bean id="phone" class="model.TelePhone">
<!-- index指定构造器的第几个参数,name指定参数名,value指定参数值 -->
<constructor-arg name="cpu" index="0" value="高通"></constructor-arg>
<constructor-arg name="ram" index="1" value="迅闪"></constructor-arg>
</bean>
测试方法:
@org.junit.Test
public void test5() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
TelePhone t = ac.getBean("phone",TelePhone.class);
t.show();
}
bean对应的java类 TelePhone.java
/**
* 手机类,模拟构造器注入
* @author haifeng
*
*/
public class TelePhone {
private String cpu;
private String ram;
public TelePhone() {
super();
}
public TelePhone(String cpu, String ram) {
this.cpu = cpu;
this.ram = ram;
}
public void show() {
System.out.println( "TelePhone [cpu:" + cpu + ", ram:" + ram + "]");
}
}
SET方式注入对象:
注入的对象需要在beans中声明,使用ref进行注入
<!-- 声明Computer -->
<bean id="student" class="model.Student">
<property name="name" value="haydn"></property>
<property name="computer" ref="computer"></property>
<property name="phone" ref="phone"></property>
</bean>
测试方法:
@org.junit.Test
public void test6() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Student stu = ac.getBean("student",Student.class);
stu.show();
}
bean的对应JAVA类,Student.java
/**
* student实体类,模拟注入类对象
* @author haifeng
*
*/
public class Student {
private String name;
private Computer computer;
private TelePhone phone;
public void setName(String name) {
this.name = name;
}
public void setComputer(Computer computer) {
this.computer = computer;
}
public void setPhone(TelePhone phone) {
this.phone = phone;
}
public void show() {
System.out.println("学生姓名:" + name);
computer.show();
phone.show();
}
}
List set map propetries 注入
<!-- 注入集合 -->
<bean id="message" class="bean.Message">
<!-- 字符串注入null值,方式一:不写该配置,方式2:不写value,加个null标签 -->
<property name="name" ><null/></property>
<property name="age" value="23"></property>
<!-- 注入list -->
<property name="friends">
<list>
<value>摩严</value>
<value>白子画</value>
<value>杀阡陌</value>
<value>花千骨</value>
<value>洛十一</value>
<value>糖宝</value>
</list>
</property>
<!-- 注入set -->
<property name="cities">
<set>
<value>北京</value>
<value>上海</value>
</set>
</property>
<!-- 注入MAP -->
<property name="score">
<map>
<entry key="语文" value="98"> </entry>
<entry key="数学" value="99"></entry>
<entry key="英语" value="100"></entry>
</map>
</property>
<!-- 注入配置文件 -->
<property name="dbParams">
<props>
<prop key="url">localhost</prop>
<prop key="username">root</prop>
<prop key="password">123</prop>
</props>
</property>
<!-- 表达式注入,类似EL表达式用#{bean对象名.属性}标记, -->
<property name="password" value="#{dbProps.password}"></property>
</bean>
<!-- spring标签注入 ,其他bean中使用ref调用,set Map Propties都可以,使用时需要加入命名空间-->
<util:list id="someList">
<value>摩严</value>
<value>白子画</value>
<value>杀阡陌</value>
<value>花千骨</value>
<value>洛十一</value>
<value>糖宝</value>
</util:list>
<!-- 注入list -->
<property name="friends" ref="someList"></property>
<!-- 加载properties文件创建Properties对象,location="classpath:dbcp.properties" location表示指定文件路径
classpath表示绝对路径 -->
<util:properties id="dbProps" location="classpath:dbcp.properties">
</util:properties>
测试类JAVA代码:
@org.junit.Test
public void test7() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Message msg = ac.getBean("message",Message.class);
msg.show();
}
bean的对应JAVA类,Message.java
/**
* 消息类,模拟各种类型对象注入
* @author haifeng
*
*/
public class Message {
private String name;
private int age;
private List<String> friends;
private Set<String> cities;
private Map<String,Integer> score;
private Properties dbParams;
private String password;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setFriends(List<String> friends) {
this.friends = friends;
}
public void setCities(Set<String> cities) {
this.cities = cities;
}
public void setScore(Map<String, Integer> score) {
this.score = score;
}
public void setDbParams(Properties dbParams) {
this.dbParams = dbParams;
}
public void setPassword(String password) {
this.password = password;
}
public void show() {
System.out.println( "Message [name:" + name + ", age:" + age +", friends"+
friends.toString()+",cities:" +cities.toString()+",score:"+score.toString()+
",properties:"+dbParams.toString()+"password:"+password+"]");
}
/**
* map的遍历
*/
public void showMap(){
Set<Entry<String,Integer>> map = score.entrySet();
for(Entry<String,Integer> e : map) {
System.out.println(e.getKey()+e.getValue());
}
}
/**
* protries的遍历
*/
public void showProp(){
Set<Object> keys = dbParams.keySet();
for(Object o : keys) {
System.out.println(dbParams.get(o));
}
}
}
自动装配(bean对象注入简配)
autowire:
byName:按名称匹配,beanid名与setXXX名字匹配植入,否则不注入
byType:按类型匹配,bean的class与类型待注入的bean属性类型一致时注入,否则不注入
<!-- 自动装配,autowire:byName根据名称,byType根据类型,不写默认会自动装配,但可读性很差 -->
<!-- 自动装配的bean的id需要和待注入的bean的属性name一致,注入的名字需要和set方法的名字一致(不含set,首字母小写) -->
<!-- byType和名字没关系,根据类型自动装配 -->
<!-- 但是byType的bean中有相同类型的bean出现则会出错,而且还要以单例模式声明要注入的bean -->
<bean id="student" class="model.Student" autowire="byName">
<property name="name" value="haydn"></property>
</bean>