【Spring实战】—— 2 构造注入

本文讲解了构造注入以及spring的基本使用方式,通过一个杂技演员的例子,讲述了依赖注入属性或者对象的使用方法。

  如果想要使用spring来实现依赖注入,需要几个重要的步骤:

  1 定义主要的类和需要分离的属性。这里主要的类,是指程序的主要对象,在例子中是Juggler杂技员。而想要分离构造的属性,是它手中的袋子的数目beanBags。

  2 配置bean.xml。通过配置文件,确定主要的类和属性之间的关系,以及实现类。

  3 通过应用上下文,获取bean,并进行使用。

注入属性

  实例代码:

  1 表演者接口:Performer.java

package com.spring.test.action1;

public interface Performer {
    void perform() throws PerformanceException;
}

  2 杂技员:Juggler,继承了表演者接口

复制代码
package com.spring.test.action1;

public class Juggler implements Performer{
    private int beanBags = 3;
    
    public Juggler(){
        
    }
    
    public Juggler(int beanBags){
        this.beanBags = beanBags;
    }
    
    public void perform() throws PerformanceException {
        System.out.println("Juggler "+beanBags+" beanBags");
    }

}
复制代码

  3 Spring配置文件:bean.xml

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
     <bean id="duke" class="com.spring.test.action1.Juggler">
         <constructor-arg value="15"/>
     </bean>
</beans>
复制代码

  4 应用上下文获取指定的bean

复制代码
package com.spring.test.action1;

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

public class test {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
        Performer performer = (Performer)ctx.getBean("duke");
        try {
            performer.perform();
        } catch (PerformanceException e) {
            e.printStackTrace();
        }
    }
}
复制代码

  执行结果如下:

Juggler 15 beanBags

 

注入对象

  1 例如,上面的杂技演员不仅仅会仍袋子,还会表演诗歌,那么诗歌这个对象就需要注入到表演者的构造函数中,可以如下表示会朗诵诗歌的杂技演员:PoeticJuggler

复制代码
package com.spring.test.action1;

public class PoeticJuggler extends Juggler{
    private Poem poem;
    
    public PoeticJuggler(Poem poem){
        super();
        this.poem = poem;
    }
    
    public PoeticJuggler(int beanBags,Poem poem){
        super(beanBags);
        this.poem = poem;
    }
    
    public void perform() throws PerformanceException {
        super.perform();
        System.out.println("While reciting...");
        poem.recite();
    }

}
复制代码

  2 诗歌对象:Sonnet29

复制代码
package com.spring.test.action1;

public class Sonnet29 implements Poem{
    private static String lines = "嘛咪嘛咪哄";
    
    public Sonnet29() {
        
    }
    
    public void recite() {
        System.out.println(lines);
    }

}
复制代码

  3 Bean.xml配置文件如下

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
     <bean id="duke" class="com.spring.test.action1.Juggler">
         <constructor-arg value="15"/>
     </bean>
     <bean id="sonnet29" class="com.spring.test.action1.Sonnet29"/>
     <bean id="poeticDuke" class="com.spring.test.action1.PoeticJuggler">
         <constructor-arg value="15"/>
         <constructor-arg ref="sonnet29"/>
     </bean>
</beans>
复制代码

  4 主要的执行代码,通过应用上下文获取制定的bean

复制代码
package com.spring.test.action1;

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

public class test {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
//        Performer performer = (Performer)ctx.getBean("duke");
        Performer performer1 = (Performer)ctx.getBean("poeticDuke");
        try {
//            performer.perform();
            performer1.perform();
        } catch (PerformanceException e) {
            e.printStackTrace();
        }
    }
}
复制代码

  5 执行结果如下

复制代码
一月 24, 2015 4:40:46 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@35bf8de1: startup date [Sat Jan 24 16:40:46 CST 2015]; root of context hierarchy
一月 24, 2015 4:40:46 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean.xml]
一月 24, 2015 4:40:46 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3401a0ad: defining beans [duke,sonnet29,poeticDuke]; root of factory hierarchy
Juggler 15 beanBags
While reciting...
嘛咪嘛咪哄
复制代码

 

posted @   xingoo  阅读(4733)  评论(8编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2014-01-24 守护进程和inetd超级服务器
点击右上角即可分享
微信分享提示