注意在调用的类和被注入的类上面都要加
@Scope("prototype")
案例 被调用类
package com.pgmsg.pojo;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
@Service
@Scope("prototype")
public class Response {
//1正常 0错误 -1异常
private int status = 0;
private String msg = null;
private Object data = null;
/* public Response(){
this.setStatus(0);
}*/
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public void destroy(){
System.out.println("destroy RESPONSE");
}
}
2.调用的controller
package com.pgmsg.controller.backend;
import com.pgmsg.pojo.BackendAdmin;
import com.pgmsg.pojo.BackendAuth;
import com.pgmsg.pojo.Response;
import com.pgmsg.service.backend.AdminServiceI;
import com.pgmsg.service.backend.impl.AdminServiceImpl;
import com.pgmsg.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.List;
@RestController
@Scope("prototype")
public class AdminController {
@Autowired
AdminServiceImpl adminService;
@Autowired
Response response;
/**
* 管理员列表
*/
@RequestMapping(value = "/backend/admin/list", method = RequestMethod.GET)
public List<BackendAdmin> list(BackendAdmin admin) {
return adminService.list(admin);
}
/**
* 增
*/
@RequestMapping(value = "/backend/admin", method = RequestMethod.POST)
public Response add(BackendAdmin admin) {
// System.out.println(admin);
if (admin.getUserName() == null || admin.getPassword() == null || admin.getUserName().length() == 0 || admin.getPassword().length() == 0) {
response.setMsg("请填写登录名和密码");
return response;
} else {
BackendAdmin admin1 = adminService.getAdminByName(admin.getUserName());
if (admin1 != null) {
System.out.println(admin1.toString());
response.setMsg("登录名已存在");
return response;
}
}
String check = ToolUtil.checkPassword(admin.getPassword());
if (!check.equals("1")) {
response.setMsg(check);
return response;
}
admin.setId(0L);
String md5_password = DigestUtils.md5DigestAsHex(admin.getPassword().getBytes(StandardCharsets.UTF_8));
admin.setPassword(md5_password);
response.setStatus(1);
int re = adminService.addAdmin(admin);
if (re == 1) {
System.out.println(re);
response.setStatus(1);
response.setMsg("添加成功!");
} else {
response.setMsg("添加失败!");
}
return response;
}
/**
* 改
*/
@RequestMapping(value = "/backend/admin", method = RequestMethod.PUT)
public Response edit(BackendAdmin admin) {
System.out.println(admin.getId());
if (admin.getId() == 0) {
response.setMsg("参数缺失");
return response;
} else {
BackendAdmin admin1 = adminService.getAdminById(admin.getId());
if (admin1 == null) {
response.setMsg("用户不存在");
return response;
}
}
if (admin.getPassword() != null) {
admin.setPassword(null);
}
//查询是否有同名不同ID用户存在
if (admin.getUserName() != null) {
BackendAdmin admin2 = adminService.getAdminByNotId(admin);
if (admin2 != null) {
response.setMsg("该用户名已被他人使用!");
return response;
}
}
int re = adminService.editAdmin(admin);
if (re == 1) {
System.out.println(re);
response.setStatus(1);
response.setMsg("编辑成功!");
} else {
response.setMsg("编辑失败!");
}
return response;
}
/**
* 删
*/
@RequestMapping(value = "/backend/admin", method = RequestMethod.DELETE)
public Response delete(BackendAdmin admin) {
if (admin.getId() == 0) {
response.setMsg("参数缺失");
return response;
} else if (admin.getId() == 1) {
response.setMsg("管理员不可删除");
return response;
}
int re = adminService.deleteAdmin(admin);
if (re == 1) {
System.out.println(re);
response.setStatus(1);
response.setMsg("删除成功!");
} else {
response.setMsg("删除失败!");
}
return response;
}
/**
* 修改密码
*/
@RequestMapping(value = "/backend/admin/password", method = RequestMethod.POST)
public Response resetPassword(BackendAdmin admin, String newPassword, String confirmPassword) {
if (admin.getId() == 0 || admin.getPassword() == null || newPassword == null || confirmPassword == null) {
response.setMsg("请填写原密码,新密码以及确认密码!");
return response;
}
if (!newPassword.equals(confirmPassword)) {
response.setMsg("新密码和确认密码不一致!");
return response;
}
BackendAdmin admin1 = adminService.getAdminById(admin.getId());
if (admin1 == null) {
response.setMsg("用户不存在!");
return response;
}
String md5Password = DigestUtils.md5DigestAsHex(admin.getPassword().getBytes(StandardCharsets.UTF_8));
if (!admin1.getPassword().equals(md5Password)) {
response.setMsg("原密码不正确!");
return response;
}
String check = ToolUtil.checkPassword(newPassword);
if (!check.equals("1")) {
response.setMsg(check);
return response;
}
admin.setPassword(DigestUtils.md5DigestAsHex(newPassword.getBytes(StandardCharsets.UTF_8)));
admin.setUpdateTime(Instant.now().getEpochSecond());
int re = adminService.editAdmin(admin);
if (re == 1) {
System.out.println(re);
response.setStatus(1);
response.setMsg("密码修改成功!");
} else {
response.setMsg("密码修改失败!");
}
return response;
}
/**
* 登录
*/
@RequestMapping(value = "/backend/admin/login", method = RequestMethod.POST)
public Response login(BackendAdmin admin) {
if (admin.getUserName() == null || admin.getPassword() == null || admin.getUserName().length() == 0 || admin.getPassword().length() == 0) {
response.setMsg("请填写登录名和密码");
return response;
}
BackendAdmin admin1 = adminService.getAdminByName(admin.getUserName());
if (admin1 == null) {
response.setMsg("用户不存在");
return response;
}
if(!admin1.getPassword().equals(DigestUtils.md5DigestAsHex(admin.getPassword().getBytes(StandardCharsets.UTF_8)))){
response.setMsg("密码错误!");
return response;
}
admin1.setLoginNumber(admin1.getLoginNumber()+1);
admin1.setUpdateTime(Instant.now().getEpochSecond());
admin1.setLoginToken(ToolUtil.getToken());
admin1.setTokenExpire(Instant.now().getEpochSecond() + 3600);
int re = adminService.editAdmin(admin1);
if (re == 1) {
admin1.setPassword(null);
System.out.println(re);
response.setStatus(1);
response.setMsg("登录成功!");
response.setData(admin1);
} else {
response.setMsg("登录失败!");
}
return response;
}
/**登出*/
}