Spring - 属性注入 5 种方式(XML)

简介: Spring - 属性注入 5 种方式(XML)

image.pngimage.png

packagecom.imooc.ioc.demo4;
publicclassUser {
privateStringname;
privateIntegerage;
publicUser(Stringname,Integerage){
this.name=name;
this.age=age;
    }
@OverridepublicStringtoString() {
return"User{"+"name='"+name+'\''+", age="+age+'}';
    }
}
<!--Bean的构造方法的属性注入--><beanid="user"class="com.imooc.ioc.demo4.User"><constructor-argname="name"value="张三"/><constructor-argname="age"value="23"/></bean>
packagecom.imooc.ioc.demo4;
importorg.junit.Test;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
publicclassSpringDemo4 {
@Testpublicvoiddemo1(){
ApplicationContextapplicationContext=newClassPathXmlApplicationContext("applicationContext.xml");
Useruser= (User)applicationContext.getBean("user");
System.out.println(user);
    }
}
运行结果:User{name='张三', age=23}

image.pngPs:普通的用 value,bean用 ref。

packagecom.imooc.ioc.demo4;
publicclassPerson {
privateStringname;
privateIntegerage;
privateCatcat;
publicStringgetName() {
returnname;
    }
publicvoidsetName(Stringname) {
this.name=name;
    }
publicIntegergetAge() {
returnage;
    }
publicvoidsetAge(Integerage) {
this.age=age;
    }
publicCatgetCat() {
returncat;
    }
publicvoidsetCat(Catcat) {
this.cat=cat;
    }
@OverridepublicStringtoString() {
return"Person{"+"name='"+name+'\''+", age="+age+", cat="+cat+'}';
    }
}
packagecom.imooc.ioc.demo4;
publicclassCat {
privateStringname;
publicStringgetName() {
returnname;
    }
publicvoidsetName(Stringname) {
this.name=name;
    }
@OverridepublicStringtoString() {
return"Cat{"+"name='"+name+'\''+'}';
    }
}
<!--Bean的set方法的属性注入--><beanid="person"class="com.imooc.ioc.demo4.Person"><propertyname="name"value="李四"/><propertyname="age"value="32"/><propertyname="cat"ref="cat"/></bean><beanid="cat"class="com.imooc.ioc.demo4.Cat"><propertyname="name"value="ketty"/></bean>
packagecom.imooc.ioc.demo4;
importorg.junit.Test;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
publicclassSpringDemo4 {
@Testpublicvoiddemo2(){
ApplicationContextapplicationContext=newClassPathXmlApplicationContext("applicationContext.xml");
Personperson= (Person)applicationContext.getBean("person");
System.out.println(person);
    }
}
运行结果:Person{name='李四', age=32, cat=Cat{name='ketty'}}

image.png

<!--Bean的p名称空间的属性注入--><beanid="person"class="com.imooc.ioc.demo4.Person"p:name="大黄"p:age="34"p:cat-ref="cat"/><beanid="cat"class="com.imooc.ioc.demo4.Cat"p:name="小黄"/>
运行结果:Person{name='大黄', age=34, cat=Cat{name='小黄'}}

image.png

packagecom.imooc.ioc.demo4;
publicclassCategory {
privateStringname;
publicStringgetName() {
returnname;
    }
publicvoidsetName(Stringname) {
this.name=name;
    }
@OverridepublicStringtoString() {
return"Category{"+"name='"+name+'\''+'}';
    }
}
packagecom.imooc.ioc.demo4;
publicclassProduct {
privateStringname;
privateDoubleprice;
privateCategorycategory;
publicStringgetName() {
returnname;
    }
publicvoidsetName(Stringname) {
this.name=name;
    }
publicDoublegetPrice() {
returnprice;
    }
publicvoidsetPrice(Doubleprice) {
this.price=price;
    }
publicCategorygetCategory() {
returncategory;
    }
publicvoidsetCategory(Categorycategory) {
this.category=category;
    }
@OverridepublicStringtoString() {
return"Product{"+"name='"+name+'\''+", price="+price+", category="+category+'}';
    }
}
packagecom.imooc.ioc.demo4;
publicclassProductInfo {
publicDoublecalculatePrice(){
returnMath.random() *199;
    }
}
<!--Bean的SpEL的属性注入--><beanid="category"class="com.imooc.ioc.demo4.Category"><propertyname="name"value="#{'服装'}"/></bean><beanid="productInfo"class="com.imooc.ioc.demo4.ProductInfo"/><beanid="product"class="com.imooc.ioc.demo4.Product"><propertyname="name"value="#{'男装'}"/><propertyname="price"value="#{productInfo.calculatePrice()}"/><propertyname="category"value="#{category}"/></bean>
packagecom.imooc.ioc.demo4;
importorg.junit.Test;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
publicclassSpringDemo4 {
@Testpublicvoiddemo3(){
ApplicationContextapplicationContext=newClassPathXmlApplicationContext("applicationContext.xml");
Productproduct= (Product)applicationContext.getBean("product");
System.out.println(product);
    }
}
运行结果:Product{name='男装', price=155.43113186211818, category=Category{name='服装'}}

