怎么搭建一个简单的springboot
版本1.4.0
第一步新建一个maven工程
修改pom文件
<!--添加父节点--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> </parent>
<!--添加依赖--> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies>
新建一个启动类
@SpringBootApplication public class App { @Bean public Book getBook() { return new Book(); } public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(App.class, args); User user = context.getBean(User.class); System.out.println("user = " + user); Book book = context.getBean(Book.class); System.out.println("book = " + book); } }
这样就完成了
如果你的工程已经有了父节点
那么久使用
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.4.0.RELEASE</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
分析:
@SpringBootApplication注解包含了其他注解
@SpringBootConfiguration @EnableAutoConfiguration @ComponentScan( excludeFilters = {@Filter( type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class} )} )
@SpringBootConfiguration:springboot的配置类注解
@EnableAutoConfiguration:新特性的注解
@ComponentScan:包扫描的注解
SpringApplication.run(App.class, args)会把App.class 包装成一个配置类
启动类的另外一种写法
// public static void main(String[] args) { // SpringApplication application = new SpringApplication(App.class); // ConfigurableApplicationContext context = application.run(args); // // Book book = context.getBean(Book.class); // System.out.println("book = " + book); // // }