zoukankan      html  css  js  c++  java
  • spring_xml配置&依赖注入

    factory源码思路

    package com.zl.factory;
     
    import java.io.InputStream;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Properties;
    import java.util.Set;
     
    public class BeanFactory {
    //BeanFactory工厂,读取配置文件bean.properties,通过传入的key=id键得到值,通过反射创建对象返回
    private static Properties p = new Properties();
     
    private static Map<String,Object> beans = new HashMap<String,Object>(); //map ioc容器
     
    static {
     
    try {
    InputStream is = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
    p.load(is);
     
    Set<Object> keySet = p.keySet();
    for (Object key : keySet) {
    String k = key.toString();
    String classpath = p.get(k).toString();
    Object o = Class.forName(classpath).newInstance();
    beans.put(k, o);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
     
    public static Object getBean(String key) {
    try {
    //String classpath = p.get(key).toString();
    //Object o = Class.forName(classpath).newInstance();
     
    Object o = beans.get(key);
    return o;
    } catch (Exception e) {
    e.printStackTrace();
    }
    return null;
    }
    }
     

    配置文件及依赖注入

    id:给对象在容器中提供一个唯一标识。用于获取对象。
    class:指定类的全限定类名。用于反射创建对象。默认情况下调用无参构造函数
    <?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 class="com.zl.service.impl.AccountServiceImpl" id="accountService"></bean>-->
    <!--<bean class="com.zl.dao.impl.AccountDaoImpl" id="accountDao"></bean>-->
    <!--<bean class="com.zl.service.impl.AccountServiceImpl" id="accountService" scope="singleton"></bean>-->
    <!--<bean class="com.zl.service.impl.AccountServiceImpl"-->
    <!--id="accountService" scope="singleton" init-method="init" destroy-method="destroy"></bean>-->
     
    <!--<bean class="com.zl.factory.StaticFactory" factory-method="createObj" id="accountService"></bean>-->
    <!--<bean class="com.zl.factory.NoStaticFactory" id="factory"/>-->
    <!--<bean factory-bean="factory" factory-method="createObj" id="accountService"/>-->
     
    <!--set方法注入-->
    <!--property属性就是通过set方法赋值,name是属性名字,value是给基本数据类型,字符串,包装类赋值-->
    <!--<bean class="java.util.Date" id="date"></bean>-->
    <!--<bean class="com.zl.service.impl.AccountServiceImpl" id="accountService">-->
    <!--<property name="name" value="fbb"/>-->
    <!--<property name="age" value="18"/>-->
    <!--<property name="birthday" ref="date"/>-->
    <!--</bean>-->
    <!--constructor-arg
    属性:
    index:指定参数在构造函数参数列表的索引位置
    type:指定参数在构造函数中的数据类型
    name:指定参数在构造函数中的名称 ,用这个找给谁赋值
    =======上面三个都是找给谁赋值,下面两个指的是赋什么值的==============
    value:它能赋的值是基本数据类型和String类型
    ref:它能赋的值是其他bean类型,也就是说,必须得是在配置文件中配置过的bean-->
    <!--构造参数注入-->
    <!--<bean class="java.util.Date" id="date"></bean>-->
    <!--<bean class="com.zl.service.impl.AccountServiceImpl" id="accountService">-->
    <!--name方式-->
    <!--<constructor-arg name="name" value="fbb"></constructor-arg>-->
    <!--<constructor-arg name="age" value="18"></constructor-arg>-->
    <!--<constructor-arg name="birthday" ref="date"></constructor-arg>-->
    <!--index方式-->
    <!--<constructor-arg index="0" value="fbb"></constructor-arg>-->
    <!--<constructor-arg index="1" value="18"></constructor-arg>-->
    <!--<constructor-arg index="2" ref="date"></constructor-arg>-->
    <!--type方式-->
    <!--<constructor-arg type="java.lang.String" value="fbb"></constructor-arg>-->
    <!--<constructor-arg type="java.lang.Integer" value="18"></constructor-arg>-->
    <!--<constructor-arg type="java.util.Date" ref="date"></constructor-arg>-->
    <!--这三种方式可以同时用,注意index位置。一般用name方法-->
    <!--</bean>-->
    <!--set方法比构造参数常用-->
     
    <!--数组,键值对-->
    <bean class="com.zl.service.impl.AccountServiceImpl" id="accountService">
    <property name="myStrs">
    <array>
    <value>fbb</value>
    <value>lbb</value>
    </array>
    </property>
    <property name="myList">
    <list>
    <value>fbb</value>
    <value>"lbb"</value>
    </list>
    </property>
    <property name="mySet">
    <set>
    <value>fbb</value>
    <value>"lbb"</value>
    </set>
    </property>
    <!--数组集合都可以用array标签-->
     
    <property name="myMap">
    <map>
    <entry key="fbb" value="123">
    </entry>
    <entry key="lbb">
    <value>1234</value>
    </entry>
    </map>
    </property>
    <property name="myProps">
    <map>
    <entry value="666" key="fbb"/>
    <entry key="bb">
    <value>888</value>
    </entry>
    </map>
    </property>
    </bean>
    </beans>
     
    value:它能赋的值是基本数据类型和String类型
    idea 空格提示属性,尖括号提示标签
     
  • 相关阅读:
    Shell脚本
    StateListDrawable、ColorStateList
    android 继承viewgroup的类不调用onDraw
    默默的向3位大牛学习.....
    总结的一些android公共库,包含公共的View、缓存以及一些工具类
    android屏幕 单位转换
    android 实现自动滚动的 Banner 横幅
    C# 求最大相同子字符串
    C# 把数字转换成链表
    C# 求奇数和偶数和
  • 原文地址:https://www.cnblogs.com/21556guo/p/13695233.html
Copyright © 2011-2022 走看看