image.png

packagecom.imooc.ioc.demo5;
importjava.util.*;
publicclassCollectionBean {
privateString[] arrs; // 数组类型privateList<String>list; // List集合类型privateSet<String>set; // Set集合类型privateMap<String,Integer>map; // Map集合类型privatePropertiesproperties; // 属性类型publicString[] getArrs() {
returnarrs;
    }
publicvoidsetArrs(String[] arrs) {
this.arrs=arrs;
    }
publicList<String>getList() {
returnlist;
    }
publicvoidsetList(List<String>list) {
this.list=list;
    }
publicSet<String>getSet() {
returnset;
    }
publicvoidsetSet(Set<String>set) {
this.set=set;
    }
publicMap<String, Integer>getMap() {
returnmap;
    }
publicvoidsetMap(Map<String, Integer>map) {
this.map=map;
    }
publicPropertiesgetProperties() {
returnproperties;
    }
publicvoidsetProperties(Propertiesproperties) {
this.properties=properties;
    }
@OverridepublicStringtoString() {
return"CollectionBean{"+"arrs="+Arrays.toString(arrs) +", list="+list+", set="+set+", map="+map+", properties="+properties+'}';
    }
}
<!--集合类型的属性注入--><beanid="collectionBean"class="com.imooc.ioc.demo5.CollectionBean"><!--数组类型--><propertyname="arrs"><list><value>aaa</value><value>bbb</value><value>ccc</value></list></property><!--List集合的属性注入--><propertyname="list"><list><value>111</value><value>222</value><value>333</value></list></property><!--Set集合的属性注入--><propertyname="set"><set><value>ddd</value><value>eee</value><value>fff</value></set></property><!--Map集合的属性注入--><propertyname="map"><map><entrykey="aaa"value="111"/><entrykey="bbb"value="222"/><entrykey="ccc"value="333"/></map></property><!--Properties的属性注入--><propertyname="properties"><props><propkey="username">root</prop><propkey="password">1234</prop></props></property></bean>
packagecom.imooc.ioc.demo5;
importorg.junit.Test;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
publicclassSpringDemo5 {
@Testpublicvoiddemo1(){
ApplicationContextapplicationContext=newClassPathXmlApplicationContext("applicationContext.xml");
CollectionBeancollectionBean= (CollectionBean)applicationContext.getBean("collectionBean");
System.out.println(collectionBean);
    }
}
运行结果:CollectionBean{arrs=[aaa, bbb, ccc], list=[111, 222, 333], set=[ddd, eee, fff], map={aaa=111, bbb=222, ccc=333}, properties={password=1234, username=root}}
目录
相关文章
|
1月前
|
XML Java 数据格式
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
这篇文章是Spring5框架的实战教程,主要介绍了如何在Spring的IOC容器中通过XML配置方式使用外部属性文件来管理Bean,特别是数据库连接池的配置。文章详细讲解了创建属性文件、引入属性文件到Spring配置、以及如何使用属性占位符来引用属性文件中的值。
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
|
14天前
|
XML Java 数据格式
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
Spring 第二节内容补充 关于Bean配置的更多内容和细节 万字详解!
91 18
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
|
1天前
|
XML Java 数据格式
spring复习02,xml配置管理bean
详细讲解了Spring框架中基于XML配置文件管理bean的各种方式,包括获取bean、依赖注入、特殊值处理、属性赋值、集合类型处理、p命名空间、bean作用域及生命周期和自动装配。
spring复习02,xml配置管理bean
|
1月前
|
XML Java 数据格式
Spring5入门到实战------4、IOC容器-Bean管理XML方式、集合的注入(二)
这篇文章是Spring5框架的实战教程,主题是IOC容器中Bean的集合属性注入,通过XML配置方式。文章详细讲解了如何在Spring中注入数组、List、Map和Set类型的集合属性,并提供了相应的XML配置示例和Java类定义。此外,还介绍了如何在集合中注入对象类型值,以及如何使用Spring的util命名空间来实现集合的复用。最后,通过测试代码和结果展示了注入效果。
Spring5入门到实战------4、IOC容器-Bean管理XML方式、集合的注入(二)
|
1月前
|
XML Java 数据格式
Spring5入门到实战------11、使用XML方式实现AOP切面编程。具体代码+讲解
这篇文章是Spring5框架的AOP切面编程教程,通过XML配置方式,详细讲解了如何创建被增强类和增强类,如何在Spring配置文件中定义切入点和切面,以及如何将增强逻辑应用到具体方法上。文章通过具体的代码示例和测试结果,展示了使用XML配置实现AOP的过程,并强调了虽然注解开发更为便捷,但掌握XML配置也是非常重要的。
Spring5入门到实战------11、使用XML方式实现AOP切面编程。具体代码+讲解
|
1月前
|
XML Java 数据格式
Spring5入门到实战------6、IOC容器-Bean管理XML方式(自动装配)
这篇文章是Spring5框架的入门教程,详细讲解了IOC容器中Bean的自动装配机制,包括手动装配、`byName`和`byType`两种自动装配方式,并通过XML配置文件和Java代码示例展示了如何在Spring中实现自动装配。
Spring5入门到实战------6、IOC容器-Bean管理XML方式(自动装配)
|
1月前
|
XML Java 数据库
Spring5入门到实战------15、事务操作---概念--场景---声明式事务管理---事务参数--注解方式---xml方式
这篇文章是Spring5框架的实战教程,详细介绍了事务的概念、ACID特性、事务操作的场景,并通过实际的银行转账示例,演示了Spring框架中声明式事务管理的实现,包括使用注解和XML配置两种方式,以及如何配置事务参数来控制事务的行为。
Spring5入门到实战------15、事务操作---概念--场景---声明式事务管理---事务参数--注解方式---xml方式
|
28天前
|
Java Spring 开发者
Spring 框架配置属性绑定大比拼:@Value 与 @ConfigurationProperties,谁才是真正的王者?
【8月更文挑战第31天】Spring 框架提供 `@Value` 和 `@ConfigurationProperties` 两种配置属性绑定方式。`@Value` 简单直接,适用于简单场景,但处理复杂配置时略显不足。`@ConfigurationProperties` 则以类级别绑定配置,简化代码并更好组织配置信息。本文通过示例对比两者特点,帮助开发者根据具体需求选择合适的绑定方式,实现高效且易维护的配置管理。
34 0
|
30天前
|
缓存 Java 数据库连接
Spring Boot 资源文件属性配置,紧跟技术热点,为你的应用注入灵动活力!
【8月更文挑战第29天】在Spring Boot开发中,资源文件属性配置至关重要,它让开发者能灵活定制应用行为而不改动代码,极大提升了可维护性和扩展性。Spring Boot支持多种配置文件类型,如`application.properties`和`application.yml`,分别位于项目的resources目录下。`.properties`文件采用键值对形式,而`yml`文件则具有更清晰的层次结构,适合复杂配置。此外,Spring Boot还支持占位符引用和其他外部来源的属性值,便于不同环境下覆盖默认配置。通过合理配置,应用能快速适应各种环境与需求变化。
33 0
|
30天前
|
安全 Java 开发者
开发者必看!@Resource与private final的较量,Spring Boot注入技巧大揭秘,你不可不知的细节!
【8月更文挑战第29天】Spring Boot作为热门Java框架,其依赖注入机制备受关注。本文通过对比@Resource(JSR-250规范)和@Autowired(Spring特有),并结合private final声明的字段注入,详细探讨了两者的区别与应用场景。通过示例代码展示了@Resource按名称注入及@Autowired按类型注入的特点,并分析了它们在注入时机、依赖性、线程安全性和单一职责原则方面的差异,帮助开发者根据具体需求选择最合适的注入策略。
34 0