zoukankan      html  css  js  c++  java
  • Spring注入方式

    1:设值注入

      spring容器使用属性的setter方法来注入被依赖的实例(使用最多)

      Person接口

    package com.lee.bean;
    
    public interface Person {
    
        public void eat();
    
    }

      Fruit接口

    package com.lee.bean;
    
    public interface Fruit {
    
        public void description();
    
    }

      Person的实现类

    package com.lee.bean.impl;
    
    import com.lee.bean.Fruit;
    import com.lee.bean.Person;
    
    public class XiaoMing implements Person {
    
        private Fruit fruit;
    
        public void eat() {
    
            fruit.description();
            System.out.println("yummy!");
        }
    
        public Fruit getFruit() {
            return fruit;
        }
    
        public void setFruit(Fruit fruit) {
            this.fruit = fruit;
        }
    
    }

      Fruit的实现类

    package com.lee.bean.impl;
    
    import com.lee.bean.Fruit;
    
    public class Apple implements Fruit {
    
        public void description() {
    
            System.out.print("this is an apple! ");
    
        }
    
    }

      spring的配置文件bean.xml(classpath下)

    <?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="xiaoMing" class="com.lee.bean.impl.XiaoMing">
            <property name="fruit" ref="apple"></property>
        </bean>
    
        <bean id="apple" class="com.lee.bean.impl.Apple"></bean>
    
    </beans>

    测试类

    package com.lee.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.lee.bean.impl.XiaoMing;
    
    public class BeanTest {
    
        public static void main(String[] args) {
    
            ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
            XiaoMing xiaoMing = ac.getBean("xiaoMing", XiaoMing.class);
    
            xiaoMing.eat();
    
        }
    
    }

      运行结果,在控制台打印出

     

    this is an apple! yummy!

      2:构造注入

      修改XiaoMing.java

    package com.lee.bean.impl;
    
    import com.lee.bean.Fruit;
    import com.lee.bean.Person;
    
    public class XiaoMing implements Person {
    
        private Fruit fruit;
    
        public void eat() {
    
            fruit.description();
            System.out.println("yummy!");
        }
    
        public XiaoMing(Fruit fruit) {
            this.fruit = fruit;
        }
    
    }

      修改bean.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="xiaoMing" class="com.lee.bean.impl.XiaoMing">
            <constructor-arg ref="apple" />
        </bean>
    
        <bean id="apple" class="com.lee.bean.impl.Apple"></bean>
    
    </beans>
  • 相关阅读:
    下载文件
    利用 js 获取地址栏参数
    子组件向父组件传值
    cordova 插件 调用iOS社交化分享(ShareSDK:微信QQ分享)
    cordova 企业应用打包Archive的时候报 "#import <Cordova file not found"
    企业应用打包的时候 修改ipa包的bundle identifier
    Mac下利用safari调试 Cordova的WebApp
    Mac下利用Cordova打包 iOS App以及出现的问题
    数据库设计流程
    Javascript 获取dom的宽度 随笔一
  • 原文地址:https://www.cnblogs.com/harryV/p/3728060.html
Copyright © 2011-2022 走看看