mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-13 21:43:45 +08:00
Spring Boot 之配置注解使用
This commit is contained in:
32
springboot-configuration/pom.xml
Executable file
32
springboot-configuration/pom.xml
Executable file
@@ -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"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user