资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

如何在Spring中操作Bean类

本篇文章给大家分享的是有关如何在Spring中操作Bean类,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创新互联是一家专注于成都网站设计、网站建设与策划设计,岱岳网站建设哪家好?创新互联做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:岱岳等地区。岱岳做网站价格咨询:028-86922220

一 配置文件



   
   

二 接口

Axe

package org.crazyit.app.service;
public interface Axe
{
   public String chop();
}

Person

package org.crazyit.app.service;
public interface Person
{
   public void useAxe();
}

三 Bean

Chinese

package org.crazyit.app.service.impl;
import org.springframework.stereotype.*;
import org.crazyit.app.service.*;
@Component
public class Chinese
  implements Person
{
  private Axe axe;
  // axe的setter方法
  public void setAxe(Axe axe)
  {
    this.axe = axe;
  }
  // 实现Person接口的useAxe()方法
  public void useAxe()
  {
    System.out.println(axe.chop());
  }
}

SteelAxe

package org.crazyit.app.service.impl;
import org.springframework.stereotype.*;
import org.crazyit.app.service.*;
@Component
public class SteelAxe implements Axe
{
  public String chop()
  {
    return "钢斧砍柴真快";
  }
}

StoneAxe

package org.crazyit.app.service.impl;
import org.springframework.stereotype.*;
import org.crazyit.app.service.*;
@Component
public class StoneAxe implements Axe
{
  public String chop()
  {
    return "石斧砍柴好慢";
  }
}

四 测试类

package lee;
import org.springframework.context.*;
import org.springframework.context.support.*;
public class BeanTest
{
  public static void main(String[] args)
  {
    // 创建Spring容器
    ApplicationContext ctx = new
      ClassPathXmlApplicationContext("beans.xml");
    // 获取Spring容器中的所有Bean实例的名
    System.out.println("--------------" +
      java.util.Arrays.toString(ctx.getBeanDefinitionNames()));
  }
}

五 测试结果

--------------[chinese, steelAxe, stoneAxe,  org.springframework.context.annotation.internalConfigurationAnnotationProcessor,  org.springframework.context.annotation.internalAutowiredAnnotationProcessor,  org.springframework.context.annotation.internalRequiredAnnotationProcessor,  org.springframework.context.annotation.internalCommonAnnotationProcessor,  org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,  org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor]

以上就是如何在Spring中操作Bean类,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


分享文章:如何在Spring中操作Bean类
本文地址:http://cdkjz.cn/article/ghhood.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220