我的第一个SpringBoot项目 Component Autowired
SpringBoot是做微服务架构非常好的一套框架,下面来讲讲我的第一个SpringBoot项目;1、依赖1.3.2.RELEASEorg.springframework.bootspring-boot-starter-web${spring.boot.ve
·
SpringBoot是做微服务架构非常好的一套框架,下面来讲讲我的第一个SpringBoot项目;
1、依赖
<properties>
<spring.boot.version>1.3.2.RELEASE</spring.boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
2、Application类,这是SpringBoot启动服务的类;
package simple;
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;
import simple.service.ExampleOneService;
/**
* Created by LK on 2016/4/24.
*/
@SpringBootApplication
public class SimpleApplication implements CommandLineRunner{
@Autowired
@Qualifier("exampleOneService")
private ExampleOneService exampleOneService;
public void run(String... strings) throws Exception {
System.out.println(this.exampleOneService.getExampleOneMessage());
if(strings.length > 0 && strings[0].equals("exitcode")){
throw new ExitException();
}
}
public static void main(String[] args) {
SpringApplication.run(SimpleApplication.class,args);
}
}
3、由于上面需要引用服务才可以获得内容,所以我们也得创建一个服务类;
package simple.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* Created by LK on 2016/4/24.
*/
@Component
public class ExampleOneService {
@Value("${name:我的第一个SpringBoot}") //name:后面的值可以在resources里面的application.properties中配置,如果不配置,那么久默认为程序里面的值
private String name;
public String getExampleOneMessage(){
return "看," + name;
}
}
4、上面说application.properties,那么我们还得创建一个这样子的文件,并配置好需要的信息;
name:我的第一个SpringBoot
5、去到
SimpleApplication 类中,右键运行main运行项目,又或者Teminal控制台执行mvn spring-boot:run可以运行项目
</pre><pre code_snippet_id="1660186" snippet_file_name="blog_20160424_7_6717521" name="code" class="java">
更多推荐
已为社区贡献5条内容
所有评论(0)