标签:bank card xmlns work span 容器 工商银行 ret add
1、 依赖注入—dependency injection
bean对象依赖于spring容器创建,bean对象依赖的资源由容器来设置和装配
2、 spring注入方式—--构造器注入
见spring ioc创建对象
3、 spring注入方式----setter注入
要求被注入的属性必须有set方法。Set方法的方法名由set+属性首字母大写。如果属性是boolean没有get方法,是is
a) 常量注入
<bean name="student" class="com.silvan.pojo.Student"> <property name="name" value="zhangsan"></property> </bean> //student类 public class Student { private String name; public void setName(String name) { this.name = name; } public void show(){ System.out.println("name="+name+" addr="+addr.getAddress()); } } //测试 public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); Student stu = (Student) ac.getBean("student"); stu.show(); }
b) Bean注入
//bean配置文件 <bean name="addr" class="com.silvan.pojo.Address"> <property name="address" value="shandong"></property> </bean> <bean name="student" class="com.silvan.pojo.Student"> <property name="name" value="zhangsan"></property> <property name="addr" ref="addr"></property> </bean> //student类 public class Student { private String name; private Address addr; public void setAddr(Address addr) { this.addr = addr; } public void setName(String name) { this.name = name; } public void show(){ System.out.println("name="+name+" addr="+addr.getAddress()); } } //address类 public class Address { private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } } //测试 public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); Student stu = (Student) ac.getBean("student"); stu.show(); }
c) 数组注入
<bean name="student" class="com.silvan.pojo.Student"> <property name="name" value="zhangsan"></property> <property name="addr" ref="addr"></property> <property name="books"> <array> <value>山居笔记</value> <value>撒哈拉的故事</value> <value>基督山伯爵</value> <value>飘</value> </array> </property> </bean>
d) List注入
<property name="hobbies"> <list> <value>看书</value> <value>听歌</value> <value>看电视</value> <value>运动</value> </list> </property>
e) Map注入
<property name="cards"> <map> <entry key="bank of china" value="1324325325"></entry> <entry> <key><value>工商银行</value></key> <value>75784758748784</value> </entry> </map> </property>
f) Set注入
<property name="games"> <set> <value>三国</value> <value>lol</value> <value>王者荣耀</value> </set> </property>
g) Null注入
<property name="wife"><null/></property>
h) Properties注入
<property name="info"> <props> <prop key="学号">21203</prop> <prop key="sex">boy</prop> <prop key="name">小强</prop> </props> </property>
i) P命名空间注入
//配置文件头部加入p声明 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> //p命名空间注入 <!-- p命名空间注入 属性依然要设置set方法 --> <bean name="user" class="com.silvan.pojo.User" p:name="杨幂" p:age="29"></bean>
j) C命名空间注入
//头部配置 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- c命名空间注入要求有对应参数的构造方法 --> <bean name="user1" class="com.silvan.pojo.User" c:name="赵丽颖" c:age="26"></bean>
标签:bank card xmlns work span 容器 工商银行 ret add
原文地址:http://www.cnblogs.com/zhouyeqin/p/7207603.html