springboot 单例模式的两种写法
- 集成 service
因为是单例模式,所有需要spring上下文注入进来,才能获取到
@Component
public class SpringContextUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtils.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBean(String name) {
return applicationContext.getBean(name);
}
public static <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz);
}
public static <T> T getBean(String name, Class<T> clazz) {
return applicationContext.getBean(name, clazz);
}
}
public class JdysEquipmentSinleton {
private static volatile JdysEquipmentSinleton INSYANCE;
private int CODE;
private IJdysEquipmentBillService jdysEquipmentBillServiceForNew;
private JdysEquipmentSinleton(){
jdysEquipmentBillServiceForNew = SpringContextUtils.getBean(IJdysEquipmentBillService.class);
}
public static JdysEquipmentSinleton getInstance(){
if(null == INSYANCE){
synchronized (JdysEquipmentSinleton.class){
if(null == INSYANCE){
INSYANCE = new JdysEquipmentSinleton();
}
}
}
return INSYANCE;
}
public String getMaxEquipmentBillCode2(){
if(0 != CODE){
CODE += 1;
return "D" + handelStrCode(CODE);
}else {
String maxEquipmentBillCode = DataUtil.isNotEmpty(jdysEquipmentBillServiceForNew.getMaxEquipmentBillCode()) ? jdysEquipmentBillServiceForNew.getMaxEquipmentBillCode() : "D00000";
CODE += Integer.valueOf(maxEquipmentBillCode.substring(1,6));
CODE += 1;
return "D" + handelStrCode(CODE);
}
}
public String handelStrCode(int code){
int length = String.valueOf(code).length();
int num = 0;
if(length < 5){
num = 5 - length;
}
String numCode = "";
for (int i = 0; i < num; i++) {
numCode += "0";
}
return numCode + code;
}
private LazyMan(){
System.out.println(Thread.currentThread().getName() + "ok");
}
private volatile static LazyMan lazyMan;
public static LazyMan getInstance(){
if(null == lazyMan){
synchronized (LazyMan.class){
if(null == lazyMan){
lazyMan = new LazyMan();
}
}
}
return lazyMan;
}
package com.central.jdys.config;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
public class LazyMan {
private static boolean LazyFlag = false;
private LazyMan(){
synchronized (LazyMan.class){
if(LazyFlag == false){
LazyFlag = true;
}else {
throw new RuntimeException("不要用反射来破坏");
}
}
System.out.println(Thread.currentThread().getName() + "ok");
}
private volatile static LazyMan lazyMan;
public static LazyMan getInstance(){
if(null == lazyMan){
synchronized (LazyMan.class){
if(null == lazyMan){
lazyMan = new LazyMan();
}
}
}
return lazyMan;
}
public static void main(String[] args) throws Exception {
Field lazyFlag = LazyMan.class.getDeclaredField("LazyFlag");
lazyFlag.setAccessible(true);
Constructor<LazyMan> declaredConstructor = LazyMan.class.getDeclaredConstructor(null);
declaredConstructor.setAccessible(true);
LazyMan instance = declaredConstructor.newInstance();
lazyFlag.set(instance,false);
LazyMan lazyMan = declaredConstructor.newInstance();
System.out.println(instance);
System.out.println(lazyMan);
}
public static class InnerClass{
}
}
- 使用枚举
– 枚举是没用办法使用反射来破解的,枚举没有无参构造,只有一个有参构造,一个是 String,一个是int类型,强行使用反射,jdk会直接抛出异常