Spring Boot框架

Spring Boot框架

简介

封装Spring框架 简化Spring开发
注解标记,代码开发使用的Spring Boot
Spring Boot是pivotal团队推出的一个框架
简化Spring Boot框架的开发应用,实现快速开发。

Spring Boot具有以下特点:

spring Boot内置集成Tomcat
spring Boot实现了无xml配置开发(properties 或yml)

properties 
spring.datasource.usrename=scott

yml
spring:
datasource:
username: scott
password: 123456

SpringBoot提供了自动配置功能
SpringBoot 应用可以实现jar包发布
java jar包
web war包
统一为jar包

SpringBoot环境搭建

1.添加jar包

springBoot可以看成一组jar包集合,简化了jar包引入过程
spring-boot.starter-xxx
基础功能包:spring-boot-starter-parent
核心包:spring-boot-starter
扩展功能包:。。。。

2.添加配置文件
默认加载的配置文件 application.properties 或 application.yml

3.启动类

SpringBoot环境搭建过程

1使用Maven创建Boot工程,在pom.xml引入jar包
2.可以使用官网创建Maven project with java
网站:start.spring.io网址,输入参数
cn.xdl
springboot1
生成后jar包导入配置,application.properties、启动类自动生成
application.properties
设置端口号 设置路径

server.port=9999
server.contextPath=/boot1

@SpringBootApplication 主配置文件
启动类
SpringApplication.run(SpringBootAPPLication.class);
当前类做入口功能

## 注意:必须使启动类与Controller在一个包下,不然无法扫描
3.启动类
@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}
4.Controller

@Controller
public class HelloController {

	@RequestMapping("/hello")
	@ResponseBody
	public String hello() {
		return "hello SpringBoot";
	}
	@RequestMapping("/hello1")
	@ResponseBody
	public Object hello1() {
		User user=new  User();
		user.setUsername("scott");
		user.setPassword("123456");
		return user;
	}
}

隐含约定 默认扫描主类包 ,及其子包
重启tomcat服务器,手段停止,不然显示端口被占用

手动配置springboot

SpringBoot Beans定义
Spring框架核心IOC、使用时需要将组件交给spring容器管理,然后可以使用依赖注入实现组件对象关系的建立
spring将组件纳入容器中的方法如下:

定义
context:component-scan basePackage=组件扫描

@Configuration+@Bean
@componentScan

入口类

@SpringBootApplication
public class BootApplication {

public static void main(String[] args) {
	SpringApplication.run(BootApplication.class, args);
}

}

注解配置


import com.xdl.boot.eneity.User;

@Configuration  //beans
public class BeanConfig {
	@Bean //id="createUser"
	public User createUser() {
		User user=new User();
		user.setUsername("hello");
		user.setPassword("12345");
		return user;
		
	}
	
	@Bean(name="user")
	public User createUser1() {
		User user=new User();
		user.setUsername("tom");
		user.setPassword("123");
		return user;
	}
}

测试类

public class TestConfiguration {
	
	@Test
	public void test1() {
		//创建一个Spring容器。然后获取user对象
		ApplicationContext ac=SpringApplication.run(BeanConfig.class);
		User user=ac.getBean("createUser",User.class);
		System.out.println(user.getUsername()+user.getPassword());
	}

}
@ComponentScan  //默认扫描本包及其子包
//@ComponentScan(basePackages= {"com.xdl.boot.bean"})
public class ScanConfig {

	
}

@Component
public class MyDataSource {

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值