zoukankan      html  css  js  c++  java
  • Spring JavaConfig实例

    从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中。
    但是,仍然允许使用经典的XML方式来定义bean和配置,JavaConfig是另一种替代解决方案。
    看来看经典的XML定义和JavaConfig的不同,如下定义在Spring容器中的bean。

    Spring XML file - applicationContext.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-3.0.xsd">
     
    	<bean id="helloBean" class="com.yiibai.hello.impl.HelloWorldImpl">
    		
    </beans>
    等效于以下JavaConfig的配置:
    package com.yiibai.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import com.yiibai.hello.HelloWorld;
    import com.yiibai.hello.impl.HelloWorldImpl;
    
    @Configuration
    public class AppConfig {
    	
        @Bean(name="helloBean")
        public HelloWorld helloWorld() {
            return new HelloWorldImpl();
        }
    	
    }

    Spring JavaConfig Hello World

    现在,看到一个完整的Spring JavaConfig例子。

    1. 工程目录结构

    这个例子的目录结构如下。

    3. Spring Bean

    一个简单的Bean

    package com.yiibai.hello;
     
    public interface HelloWorld {
    	
    	void printHelloWorld(String msg);
     
    }
    package com.yiibai.hello.impl;
    
    import com.yiibai.hello.HelloWorld;
    
    public class HelloWorldImpl implements HelloWorld {
    
    	@Override
    	public void printHelloWorld(String msg) {
    
    		System.out.println("Hello : " + msg);
    	}
    
    }

    4. JavaConfig 注解

    使用 @Configuration 注释告诉 Spring,这是核心的 Spring 配置文件,并通过 @Bean 定义 bean。
    package com.yiibai.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import com.yiibai.hello.HelloWorld;
    import com.yiibai.hello.impl.HelloWorldImpl;
    
    @Configuration
    public class AppConfig {
    	
        @Bean(name="helloBean")
        public HelloWorld helloWorld() {
            return new HelloWorldImpl();
        }
    	
    }

    5. 执行结果

    使用 AnnotationConfigApplicationContext 加载您的JavaConfig类

    package com.yiibai.core;
     
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import com.yiibai.config.AppConfig;
    import com.yiibai.hello.HelloWorld;
     
    public class App {
    	public static void main(String[] args) {
    	    
                ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
    	    HelloWorld obj = (HelloWorld) context.getBean("helloBean");
    	    
    	    obj.printHelloWorld("Spring Java Config");
    
    	}
    }

    输出结果

    Hello : Spring Java Config
     
    http://www.yiibai.com/spring/spring-3-javaconfig-example.html
  • 相关阅读:
    夺命雷公狗---linux NO:8 linux的通配符和ll以及ls的使用方法
    夺命雷公狗---linux NO:7 linux命令基本格式
    夺命雷公狗---linux NO:6 linux远程登录和关机和重启
    夺命雷公狗---解决网络和共享中心打不开的问题
    夺命雷公狗---linux NO:5 linux系统登录和注销
    夺命雷公狗---linux NO:4 linux系统运行级别
    利用win7系统自带服务在内网搭建ftp服务器
    2017-05-31--夺命雷公狗发牢骚
    夺命雷公狗C/C++-----9---自定义一个函数测试简单的运算
    夺命雷公狗C/C++-----8---使用ShellExecute打开一个文件和一个网址和打印文件
  • 原文地址:https://www.cnblogs.com/soundcode/p/6367003.html
Copyright © 2011-2022 走看看