前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring的JavaConfig

Spring的JavaConfig

作者头像
冯杰宁
发布2019-06-11 10:51:49
4030
发布2019-06-11 10:51:49
举报
文章被收录于专栏:全栈技术Kojo全栈技术Kojo

之前我们都是在xml文件中定义bean的,比如:

    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-3.0.xsd">

其实我们可以使用注解来完成这些事情,例如下面的代码,完成的功能和上面的xml配置的功能是一样的:

importorg.springframework.context.annotation.Bean;

importorg.springframework.context.annotation.Configuration;

importcom.mkyong.hello.HelloWorld;

importcom.mkyong.hello.impl.HelloWorldImpl;

@Configuration

publicclassAppConfig {

    @Bean(name="helloBean")

    publicHelloWorld helloWorld() {

        returnnewHelloWorldImpl();

    }

}

 想象一个场景,我们有一个很大的工程项目,如果将所有的bean都配置在一个xml文件中,那么这个文件就会非常的大。所以在很多的时候我们都会将一个大的xml配置文件分割为好几份。这样方便管理,最后在总的那个xml文件中导入就行了,比如:

 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-2.5.xsd">

 但是现在我们也可以使用JavaConfig来完成同样的工作了:

importorg.springframework.context.annotation.Configuration;

importorg.springframework.context.annotation.Import;

@Configuration

@Import({ CustomerConfig.class, SchedulerConfig.class})

publicclassAppConfig {

}

  我们对这个例子来看一个demo:

CustomerBo.java

publicclassCustomerBo {

    publicvoidprintMsg(String msg) {

        System.out.println("CustomerBo : "+ msg);

    }

}

SchedulerBo.java

publicclassSchedulerBo {

    publicvoidprintMsg(String msg) {

        System.out.println("SchedulerBo : "+ msg);

    }

}

现在我们来使用注解:

@Configuration

publicclassCustomerConfig {

    @Bean(name="customer")

    publicCustomerBo customerBo(){

        returnnewCustomerBo();

    }

}

@Configuration

publicclassSchedulerConfig {

    @Bean(name="scheduler")

    publicSchedulerBo suchedulerBo(){

        returnnewSchedulerBo();

    }

}

AppConfig.java

importorg.springframework.context.annotation.Configuration;

importorg.springframework.context.annotation.Import;

@Configuration

@Import({ CustomerConfig.class, SchedulerConfig.class})

publicclassAppConfig {

}

  然后运行:

publicclassApp {

    publicstaticvoidmain(String[] args) {

        ApplicationContext context = newAnnotationConfigApplicationContext(

                AppConfig.class);

        CustomerBo customer = (CustomerBo) context.getBean("customer");

        customer.printMsg("Hello 1");

        SchedulerBo scheduler = (SchedulerBo) context.getBean("scheduler");

        scheduler.printMsg("Hello 2");

    }

}

==============================================================================

本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/12/27/2835087.html,如需转载请自行联系原作者

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.01.10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档