属性依赖注入
来源:互联网 发布:camhi监控软件说明书 编辑:程序博客网 时间:2024/06/05 11:00
基于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;}@Overridepublic String toString() {return "User [uid=" + uid + ", username=" + username + ", age=" + age+ "]";}}
2spring配置
<!-- 配置service <bean> 配置需要创建的对象id :用于之后从spring容器获得实例时使用的class :需要创建实例的全限定类名构造方法注入:<consturctor-arg> 用于配置构造方法中一个参数argumentindex:参数的索引号,从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 {@Testpublic 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;}@Overridepublic 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;}@Overridepublic 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命令空间方法注入
该方法是对setter方法进行简化,替换<property>,而是在<bean p:属性名= “普通数据 ” p:属性名-ref = "引用数据">
<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>
阅读全文
0 0
- 三、依赖注入属性
- spring属性依赖注入
- 属性依赖注入
- set方法注入依赖属性
- Spring DI 依赖注入属性
- IOC依赖注入集合属性
- spring依赖注入方式一:属性注入
- 依赖注入DI(Dependency Injection)--属性注入
- spring mvc依赖属性注入属性设置
- Action的属性依赖注入详解
- Spring依赖注入 — 集合属性配置
- Spring依赖注入 — 属性自动装配
- Spring依赖注入 — 集合属性配置
- Spring依赖注入 — 属性自动装配
- struts2中action属性的依赖注入
- Spring DI 集合属性依赖注入
- Spring属性依赖注入(手动装配)
- spring 依赖注入bean的属性
- Android Wi-Fi源码分析之wpa_supplicant初始化(二):wpa_supplicant_init中的三个关键点
- 搭建一套phpstorm+wamp+xdebug调试PHP的环境
- PageRank算法详解
- 给别人做项目一些注意事项
- MapReduce used in log analysis
- 属性依赖注入
- textarea点击一下,写字的光标就在中间部位
- eclipse中发布项目到nexus私服
- suse install python pip
- ng-if,ng-show,ng-hide的区别
- ant 安装和环境配置的方法
- Android开发第三方登录--QQ登录
- linux中samba服务器的安装及其配置
- sql字段切分