Spring boot Hello world 独立应用程序示例
在本教程中,我们将创建Spring Boot Hello World Standalone应用程序。
我们已经创建了Spring Boot Web Hello World示例。
使用Spring Boot配置Spring Standalone项目非常容易。
项目结构:
第1步:使用Maven在Eclipse中创建一个简单的Java项目名为"SpringboothellyorldalloneAneExample"。
第2步:更改"pom.xml"如下:
<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>org.igi.theitroad</groupId> <artifactId>SpringHelloWorldStandaloneExample</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> </project>
第3步:在package .org.igi.theitroad.springboot中创建名为"country.java"的文件
package org.igi.theitroad.springboot;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Country {
@Value("{countryName}")
private String countryName;
public Country()
{
}
public Country(String countryName) {
super();
this.countryName = countryName;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
}
如果我们注意到,我们正在使用@Value("{countryname}")for countryname属性。
此语法用于从属性文件中读取字符串。
在下一步中,我们将在应用程序中将"CountryName = Netherlands"。
。
特性。
我们已声明使用@Component注释,因此Spring Boot将使用组件扫描自动查找此bean。
第4步:在Package SRC/Main/Resources中创建名为"application.properties"的文件
countryName=Netherlands
第5步:在Package .org.igi.theitroad中创建名为"springboothellyorldallonaloneApplication.java"的文件
package org.igi.theitroad;
import org.igi.theitroad.springboot.Country;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootHelloWorldStandaloneApplication implements CommandLineRunner {
@Autowired
@Qualifier("country")
Country countryBean;
public static void main(String[] args)
{
SpringApplication.run(SpringBootHelloWorldStandaloneApplication.class, args);
}
public void run(String... arg0) throws Exception {
System.out.println("Country Name: "+countryBean.getCountryName());
}
}
我们可以了解更多关于@autowire和@qualifier的信息。
SpringBoothellDordallateLoneApplication的主要方法使用Spring Boot的pringapplication.run()方法来启动Spring 应用程序。
如果我们注意,我们在此处没有执行任何单行XML配置。
我们在此实现了CommandLinerUnner接口。
此接口表示Bean应该在Spring应用程序中包含。
运行上面的应用程序时,我们将得到以下输出。
2016-05-06 00:12:49.911 INFO 15824 — [ main] pringBootHelloWorldStandaloneApplication : Starting SpringBootHelloWorldStandaloneApplication on apples-MacBook-Air.local with PID 15824 (/Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldStandaloneExample/target/classes started by apple in /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldStandaloneExample) 2016-05-06 00:12:49.914 INFO 15824 — [ main] pringBootHelloWorldStandaloneApplication : No active profile set, falling back to default profiles: default 2016-05-06 00:12:50.012 INFO 15824 — [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5579bb86: startup date [Sat Jan 06 00:12:50 IST 2016]; root of context hierarchy 2016-05-06 00:12:50.817 INFO 15824 — [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup Country Name: Netherlands 2016-05-06 00:12:50.838 INFO 15824 — [ main] pringBootHelloWorldStandaloneApplication : Started SpringBootHelloWorldStandaloneApplication in 1.421 seconds (JVM running for 2.006) 2016-05-06 00:12:50.839 INFO 15824 — [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@5579bb86: startup date [Sat Jan 06 00:12:50 IST 2016]; root of context hierarchy 2016-05-06 00:12:50.841 INFO 15824 — [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown

