Skip to content

Commit

Permalink
Spring Boot 之配置注解使用
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffLi1993 authored and liqiangqiang committed Sep 8, 2017
1 parent e688ed0 commit cf05b3b
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
32 changes: 32 additions & 0 deletions springboot-configuration/pom.xml
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>springboot</groupId>
<artifactId>springboot-configuration</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-configuration :: 配置 Demo</name>

<!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>

<dependencies>
<!-- Spring Boot web依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,19 @@
package org.spring.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* Spring Boot 应用启动类
*
*/
// Spring Boot 应用的标识
@SpringBootApplication
public class Application {

public static void main(String[] args) {
// 程序启动入口
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
SpringApplication.run(Application.class,args);
}
}
@@ -0,0 +1,16 @@
package org.spring.springboot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* Created by bysocket on 08/09/2017.
*/
@Configuration
public class MessageConfiguration {

@Bean
public String message() {
return "message configuration";
}
}
@@ -0,0 +1,27 @@
package org.spring.springboot.config;

import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import static org.junit.Assert.assertEquals;

/**
* Spring Boot MessageConfiguration 测试 - {@link MessageConfiguration}
*
*/
public class MessageConfigurationTest {

@Test
public void testGetMessageBean() throws Exception {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MessageConfiguration.class);
assertEquals("message configuration", ctx.getBean("message"));
}

@Test
public void testScanPackages() throws Exception {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.scan("org.spring.springboot");
ctx.refresh();
assertEquals("message configuration", ctx.getBean("message"));
}
}

0 comments on commit cf05b3b

Please sign in to comment.