使用XML注入的方式向实例化的bean中注入值

  • 写一个bean ,里面有int,引用类型,数组,List,Set ,Map,Properties等类型的属性成员,并通过Spring注入初始值

这是写好的Bean

package com.bamzhy.day2.Bean;
import com.bamzhy.day2.dao.Dao;
import java.util.*;

public class Bean {
    public Bean(int a) {
        this.a = a;
    }
    public int getA() {
        return a;
    }
    public void setA(int a) {
        this.a = a;
    }
    public Bean(Properties properties) {
        this.properties = properties;
    }
    public Properties getProperties() {
        return properties;
    }
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
    public List<String> getC() {
        return c;
    }
    public void setC(List<String> c) {
        this.c = c;
    }
    public void getB() {
        for (int i = 0; i <b.length ; i++) {
            System.out.println(b[i]);
        }
    }
    public void setB(int[] b) {
        this.b = b;
    }
    public Set<String> getD() {
        return d;
    }
    public void setD(Set<String> d) {
        this.d = d;
    }
    public Map<String, String> getE() {
        return e;
    }
    public void setE(Map<String, String> e) {
        this.e = e;
    }
    public String getKey(Object key) {
        return e.get(key);
    }
    public Dao getDao() {
        System.out.println("我拿到了Dao");
        return dao;
    }
    public void setDao(Dao dao) {
        this.dao = dao;
    }
    Properties properties;
    int a;
    int[] b;
    List<String> c;
    Set<String> d;
    Map<String, String> e;
    Dao dao;
}

这个是XML文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!--<bean  class="com.bamzhy.day2.test.PostProcessor" ></bean>-->
    <!--<bean id="dao" class="com.bamzhy.day2.dao.impl.DaoImpl" init-method="init" destroy-method="destory"></bean>-->
    <!--<bean id="service" class="com.bamzhy.day2.service.impl.ServiceImpl" init-method="init" destroy-method="destory"></bean>-->
    <bean id="mybean" class="com.bamzhy.day2.Bean.Bean" >
        <!--int a;-->
        <constructor-arg name="a" value="25"></constructor-arg>
        <!--Properties properties;-->
        <property name="properties">
            <props>
                <prop key="jdbcUrl">jdbc:mysql://localhost:3306/estore</prop>
                <prop key="username">haha</prop>
                <prop key="password">12345</prop>
            </props>
        </property>
        <!--int[] b;-->
        <property name="b">
            <array>
                <value>1234</value>
            </array>
        </property>
        <!--List<String> c;-->
        <property name="c">
            <list>
                <value>haha1</value>
            </list>
        </property>
        <!--Set<String>  d;-->
        <property name="d">
            <set>
                <value>haha2</value>
            </set>
        </property>
        <!--Map<String,String> e;-->
        <property name="e">
            <map>
                <entry key="user1" value="haha1"></entry>
                <entry key="user2" value="haha2"></entry>
            </map>
        </property>
        <!--Dao dao-->
        <property name="dao" ref="haha">
        </property>
    </bean>
    <bean name="haha" class="com.bamzhy.day2.dao.impl.DaoImpl"></bean>
</beans>

这个是test文件

import com.bamzhy.day2.dao.impl.DaoImpl;
import com.bamzhy.day2.service.impl.ServiceImpl;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    @Test
    public void test(){
        //使用init方法和destory方法来使得每次创建、close都会输出log信息
//        ClassPathXmlApplicationContext classPathXmlApplicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//        DaoImpl dao = (DaoImpl) classPathXmlApplicationContext.getBean("dao");
//        dao.method1();
//        ServiceImpl service = (ServiceImpl) classPathXmlApplicationContext.getBean("service");
//        service.method1();
//        classPathXmlApplicationContext.close();
        //BeanPostProcessor方法
        ClassPathXmlApplicationContext classPathXmlApplicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        Bean mybean = (Bean) classPathXmlApplicationContext.getBean("mybean");
        //Properties properties
        System.out.println(mybean.getProperties());
        //int a
        System.out.println(mybean.getA());
        //int[]b
        mybean.getB();
        //List<> c
        System.out.println(mybean.getC());
        //Set<> d
        System.out.println(mybean.getD());
        //Map<String,String> e
        System.out.println(mybean.getKey("user1"));
        //Dao dao
        System.out.println(mybean.getDao());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值