Spring IOC (2)
标签: JAVA框架
1.自动注入
<bean autowire="">
用于指定自动注入规则.可以使用byType,byName,constructor等,用于简化注入
1.byType是用于类型相同的匹配注入,有2个及以上匹配会出现异常
2.byName是id与类中属性相同名字的才可以注入
3.constructor 与byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常
4.通过bean类的自省机制来决定是使用constructor还是byType方式进行自动装配。如果发现默认的构造器,那么将使用byType方式.
2.各种类型信息的注入配置格式
a.注入字符串,数值,单个数值
<property name="" value="">
b.注入bean对象
c.注入集合list,set,map,properties
d.Spring表达式注入
vluae="#{dbParams.user}" **需要get方法
例子::
Bean
package com.cd.bean;
import java.sql.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import javax.ejb.EnterpriseBean;
public class MessageBean {
private String name;
private int age;
private Date birth;
private List<String> friends;
private Set<String> cities;
private Map<String,String> books;
private Properties pro;
public void setPro(Properties pro) {
this.pro = pro;
}
public void setBooks(Map<String, String> books) {
this.books = books;
}
public void setCities(Set<String> cities) {
this.cities = cities;
}
public void setFriends(List<String> friends) {
this.friends = friends;
}
public void setBirth(String birth) {
Date date = Date.valueOf(birth);
this.birth = date;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void show(){
System.out.println("name:"+name);
System.out.println("age:"+age);
System.out.println("date:"+birth);
System.out.println("friends"+friends);
for(String s : cities){
System.out.println(s);
}
Set<Entry<String,String>> booksSet= books.entrySet();
for(Entry<String, String> c:booksSet){
System.out.println(c.getKey()+c.getValue());
}
Set<Object> keys = pro.keySet();
for(Object key:keys){
System.out.println("key"+key+
"value"+pro.getProperty(key.toString()));
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
>
<bean id="msg" class="com.cd.bean.MessageBean">
<property name="name" value="root">
</property>
<property name="age" value="18">
</property>
<property name="birth" value="1995-09-07">
</property>
<property name="friends">
<list>
<value>tom</value>
<value>jack</value>
<value></value>
</list>
</property>
<property name="cities">
<set>
<value>北京</value>
<value>上海</value>
</set>
</property>
<property name="books">
<map>
<entry key="1001" value="Java基础"></entry>
<entry key="1002" value="JavaWeb开发"></entry>
</map>
</property>
<property name="pro">
<props>
<prop key="name">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</beans>
Test测试类
package com.cd.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.cd.bean.MessageBean;
public class MessageTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
String conf = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
MessageBean msg = (MessageBean)ac.getBean("msg");
msg.show();
}
}