Spring DI

Spring DI

  • class
public class Address {

    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
public class Student {

    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;

    public void setName(String name) {
        this.name = name;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    public void show(){
        System.out.println("name="+ name
                + ",address="+ address.getAddress()
                + ",books="
        );
        for (String book:books){
            System.out.print("<<"+book+">>\t");
        }
        System.out.println("\n爱好:"+hobbys);

        System.out.println("card:"+card);

        System.out.println("games:"+games);

        System.out.println("wife:"+wife);

        System.out.println("info:"+info);

    }
}
  • XML
<bean id="stu" class="org.example.pojo.Student">
        <property name="name" value="lixing"/>
    	<!--数组-->
        <property name="books">
            <array>
                <value>《西游记》</value>
                <value>《水浒传》</value>
                <value>《红楼梦》</value>
                <value>《三国演义》</value>
            </array>
        </property>
    	<!--map-->
        <property name="card">
            <map key-type="java.lang.String" value-type="java.lang.Integer">
                <entry key="aaa" value="1"/>
                <entry key="bbb" value="2"/>
            </map>
        </property>
    	<!--list-->
        <property name="hobbys">
            <list>
                <value>跑步</value>
                <value>爬山</value>
            </list>
        </property>
    	<!--set-->
        <property name="games">
            <set>
                <value>王者荣耀</value>
                <value>和平精英</value>
            </set>
        </property>
    	<!--空值-->
        <property name="wife">
            <null/>
        </property>
    	<!--assocation-->
        <property name="address" ref="addr"/>
    	<!--property-->
        <property name="info">
            <props>
                <prop key="姓名">李星</prop>
                <prop key="学号">19030100010</prop>
                <prop key="性别"></prop>
            </props>
        </property>
    </bean>
	
    <bean id="addr" class="org.example.pojo.Address">
        <property name="address" value="河北"/>
    </bean>
  • Test
@Test
public void Studenttest(){
    ApplicationContext ac = new ClassPathXmlApplicationContext("Beans2.xml");
    Student st = (Student) ac.getBean("stu");
    st.show();
}
  • P命名空间
xmlns:p="http://www.springframework.org/schema/p"
<bean id="stu" class="org.example.pojo.Student" p:name="LX" p:age="22"/>
  • C命名空间
 xmlns:c="http://www.springframework.org/schema/c"
<bean id="stu" class="org.example.pojo.Student" p:name="LX" p:age="22"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李星且小白blog.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值