zoukankan      html  css  js  c++  java
  • Spring bean注入

    1.构造方法注入

    1.bean类
    public class User {
        private  String name;
        private Integer age;
        private Cat cat;
        public User(String  name,Integer age,Cat cat){
            this.name=name;
            this.age=age;
            this.cat=cat;
        }
    
        @Override
        public String toString() {
            return "User"+"name"+name+"age"+age+"cat"+cat.getName();
        }
    }
    
    2.测试方法
      public void demo1(){
            ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
            User user=(User)applicationContext.getBean("user");
            System.out.println(user);
        }
    
    3.xml文件配置
       <!--构造方法 注入-->
        <bean id="user" class="com.imooc.ioc.demo4.User">
            <constructor-arg name="name" value="张三"></constructor-arg>
            <constructor-arg name="age" value="13"></constructor-arg>
            <constructor-arg name="cat" ref="cat"></constructor-arg>
        </bean>
    <bean id="cat" class="com.imooc.ioc.demo4.Cat">
    <property name="name" value="cat"></property>
    </bean>

    2.set方法注入

    1.bean类
    
    public class Person {
        private String name;
        private Integer age;
        private Cat cat;
    
        public void setCat(Cat cat) {
            this.cat = cat;
        }
    
        public Cat getCat() {
            return cat;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        @Override
        public String toString() {
            return "name"+name+"age"+age+"cat"+cat.getName();
        }
    }
    
    2.测试方法
     public void demo2(){
            ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
            Person person=(Person)applicationContext.getBean("person1");
            System.out.println(person);
        }
    3.xml文件配置
        <!--set方法注入-->
        <bean id="person1" class="com.imooc.ioc.demo4.Person">
            <property name="age" value="12"></property>
            <property name="name"  value="李四"></property>
            <property name="cat" ref="cat"></property>
        </bean>

    3.复杂类型的注入

    数组  List   set Map  Properties

    package com.imooc.ioc.demo5;
    
    import java.util.*;
    
    /*
    *
    *
    * 复杂类型注入*/
    public class CollectionBean {
        private String[] args;  //数组类型
        private List<String> list;//list集合
        private Set<String> set ;//set集合
        private Map<String,Integer> map;
        private Properties properties;
    
        public List<String> getList() {
            return list;
        }
    
        public Map<String, Integer> getMap() {
            return map;
        }
    
        public Properties getProperties() {
            return properties;
        }
    
        public Set<String> getSet() {
            return set;
        }
    
        public String[] getArgs() {
            return args;
        }
    
        public void setList(List<String> list) {
            this.list = list;
        }
    
        public void setMap(Map<String, Integer> map) {
            this.map = map;
        }
    
        public void setProperties(Properties properties) {
            this.properties = properties;
        }
    
        public void setSet(Set<String> set) {
            this.set = set;
        }
    
        public void setArgs(String[] args) {
            this.args = args;
        }
    
        @Override
        public String toString() {
            return "arrs"+ Arrays.toString(args)+"list"+list+"map"+map+"properties"+properties+"set"+set;
        }
    }


    2.测试方法
    public  void  demo1(){
    ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
    CollectionBean collectionBean=(CollectionBean)applicationContext.getBean("collectionBean");
    System.out.println(collectionBean);
    }
    3.配置文件
    <bean id="collectionBean" class="com.imooc.ioc.demo5.CollectionBean" >
    <!--数组的属性注入-->
    <property name="args">
    <list>
    <value>aaa</value>
    <value>bbb</value>
    <value>ccc</value>
    </list>
    </property>
    <!--list的属性注入-->
    <property name="list">
    <list>
    <value>111</value>
    <value>333</value>
    <value>222</value>
    </list>
    </property>
    <!--set集合的属性注入-->
    <property name="set">
    <set>
    <value>ddd</value>
    <value>eee</value>
    <value>fff</value>
    </set>
    </property>
    <!--map的属性注入-->
    <property name="map">
    <map>
    <entry key="aaa" value="111"></entry>
    <entry key="bbb" value="222"></entry>
    </map>
    </property>
    <!--properties的属性注入-->
    <property name="properties">
    <props>
    <prop key="username">zhangsan</prop>
    </props>
    </property>
    </bean>
     
  • 相关阅读:
    鱼群生长曲线分析
    记住这一刻,学会感恩
    编译boost命令
    ASP之SOAP的发送、接收与处理类[转载]
    在WinForm中使用WebServices来实现软件自动升级(AutoUpdate)(C#)[转载]
    .Net下采用GET/POST/SOAP方式动态调用WebService的简易灵活方法(C#) [轉]Redfox
    使用 HttpWebRequest 类调用 WEB 服务的示例(C#)【转载】
    Log4Net使用指南
    C# post xml using HttpWebRequest/Response
    XmlHttp对象简介[转载]
  • 原文地址:https://www.cnblogs.com/qiujichu/p/10806311.html
Copyright © 2011-2022 走看看