使用spring boot能够轻松建立独立的,基于Spring框架的生产级别应用程序。Spring boot应用程序只须要不多的spring配置java
接下来简单介绍如何利用idea新建一个Spring Boot项目web
pom.xml编辑spring
<!-- 将项目打包成jar --> <packaging>jar</packaging>
依赖项shell
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
启动类查看tomcat
@SpringBootApplication public class SpringbootHelloworldApplication { public static void main(String[] args) { SpringApplication.run(SpringbootHelloworldApplication.class, args); } }
@SpringBootApplication=(默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan。app
一、@Configuration:提到@Configuration就要提到他的搭档@Bean。使用这两个注解就能够建立一个简单的spring配置类,能够用来替代相应的xml配置文件。框架
<beans> <bean id = "car" class="com.test.Car"> <property name="wheel" ref = "wheel"></property> </bean> <bean id = "wheel" class="com.test.Wheel"></bean> </beans>
至关于maven
@Configuration public class Conf { @Bean public Car car() { Car car = new Car(); car.setWheel(wheel()); return car; } @Bean public Wheel wheel() { return new Wheel(); } }
@Configuration的注解类标识这个类可使用Spring IoC容器做为bean定义的来源。@Bean注解告诉Spring,一个带有@Bean的注解方法将返回一个对象,该对象应该被注册为在Spring应用程序上下文中的bean。ide
二、@EnableAutoConfiguration:可以自动配置spring的上下文,试图猜想和配置你想要的bean类,一般会自动根据你的类路径和你的bean定义自动配置。spring-boot
三、 @ComponentScan:会自动扫描指定包下的所有标有@Component的类,并注册成bean,固然包括@Component下的子注解@Service,@Repository,@Controller。(注:默认只能扫描当前包及其子包下的Bean)
控制器编辑
新建一个HelloWorldController
@RestController public class HelloWorldController { @RequestMapping("hello") String hello(){ return "hello world"; } }
打包
mvn clean package
启动
## 后台运行 nohup java -jar **.jar & ## maven方式运行 mvn spring-boot:run
访问