基于xml装配:
构造方法注入
1目标类
package com.itheima.f_xml.a_construct; public class User { private Integer uid ; private String username ; private Integer age ; public User(Integer uid,String username) { super(); this.uid = uid; this.username = username; } public User(String username,Integer age) { super(); this.username = username; this.age = age; } public Integer getUid() { return uid; } public void setUid(Integer uid) { this.uid = uid; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Override public String toString() { return "User [uid=" + uid + ",username=" + username + ",age=" + age + "]"; } }
2spring配置
<!-- 配置service <bean> 配置需要创建的对象 id :用于之后从spring容器获得实例时使用的 class :需要创建实例的全限定类名 构造方法注入: <consturctor-arg> 用于配置构造方法中一个参数argument index:参数的索引号,从0开始,如果只有索引,匹配到多个构造方法时,默认使用第一个。 type : 确定参数类型 --> <bean id="userId" class="com.itheima.f_xml.a_construct.User"> <constructor-arg index="0" type="java.lang.String" value="1"></constructor-arg> <constructor-arg index="1" type="java.lang.Integer" value="2"></constructor-arg> </bean>
3测试
package com.itheima.f_xml.a_construct; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestCon { @Test public void demo02(){ String xmlPath = "com/itheima/f_xml/a_construct/beans.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); User user= (User) applicationContext.getBean("userId"); System.out.println(user); }}
setter方法注入
1目标类
package com.itheima.f_xml.b_setter; public class Person { private String name ; private Integer age ; private Address homeaddr ; private Address companyaddr ; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Address getHomeaddr() { return homeaddr; } public void setHomeaddr(Address homeaddr) { this.homeaddr = homeaddr; } public Address getCompanyaddr() { return companyaddr; } public void setCompanyaddr(Address companyaddr) { this.companyaddr = companyaddr; } @Override public String toString() { return "Person [name=" + name + ",age=" + age + ",homeaddr=" + homeaddr + ",companyaddr=" + companyaddr + "]"; } }
package com.itheima.f_xml.b_setter; public class Address { private String addr ; private String tel ; public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } @Override public String toString() { return "Address [addr=" + addr + ",tel=" + tel + "]"; } }
2配置
<!-- setter方法注入 property --> <bean id = "personid" class="com.itheima.f_xml.b_setter.Person"> <property name="name" value = "**"></property> <property name="age" value="27"></property> <property name="homeaddr" ref="homeAddrId"></property> <property name="companyaddr" ref="companyAddrId"></property> </bean> <bean id = "homeAddrId" class="com.itheima.f_xml.b_setter.Address"> <property name="addr" value="湖北"></property> <property name="tel" value="158"></property> </bean> <bean id="companyAddrId" class="com.itheima.f_xml.b_setter.Address"> <property name="addr" value="上海"></property> <property name="tel" value="159"></property> </bean>3测试
p命令空间方法注入
<bean id = "personid" class="com.itheima.f_xml.b_setter.Person" p:name = "zn" p:age="27" p:homeaddr-ref="homeAddrId" p:companyaddr-ref= "companyAddrId"> </bean> <bean id = "homeAddrId" class="com.itheima.f_xml.b_setter.Address" p:addr = "湖北" p:tel = "111"> </bean> <bean id="companyAddrId" class="com.itheima.f_xml.b_setter.Address" p:addr = "sh" p:tel = "123"> </bean>
SpEL注入
对<property>进行统一编程,所有的内容都使用value
<!-- <property name="name" value="#{'jack'}"></property> <property name="name" value="#{personId.name.toUpperCase()}"></property> 通过另一个bean,获得属性,调用方法 <property name="name" value="#{personId.name?.toUpperCase()}"></property> ?.如果对象不为null,将调用方法 <property name="pi" value="#{T(java.lang.Math).PI}"></property> 静态类的字段 --> <bean id="personId" class = "com.itheima.f_xml.c_spEL.Person" > <property name="name" value="#{personId.name?.toUpperCase()}"></property> <property name="pi" value="#{T(java.lang.Math).PI}"></property> </bean>
集合注入
<!-- 集合的注入都是给<property>添加子标签 数组:标签<array> List: <list> Set:<set> Map:<map> map存k/v键值对,用entry Properties: <props> <prop key="">value</prop> 普通数据: 标签<value> 引用数据:<ref> --> <bean id="personId" class = "com.itheima.f_xml.c_spEL.Person" > <property name="arrayData"> <array> <value>dd</value> <value>dd1</value> <value>dd2</value> <value>dd3</value> </array> </property> <property name="listData"> <list> <value>list1</value> <value>list2</value> <value>list3</value> <value>list4</value> </list> </property> <property name="setData"> <set> <value>set1</value> <value>set2</value> </set> </property> <property name="mapData"> <map> <entry key="map" value="地图"></entry> <entry key="jack" value="捷克"></entry> </map> </property> <property name="propsData"> <props> <prop key="高富帅">洋</prop> <prop key="白富美">美</prop> <prop key="屌丝">周</prop> </props> </property> </bean>