spring普通值注入

Spring-ioc-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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">


</beans>

Spring xml注入

對象單例和多例模式的設置

創建一個Hello對象

/**
 * Created by Administrator on 2018/1/17.
 */
public class Hello {
    private  String name;

    public String getName() {
        return name;
    }

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

spring.xml注入對象

  <!--創建一個bean對象-->
    <!--scope="singleton" 單例模式 ,默認爲單例模式-->
    <!--scope="prototype" 多例模式-->
    <bean name="hello" class="com.zx.spring.model.Hello" scope="prototype">
        <property name="name" value="spring"/>
    </bean>

加載spring.xml裏面的對象

public class TestSpringh {

    @Test
    public  void show(){
        //加載spring.xml配置文件時創建bean
        //ApplicationContext applicationContext = new ClassPathXmlApplicationContext("");

        //使用某個bean才創建
        BeanFactory beanFactory =  new ClassPathXmlApplicationContext("spring.xml");

        //getBean獲取的是Object類型
        Hello hello1 =(Hello) beanFactory.getBean("hello");//通過bean對象的名稱獲取
        Hello hello2 = (Hello) beanFactory.getBean("hello");

        System.out.println(hello1.getName());
        /* 
        false,因爲spring.xml裏面hello設置的爲scope="prototype"(多例模式),當scope="singleton"             時(單例模式),打印true
        */
        System.out.println(hello1 == hello2);

    }

注入

創建一個Group對象

public class Group {
    private String name;

    public String getName() {
        return name;
    }

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

創建一個USer對象用於數組,List,Set,Map的注入

public class User {
    private String[] name;//數組注入
    private List<Group> groupList;//List注入
    private Set<Group>  groupSet;//Set注入
    private Map<String,Group> groupMap;//Map注入

    public Map<String, Group> getGroupMap() {
        return groupMap;
    }

    public void setGroupMap(Map<String, Group> groupMap) {
        this.groupMap = groupMap;
    }

    public Set<Group> getGroupSet() {
        return groupSet;
    }

    public void setGroupSet(Set<Group> groupSet) {
        this.groupSet = groupSet;
    }

    public List<Group> getGroupList() {
        return groupList;
    }

    public void setGroupList(List<Group> groupList) {
        this.groupList = groupList;
    }

    public String[] getName() {
        return name;
    }

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

spring.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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

    <!--創建一個bean對象-->
    <!--scope="singleton" 單例模式 ,默認爲單例模式-->
    <!--scope="prototype" 多例模式-->
    <bean name="hello" class="com.zx.spring.model.Hello" scope="prototype">
        <property name="name" value="spring"/>
    </bean>


    <!-- 創建Group對象,用於向List裏面添加對象-->
    <bean name="group1" class="com.zx.spring.model.Group">
        <property name="name" value="group1"></property>
    </bean>
    <bean name="group2" class="com.zx.spring.model.Group">
        <property name="name" value="group2"></property>
    </bean>


    <!-- 數組注入 -->
    <bean name="user" class="com.zx.spring.model.User">
        <property name="name">
            <array>
                <value>戰士</value>
                <value>法師</value>
                <value>弓箭手</value>
            </array>
        </property>

        <!-- List注入 -->
        <property name="groupList">
            <list>
             //
                <ref bean="group1"/>
                <ref bean="group2"/>
            </list>

        </property>

        <!-- set注入 -->
        <property name="groupSet">
            <set>
                <ref bean="group1"/>
                <ref bean="group2"/>
            </set>
        </property>
        <!-- map注入 -->
        <property name="groupMap">
            <map>
                <entry key="g1" value-ref="group1"></entry>
                <entry key="g2" value-ref="group2"></entry>
            </map>
        </property>
    </bean>

</beans>

加載spring.xml注入的對象


 **
 * Spring  IOC(控制反轉)
 * 將控制管理權不由JavaBean管理,交給Spring容器管理
 *
 * DI 依賴注入
 * --分層
 *      --上層依賴於下層
 *      --下層服務於上層
 *
 *
 *
 * Created by Administrator on 2018/1/18.
 */

public class TestSpringh {

    @Test
    public  void show(){
        //加載spring 配置文件時創建bean
        //ApplicationContext applicationContext = new ClassPathXmlApplicationContext("");

        //使用某個bean才創建
        BeanFactory beanFactory =  new ClassPathXmlApplicationContext("spring.xml");

        //getBean獲取的是Object類型
        Hello hello1 =(Hello) beanFactory.getBean("hello");
        Hello hello2 = (Hello) beanFactory.getBean("hello");

        System.out.println(hello1.getName());
        System.out.println(hello1 == hello2);

    }

    @Test
    public void showUser(){
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring.xml");

        User user =  beanFactory.getBean("user",User.class);

        String[] name = user.getName();

        for(int i=0;i<name.length;i++){
            System.out.println(name[i]);
        }

    }
    //list集合
    @Test
    public void showList(){
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring.xml");
        //通過bean名稱和對象.class獲取
        User user = beanFactory.getBean("user",User.class);

        List<Group> list = user.getGroupList();

        for(Group grp:list){
            System.out.println(grp.getName());
        }

    }

    //set集合
    @Test
    public void showSet(){
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring.xml");
        User user = beanFactory.getBean("user",User.class);
        Set<Group> set = user.getGroupSet();

        for(Group group:set){
            System.out.println(group.getName());
        }
    }
    //Map集合
    @Test
    public void showMap(){
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring.xml");
        User user = beanFactory.getBean("user",User.class);
        Map<String,Group> map  = user.getGroupMap();

        //遍歷Map集合
        for (Map.Entry<String,Group> entry:map.entrySet()){
            System.out.println(entry.getKey());
            System.out.println(entry.getValue().getName());
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章