1.spring boot实现热部署方式一 jrebel
只要点击resources右键最下位,选择Jrebel--rebel.xml 就会创建配置文件
然后每次更新只需要cmd+F9就会自动更新修改过的地方
2.spring boot实现热部署方式二 spring-boot-devtools
在spring boot下,使用这种方式更快!!!
添加此依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency>
注意事项
在preference的build中的compiler中选择Build project automatically
第二步按shift alt cmd / 快捷键 点击registry 选中compiler when app running
还有其他人说的几个其他要点,虽然我觉得并没什么用
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional>//这点 </dependency></dependencies><build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork><!--和这点 如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart --> </configuration> </plugin> </plugins></build>
3.Bean的声明和作用
@Component 组件,没有明确的角色
@Service 在业务逻辑层 service层使用
@Repository 在数据访问层 dao层使用
@Controller 在展现层 MVC使用
注入Bean的注解
@Autowired Spring提供的注解 //主要使用这个
@Inject JSR-300提供的注解 //这个没有代码提示,没法用
@Resource JSR-250提供的注解 //这个和Autowired效果相同
注入Bean可以作用在方法和属性上,推荐注入在属性上,代码更少,层次更清晰
以Service注解为例
创建Bean FunctionService
@Service
public class FunctionService {
public String sayService(String word){ return "我是"+word;}
}
使用Bean UseFunctionService
@RestController
@Service//声明UseFunctionService是一个Bean,才能使用其他Bean
public class UseFunctionService {
@Autowired//自动注入FunctionService BeanFunctionService functionService;@GetMapping(value = "service/{word}")public String sayHello(@PathVariable("word") String word){ return functionService.sayService(word);}
}
4.我对aop切面的理解
如何创建一个aop切面
1.@Aspect注解声明一个切面
2.@Component让切面成为一个Bean
3.@PointCut注解声明切点
4.@After注解声明一个建言
5.可以通过java的反射获取注解上的属性
6.@Before注解声明一个建言,此建言直接使用拦截规则作为参数
我对aop的理解:aop就是建立一个抽象层,可以作用于多个对象,对多个对象实现相同的动作
比如Http的Request访问是一个对象,那么如果我们想对所有的Request进行前置动作,和后置
动作,那么就可以创建一个切面。aop即不用在Request中显示的写出来,又能发挥作用。
我将此类比于laravel的中间件
5.Bean的作用域
@Scope("Singleton") 一个Spring容器中只有一个Bean实例,此为Spring默认配置,全窗口共享一个实例
@Scope("Prototype") 每次调用新建一个Bean实例
@Scope("Request") 每次发起一个Http Request创建一个Bean实例
@Scope("Session") 每次创建一个Http Session创建一个Bean实例
6.spring EL 和资源调用
拥有以下作用
注入普通字符
注入操作系统属性
注入表达式运算结果
注入其他Bean的属性
注入文件内容
注入网址内容
注入属性文件
7.Bean的初始化和销毁
比如我们想在Bean初始化时做一些动作,销毁时做一些动作,就可以使用下面的jsr250
引入依赖
<dependency> <groupId>javax.annotation</groupId> <artifactId>jsr250-api</artifactId> <version>1.0</version> </dependency>
先创建BeanWayService
再创建JSR250WayService
最后创建PrePostConfig和Main
8.事件 Application Event
DemoEvent 继承ApplicationEvent接口,创建事件
DemoListener 继承ApplicationListener<DemoEvent>接口,并传入事件类型,接收处理
DemoPublisher 注入应用的环境,发布事件
9.组合注解
比如@Configuration和@ComponentScan("com.yurencloud")两个注解通常都是一起出现的,所以可以用spring提供的组合注解
@WiselyConfiguration("com.yurencloud")
10.最快的Hello World
直接在xxxApplication中写@RestController
@RestController
@SpringBootApplication//这个是核心注解,用来启动spring boot,这是一个组合注解,可以cmd+b来看原注解
public class SpringBoot2Application {
@RequestMapping("/")String index(){ return "Hello Spring Boot";}public static void main(String[] args) { SpringApplication.run(SpringBoot2Application.class, args);}
}
11.定制Banner,就是每次启动spring boot时,在控制台的欢迎图案
在src/main/resources下新建一个banner.txt,此文件中的字符就会作为欢迎图案
关闭欢迎图案
//在启动的main中添加
SpringApplication app = new SpringApplication(项目启动类.class);
app.setShowBanner(false);//关闭欢迎图案
app.run(args);//重新载入设置
12.全局配置文件
放到resources/config下
application.properties
application-dev.properties
application-prod.properties
application.yml
application-dev.yml
application-prod.yml
13.spring boot升级成HTTPS
letsencrypt 的使用方式还是一样,生成加密的文件
只需要在spring boot 的全局配置文件中添加上面的引用就可以,例如:
server.port: 8443
security.require-ssl=true
server.ssl.key-store:/etc/letsencrypt/live/example.com/keystore.p12
server.ssl.key-store-password: <your-password>
server.ssl.keyStoreType: PKCS12
server.ssl.keyAlias: tomcat`
14.网站的Favicon图标设置
只需要把favicon.ico这样取名的图标放到src/main/resources/static目录下就可以
关闭图标如下
spring.mvc.favicon.enabled=false
15.数据库查询,排序
在findAll中传入以面参数
new Sort(Sort.Direction.DESC,"id")
16.数据库查询,分页
先在定义数据仓库的地方给方法添加Pageable
Page<Admin> findAll(Pageable pageable);
然后使用的时候,就可以传递pageable实例对象参数
其中new PageRequest的第一个参数是offset
@GetMapping(value = "/admin/page/{id}")
public Page<Admin> getListPage(@PathVariable Integer id){ return adminRepository.findAll(new PageRequest(id,4));}
然后他返回的数据中,除了当前获取的数据外,还有用与分页的额外数据
以方便判断当前是最后一页,总共多少页,总共有多少条记录,每页多少条,是否排序,是否是第一条,当前多少条
"last":true,"totalPages":2,"totalElements":8,"size":4,"number":1,"sort":null,"first":false,"numberOfElements":4}
17.在resources下新建一个import.sql文件
每次重启spring boot之后 都 会自动的向数据库执行此文件
可以用来初始化测试数据
相当于laravel中的seeder
18.使用data-rest 来增强jpa,简化数据操作
原本我们要使用jpa来查询一条数据,那么要写repository,要创建model,要创建controller
现在我们只要在全局配置文件中引入REST的配置类,那么我们将自动创建所有的repository的REST风格的访问路径
在localhost:8080下可以看到我们可以通过哪些链接来访问这些数据
{
"_links" : {
"admins" : { "href" : "http://localhost:8080/admins{?page,size,sort}", "templated" : true},"girls" : { "href" : "http://localhost:8080/girls{?page,size,sort}", "templated" : true},"profile" : { "href" : "http://localhost:8080/profile"}
}
}
如何使用REST呢
1.添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency>
2.在你的配置文件中引入RepositoryRestMvcAutoConfiguration.class REST的配置文件
@Import(RepositoryRestMvcAutoConfiguration.class)
3.正常的创建每个数据表的repository和model,在model中要创建一个构造器包含所有参数并带super()
此时就可以通过http://localhost:8080/girls{?page,size,sort} 这样一个路径操作数据了
REST的通过访问方式的不同,来操作数据,可以用postman来进行测试
GET:获取数据
用GET方式,请求http://localhost:8080/girls/1 获取id为1的girl的信息
POST:添加数据
用POST方式,请求http://localhost:8080/girls 传递{"age":7,"cupSize":"D"}的json信息,会自动添加该数据,服务器会返回该数据
PUT:更新数据
用PUT方式,请求http://localhost:8080/girls/1 传递{"age":17,"cupSize":"D"}的json信息,会自动更新该数据,服务器会返回该数据
DELETE:删除数据
用DELETE方式,请求http://localhost:8080/girls/1 会删除id为1的gril数据
4.修改访问数据的路径
REST数据访问都是直接在根路径下的
http://localhost:8080/girls
我们可以在application.properties中添加
spring.data.rest.base-path= /api
那么路径就会变成
http://localhost:8080/api/girls
注意,默认的REST访问路径是数据表的小写复数形式比如 girl 就是 /girls
19.启动spring应用的几种方式
1.使用idea来启动应用,在导航栏或者右键都有启动按钮
2.使用mvn命令来启动mvn spring-boot:run
3.使用mvn命令编译mvn install,然后在tartget目录下会有xxx-SNAPSHOT.jar文件,使用java -jar xxx-SNAPSHOT.jar即可启动spring程序
20.使用properties或yml文件
二者的作用效果是一样的,只不过yml更加简练,所以推荐
application.properties
server.port=8081#在8081端口启动应用
server.context-path=/girl#给项目的根目录加上girl
application.yml
server:
port: 8081#8081前记得有一个空格
context-path: /girl
name: haha#如果是自定义的变量名,则要顶格写,每空两格则是一个嵌套
age: 18
name-age: "name is ${name} and age is ${age}"#可以在变量中引用变量
girl:
girl-name: cindy
girl-age: 20