zoukankan      html  css  js  c++  java
  • Sping4之注入参数

    Spring的依赖注入不仅可以注入基本类型,也可以注入包括model,list等等类型

    package com.hongcong.test;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.hongcong.model.People;
    
    public class test {
        public static void main(String[] args) {
            ClassPathXmlApplicationContext ca = new  ClassPathXmlApplicationContext("beans.xml");
            //Bean注入
            People people1 = (People)ca.getBean("people1");
            System.out.println(people1);
            //内部bean
            People people2 = (People)ca.getBean("people2");
            System.out.println(people2);
            //null注入
            People people3 = (People)ca.getBean("people3");
            System.out.println(people3);
            //集合注入
            People people4 = (People)ca.getBean("people4");
            System.out.println(people4);
            
            
        }
    }

    beans.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">
        
        <bean id="dog" class="com.hongcong.model.Dog">
            <property name="name" value="Jack"/>
        </bean>
        
        <!--Bean注入  -->
        <bean id="people1" class="com.hongcong.model.People">
            <property name="id" value="1"/>
            <property name="name" value="小一"/>
            <property name="age" value="11"/>
            <property name="dog" ref="dog"></property>
        </bean>
        
        <!--内部bean  -->
        <bean id="people2" class="com.hongcong.model.People">
            <property name="id" value="1"/>
            <property name="name" value="小一"/>
            <property name="age" value="11"/>
            <property name="dog">
                <bean class="com.hongcong.model.Dog" >
                    <property name="name" value="Tom"></property>
                </bean>
            </property>
        </bean>
        
        <!--null注入  -->
        <bean id="people3" class="com.hongcong.model.People">
            <property name="id" value="1"/>
            <property name="name" value="小一"/>
            <property name="age" value="11"/>
            <property name="dog" >
                <null></null>
            </property>
        </bean>
        
        <!--list注入  -->
        <bean id="people4" class="com.hongcong.model.People">
            <property name="id" value="1"/>
            <property name="name" value="小一"/>
            <property name="age" value="11"/>
            <property name="loves" >
                <list>
                    <value>唱歌</value>
                    <value>听歌</value>
                </list>
            </property>
        </bean>
    </beans>

    people

    package com.hongcong.model;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class People {
        private int id;
        private String name;
        private int age;
        private Dog dog;
        List<String> loves = new ArrayList<String>();
        
        public People() {
            super();
            // TODO Auto-generated constructor stub
        }
        
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        
        
        public Dog getDog() {
            return dog;
        }
        public void setDog(Dog dog) {
            this.dog = dog;
        }
        public List<String> getLoves() {
            return loves;
        }
    
        public void setLoves(List<String> loves) {
            this.loves = loves;
        }
    
        @Override
        public String toString() {
            return "People [id=" + id + ", name=" + name + ", age=" + age
                    + ", dog=" + dog + ", loves=" + loves + "]";
        }
    
         
    }

    dog

    package com.hongcong.model;
    
    public class Dog {
        private String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
        
    }
  • 相关阅读:
    在visual studio 2010中调用ffmpeg
    RTP/RTCP/RTSP/SIP/SDP
    YV12数据与AVFrame的相互转换
    实现输出h264直播流的rtmp服务器
    RTMP协议发送H.264编码及AAC编码的音视频,实现摄像头直播
    CentOS 硬盘分区方案
    ubuntu默认root密码
    windows下ACE怎样安装与使用说明?
    CentOS 6.4 图文安装教程
    我自己的FFMpeg编译之路
  • 原文地址:https://www.cnblogs.com/hongcong/p/7587618.html
Copyright © 2011-2022 走看看