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}
Ps:普通的用 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'}}
<!--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='小黄'}}
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='服装'}}
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}}