spring-IOC注入

**spring-IOC注入 ** xml格式 简答的构造注入 先在src目录下建立一个xml文件 , 最好取名为applicationContext.xml ,而后在xml文件中复制spring配置文件所须要的约束spring

<?xml version="1.0" encoding="UTF-8"?>app

<beans xmlns="http://www.springframework.org/schema/beans"code

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xml

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>对象

而后把你写程序过程当中所须要调用的类 ,而后在里面声明类的bean , 声明就是把类的路径 , id,name等信息告诉spring容器blog

<bean class="com.sxt.pojo.Cat" id="cat"></bean>

而后初始化容器get

public static void main(String[] args) {it

//初始化Spring容器,当Spring容器初始化时,会自动加载配置文件,而后根据配置文件中的内容初始化Beanio

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");class

//经过getBean(在配置文件中设置的id或者name)方法获取到配置文件里所需类

Cat cat = ac.getBean("cat");

System.out.println(cat);

}

这样就获取到了cat类

上面时直接获取到类对象 , 那么怎么在经过spring容器获取对象的时候同时给要获取到的属性赋值呢

能够经过构造注入

构造注入时 ,注入几个属性 ,那么该类就应该有所对应的构造方法 ,提供有参构造方法 , 同时必须提供无参构造的

<bean class="com.sxt.pojo.Cat" id="cat1">

//构造注入经过bean里的constructor-arg来注入
         <constructor-arg  name="id" value="1" />
         <constructor-arg name="name" value="安安" />
     </bean>

1)构造注入能够再构造器中决定依赖关系的注入顺序,有限依赖的优先注入。例如,组件中其它依赖关系的注入,经常须要依赖于Datasource的注入。采用构造注入,能够在代码中清晰地决定注入顺序。 2)对于依赖关系无需变化的Bean,构造注入更加有用。由于没有setter方法,全部的依赖关系所有在构造器内设定。所以,无需担忧后续代码对依赖关系的破坏。 3)依赖关系只能在构造器中设定,则只有组建的建立者才能改变组建的依赖关系。队组建的调用者而言,组件内部的依赖关系彻底透明,更符合高内聚的原则。

设置注入

须要类有无惨构造方法 , 和构造注入不一样 , 设置注入能够任意的为任意的属性设置 ,可是必需要有setter方法

<bean class="com.sxt.pojo.Cat" id="cat1">

//property是设置注入的关键字
         <property name="id" value="1" />
         <property name="name" value="安安" />
     </bean>

静态工厂注入

public class StaticFactory {
    
    public static User getUser(){
        return new User();
    }
}

<bean class="com.staticFactory.StaticFactory" factory-method="getUser" id ="s" />

动态工厂注入

public class DynamicFactory {
    public  User getUser(String name){
        return new User(name);
    }

}

<bean class="com.dynamicFactory.DynamicFactory"  id ="d" />
 <bean factory-bean="d" factory-method="getUser"   id="dm"></bean>

还有对象注入 ,集合注入 ,map注入等

<bean class="com.sxt.pojo.Cat" id="cat1">
         <property name="id" value="1" />
         <property name="name" value="安安" />
     </bean>
     
     <bean class="com.sxt.pojo.User" id="user">
         <property name="all">
             <array> 
                 <value>1</value>
                 <value>8</value>
                 <value>23</value>
             </array>
         </property>
         
             <property name="cat">
             <array>
                 <ref bean="cat1"/>
                 <bean class="com.sxt.pojo.Cat">
                     <property name="id" value="2" />
                     <property name="name" value="wangwu" />
                 </bean>
             </array>
         </property>
         
         <property name="list">
             <list>
                 <ref bean="cat1"/>
                 <bean class="com.sxt.pojo.Cat">
                     <property name="id" value="3" />
                     <property name="name" value="list1" />
                 </bean>
                 <bean class="com.sxt.pojo.Cat">
                     <property name="id" value="4" />
                     <property name="name" value="list2" />
                 </bean>
             </list>
         </property>
         
         <property name="map">
             <map>
                 <entry key="key1" value="value1" />
                 <entry key="key2" value="value2" />
             </map>
         </property>
     </bean>

@org.junit.Test     public void test() {      ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");      User user =  ac.getBean("user",User.class);      int[] all = user.getAll();      for (int i : all) {          System.out.println(i);     }                Cat[] cats =  user.getCat();     for (Cat cat : cats) {         System.out.println(cat);     }          List<Cat> list = user.getList();     for (Cat cat : list) {         System.out.println(cat);     }               Map<String, String> map = user.getMap();     Set<String> set = map.keySet();     for (String s : set) {         System.out.println(s+":"+map.get(s));     }          }

相关文章
相关标签/搜索