【Java】使用Spring的JavaConfig

栏目: Java · 发布时间: 8年前

内容简介:【Java】使用Spring的JavaConfig
<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-3.0.xsd">


  

    <bean id="helloBean" class="com.mkyong.hello.impl.HelloWorldImpl">


  

</beans>

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

import org.springframework.context.annotation.Bean;


import org.springframework.context.annotation.Configuration;


import com.mkyong.hello.HelloWorld;


import com.mkyong.hello.impl.HelloWorldImpl;


  
@Configuration

public class AppConfig {


  

    @Bean(name="helloBean")


    public HelloWorld helloWorld() {


        return new HelloWorldImpl();


    }


  
}

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

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


  

    <import resource="config/customer.xml"/>


        <import resource="config/scheduler.xml"/>


  

</beans>

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

import org.springframework.context.annotation.Configuration;


import org.springframework.context.annotation.Import;


  
@Configuration

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


public class AppConfig {


  
}

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

CustomerBo.java

public class CustomerBo {


  

    public void printMsg(String msg) {


  

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


    }


  
}

SchedulerBo.java

public class SchedulerBo {


  

    public void printMsg(String msg) {


  

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


    }


  
}

现在我们来使用注解:

@Configuration

public class CustomerConfig {


  

    @Bean(name="customer")


    public CustomerBo customerBo(){


  

        return new CustomerBo();


  

    }

}
@Configuration

public class SchedulerConfig {


  

    @Bean(name="scheduler")


    public SchedulerBo suchedulerBo(){


  

        return new SchedulerBo();


  

    }


  
}

AppConfig.java

import org.springframework.context.annotation.Configuration;


import org.springframework.context.annotation.Import;


  
@Configuration

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


public class AppConfig {


  
}

然后运行:

public class App {


    public static void main(String[] args) {


  

        ApplicationContext context = new AnnotationConfigApplicationContext(


                AppConfig.class);


  

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


        customer.printMsg("Hello 1");


  

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


        scheduler.printMsg("Hello 2");


  

    }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

虚拟化与云计算

虚拟化与云计算

《虚拟化与云计算》小组 / 电子工业出版社 / 2009-10 / 45.00元

本书系统阐述了当今信息产业界最受关注的两项新技术——虚拟化与云计算。云计算的目标是将各种IT资源以服务的方式通过互联网交付给用户。计算资源、存储资源、软件开发、系统测试、系统维护和各种丰富的应用服务,都将像水和电一样方便地被使用,并可按量计费。虚拟化实现了IT资源的逻辑抽象和统一表示,在大规模数据中心管理和解决方案交付方面发挥着巨大的作用,是支撑云计算伟大构想的最重要的技术基石。本书以在数据中心采......一起来看看 《虚拟化与云计算》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

随机密码生成器
随机密码生成器

多种字符组合密码

URL 编码/解码
URL 编码/解码

URL 编码/解码