• Spring 依赖注入


    1、设值注入(最常用)
    在 设值注入中,Bean必须有针对该属性的setter方法,并且Bean定义文件中,有该属性的设置。
    其实就是常说的getter setter方法。
    2、构造方法注入(较常用)
    Bean中必须有构造方法
        (1)bean    设为User(Integer age,String name)
        (2)定义文件
                             <bean id ="user" class="包.类">
                                       <constructor-arg   value = "参数值" Index=“0” type=java.lang.Integer>
                                       <constructor-arg   value = "参数值" Index=“1” type=java.lang.String>
                             </bean>
                可以通过index制定具体那个参数,或者通过type制定,或者可以省略。
     
     
    ref 表示对另外一个bean的引用
     
    <ref   bean = "另一个bean,可以是相同的文件中的,或者不同文件中的">
    <ref   local = "相同的定义文件中">
     
     
     
    集合对象(List、Set、map)注入和获取
      1、bean文件
    import java.util.List;
    import java.util.Map;
    import java.util.Properties;
    import java.util.Set;
    public class Bean2{
    private List list;
    private Map map;
    private Set set;
    private Properties property;
    public Properties getProperty() {
    return property;
    }
    public void setProperty(Properties property) {
    this.property = property;
    }
    public Map getMap() {
    return map;
    }
    public void setMap(Map map) {
    this.map = map;
    }
    public Set getSet() {
    return set;
    }
    public void setSet(Set set) {
    this.set = set;
    }
    public List getList() {
    return list;
    }
    public void setList(List list) {
    this.list = list;
    }
    }
    bean.xml文件
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" >
    <beans>
    <bean id="bean2" class="com.pets.Bean2">
    <property name="list">
    <list>
    <value>a</value>
    <value>b</value>
    <value>c</value>
    <value>d</value>
    </list>
    </property>
    <property name="set">
    <set>
    <value>a</value>
    <value>b</value>
    <value>c</value>
    <value>d</value>
    </set>
    </property>
    <property name="map">
    <map>
    <entry>
    <key>
    <value>1</value>
    </key>
    <value>one</value>
    </entry>
    <entry key="2" value="two"/>
    </map>
    </property>
    <property name="property">
    <props>
    <prop key="1">one</prop>
    <prop key="2">two</prop>
    </props>
    </property>
    </bean>
    </beans>
    测试文件

    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Properties;
    import java.util.Set;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class ShowResult {

    public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

    System.out.println("=============about list=======");
    AboutList al = (AboutList)context.getBean("bean2");
    List list = al.getList();
    for(int i=0;i<list.size();i++){
    System.out.println(list.get(i));
    }
    System.out.println("=============about set=======");
    Set s = al.getSet();
    Iterator it = s.iterator();
    while(it.hasNext()){
    System.out.println(it.next());
    }
    System.out.println("=============about map=======");
    Map map = al.getMap();
    Set keys = map.keySet();
    Iterator itr = keys.iterator();
    while(itr.hasNext()){
    String key=(String)itr.next();
    String value=(String)map.get(key);
    System.out.println(key+":"+value);
    }
    System.out.println("=============about Properties=======");
    Properties prop = al.getProperty();
    System.out.println(prop.getProperty("1"));
    }
    }
     
    显示结果为
    =============about list=======
    a
    b
    c
    d
    =============about set=======
    a
    b
    c
    d
    =============about map=======
    1:one
    2:two
    =============about Properties=======
    one
     
  • 相关阅读:
    发现:在Silverlight for windows embedded 当中用户控件状态的改变的问题
    silverlight for windows embedded 当中资源的使用
    WINCE7自带DEMO_SmallMusic_wmpTitleBar_学习笔记
    silverlight for windows embbed 的键盘处理之一
    siverlight for windows embeddem 当中方便查看错误代码的类
    基于siverlight for windows embedded 项目开发的小工具
    对于OPENGL ES SURFACE的学习
    openg es 之一
    理解相机的相关参数的设置
    ListViewWebpart的开发
  • 原文地址:https://www.cnblogs.com/doudingbest/p/4900323.html
Copyright © 2020-2023  润新知