Spring - 属性注入 5 种方式(XML)
发布日期:2021-06-30 23:47:05 浏览次数:2 分类:技术文章

本文共 6731 字,大约阅读时间需要 22 分钟。

package com.imooc.ioc.demo4;public class User {    private String name;    private Integer age;    public User(String name,Integer age){        this.name = name;        this.age = age;    }    @Override    public String toString() {        return "User{" +                "name='" + name + '\'' +                ", age=" + age +                '}';    }}
package com.imooc.ioc.demo4;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringDemo4 {    @Test    public void demo1(){        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");        User user = (User)applicationContext.getBean("user");        System.out.println(user);    }}
运行结果:User{name='张三', age=23}

Ps:普通的用 value,bean用 ref。

package com.imooc.ioc.demo4;public class Person {    private String name;    private Integer age;    private Cat cat;    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 Cat getCat() {        return cat;    }    public void setCat(Cat cat) {        this.cat = cat;    }    @Override    public String toString() {        return "Person{" +                "name='" + name + '\'' +                ", age=" + age +                ", cat=" + cat +                '}';    }}
package com.imooc.ioc.demo4;public class Cat {    private String name;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Override    public String toString() {        return "Cat{" +                "name='" + name + '\'' +                '}';    }}
package com.imooc.ioc.demo4;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringDemo4 {    @Test    public void demo2(){        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");        Person person = (Person)applicationContext.getBean("person");        System.out.println(person);    }}
运行结果:Person{name='李四', age=32, cat=Cat{name='ketty'}}

运行结果:Person{name='大黄', age=34, cat=Cat{name='小黄'}}

package com.imooc.ioc.demo4;public class Category {    private String name;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Override    public String toString() {        return "Category{" +                "name='" + name + '\'' +                '}';    }}
package com.imooc.ioc.demo4;public class Product {    private String name;    private Double price;    private Category category;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Double getPrice() {        return price;    }    public void setPrice(Double price) {        this.price = price;    }    public Category getCategory() {        return category;    }    public void setCategory(Category category) {        this.category = category;    }    @Override    public String toString() {        return "Product{" +                "name='" + name + '\'' +                ", price=" + price +                ", category=" + category +                '}';    }}
package com.imooc.ioc.demo4;public class ProductInfo {    public Double calculatePrice(){        return Math.random() * 199;    }}
package com.imooc.ioc.demo4;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringDemo4 {    @Test    public void demo3(){        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");        Product product = (Product)applicationContext.getBean("product");        System.out.println(product);    }}
运行结果:Product{name='男装', price=155.43113186211818, category=Category{name='服装'}}

package com.imooc.ioc.demo5;import java.util.*;public class CollectionBean {    private String[] arrs; // 数组类型    private List
list; // List集合类型 private Set
set; // Set集合类型 private Map
map; // Map集合类型 private Properties properties; // 属性类型 public String[] getArrs() { return arrs; } public void setArrs(String[] arrs) { this.arrs = arrs; } public List
getList() { return list; } public void setList(List
list) { this.list = list; } public Set
getSet() { return set; } public void setSet(Set
set) { this.set = set; } public Map
getMap() { return map; } public void setMap(Map
map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } @Override public String toString() { return "CollectionBean{" + "arrs=" + Arrays.toString(arrs) + ", list=" + list + ", set=" + set + ", map=" + map + ", properties=" + properties + '}'; }}
aaa
bbb
ccc
111
222
333
ddd
eee
fff
root
1234
package com.imooc.ioc.demo5;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringDemo5 {    @Test    public void demo1(){       ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");       CollectionBean collectionBean = (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}}

转载地址:https://lux-sun.blog.csdn.net/article/details/84191978 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Spring - Bean管理之注解(@Component、@Controller、@RestController、@Service、@Repository)
下一篇:Java - 动态代理机制讲解(Proxy.newProxyInstance)

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月11日 10时18分48秒