创建一个spring boot的web项目
创建如下包 Count代码
package com.example.demo.bean;
public class Count {
private int count;
public int getCount() {
return count;
}
public void setCount(int count)
{
this.count=count;
}
}
controller代码:
package com.example.demo.controller;
import com.example.demo.bean.Count;
import com.example.demo.service.ResourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
public class controller {
@Autowired
ResourseService resourseService;
@RequestMapping(value = "/me/count",method = RequestMethod.PUT)
@ResponseBody
public void initCount(@RequestBody Count count) {
resourseService.initCount(count);
}
@RequestMapping(value = "/me/count", method = RequestMethod.POST)
@ResponseBody
public void modifyCount(@RequestBody Count count){
resourseService.addCount(count);
}
@RequestMapping(value = "/me/count", method = RequestMethod.GET)
@ResponseBody
public Count getCount() {
return resourseService.getCount();
}
}
manager代码
package com.example.demo.manager;
public class ResourseManager {
public static ResourseManager getInstance;
private int count = 0;
private static ResourseManager instance = new ResourseManager();
private ResourseManager(){}
public static ResourseManager getInstance() {
return instance;
}`在这里插入代码片`
public synchronized void addCount(int i){
count = count+i;
}
public synchronized void minusCount(int i){
count= count-i;
}
public int getCount(){
return count;
}
}
service代码
```css
package com.example.demo.service;
import com.example.demo.bean.Count;
import com.example.demo.manager.ResourseManager;
import org.springframework.stereotype.Service;
import java.awt.*;
@Service
public class ResourseService {
public void initCount(Count count) {
if (count != null){
ResourseManager.getInstance().addCount(count.getCount());
}
}
public void addCount(Count count) {
if (count!=null){
ResourseManager.getInstance.minusCount(count.getCount());
}
}
public Count getCount() {
Count count = new Count();
count.setCount(ResourseManager.getInstance().getCount());
return count;
}
}
|