创建简单spring boot项目
2024-08-31 02:24:10
## 简介
使用spring boot可以轻松创建独立的,基于Spring框架的生产级别应用程序。Spring boot应用程序只需要很少的spring配置
特点
- 创建独立的Spring应用程序
- 直接嵌入tomcat
- 提供starter依赖项,简化构建配置
- 尽可能自动配置Spring和三方库
- 提供生产就绪的功能,例如指标,运行状况检查和外部配置
- 完全没有代码生成,也不需要XML配置
启程
接下来简单介绍如何利用idea新建一个Spring Boot项目
- 新建项目
- 输入项目名和包名
pom.xml编辑
<!-- 将项目打包成jar -->
<packaging>jar</packaging>
依赖项
<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>
启动类查看
@SpringBootApplication
public class SpringbootHelloworldApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootHelloworldApplication.class, args);
}
}
@SpringBootApplication=(默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan。
1、@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>
相当于
@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。
2、@EnableAutoConfiguration:能够自动配置spring的上下文,试图猜测和配置你想要的bean类,通常会自动根据你的类路径和你的bean定义自动配置。
3、 @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
访问
最新文章
- ASP.NET从MVC5升级到MVC6 RC2 总目录 - 发布在RC2Release之后
- spring 4.x下让http请求返回json串
- 【DLL测试】为DLL项目建立测试
- hdu 1013 Digital Roots
- 关于c中的%x及其它格式化符
- 小白日记14:kali渗透测试--NMAP
- Android实例-LocationSensor位置传感器(XE8+小米2)
- Android开发之Action Bar
- 1.4分布式-通讯协议TCP/IP
- 团队项目-课程MS需求分析心得
- 删除 nuget 文件夹内容
- Spring实战 难懂的JavaBean
- appium的内存泄露问题
- 插入排序之python
- Spring的bean创建详解
- 20145122《Java程序设计》第一周学习总结
- 华为OJ:2199 推断输入字符串中的括号匹配
- uwsgi特性
- Kuernetes-设计架构(二)
- Hbuilder实用技巧(转)
热门文章
- 剑指offer圆圈中最后剩下的数字 和 迭代器总结
- css的响应式布局和动画
- login SMTP send mail error : Unable to read data from the transport connection: net_io_connectionclosed
- 无法识别的配置节 system.webServer
- 解析underscore中的debounce
- 02 DML(DataManipulationLanguage)
- vlc rtsp 服务器脚本
- Python 35个内置函数,你都ok吗?
- 六十八、SAP中内表插入的三种方法之二,COLLECT的使用,用于计算数字字段之和
- java基础源码 (4)--reflect包-AnnotatedElement接口