项目中的一个页面跳转功能存在10个以上的if else判断,想要做一下整改 (代码参照于蚂蚁课堂)
在spring项目中,无需再使用上文中的工厂方法获取具体策略的实现类
目录
具体代码实现:
controller层
scanBasePackages配置解决idea 多module下的service类注入问题
这里用的是Class.forName("类路径")
亦可使用beanId的方式 SpringUtils.getBean(beanId)
package com.google.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author wk
* @Description:
* @date 2019/11/25 16:05
**/
@SpringBootApplication(scanBasePackages="com.google")
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
package com.google.controller;
import com.google.fact.FactApplyStrategy;
import com.google.fact.PurposeEnum;
import com.google.util.SpringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author wk
* @Description:
* @date 2019/12/26 17:59
**/
@RestController
public class FactController {
@RequestMapping(value = "/toAddFact")
public String toAddFact(Integer code) {
FactApplyStrategy factApplyStrategy = null;
try {
factApplyStrategy = (FactApplyStrategy)
SpringUtils.getBean(Class.forName(PurposeEnum.getEnumObjByCode(code).getClassName()));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return factApplyStrategy.toAddFact();
}
}
service层
package com.google.fact;
/**
* 共同行为
*/
public interface FactApplyStrategy {
String toAddFact();
}
package com.google.fact.impl;
import com.google.fact.FactApplyStrategy;
import org.springframework.stereotype.Component;
/**
* @author wk
* @Description:
* @date 2019/12/24 13:59
**/
@Component
public class GdFactApplyStrategy implements FactApplyStrategy {
@Override
public String toAddFact() {
return "跳转光大银行";
}
}
package com.google.fact.impl;
import com.google.fact.FactApplyStrategy;
import org.springframework.stereotype.Component;
/**
* @author wk
* @Description:
* @date 2019/12/26 14:51
**/
@Component
public class JTFactApplyStrategy implements FactApplyStrategy {
@Override
public String toAddFact() {
return "跳转交通银行";
}
}
package com.google.fact.impl;
import com.google.fact.FactApplyStrategy;
import org.springframework.stereotype.Component;
/**
* @author wk
* @Description:
* @date 2019/12/26 17:10
**/
@Component
public class ZGFactApplyStrategy implements FactApplyStrategy {
@Override
public String toAddFact() {
return "跳转中国银行";
}
}
枚举类
package com.google.fact;
/**
* 策略枚举类
*/
public enum PurposeEnum {
TYPE_1(1, "中国银行", "com.google.fact.impl.ZGFactApplyStrategy"),
TYPE_2(2, "光大银行", "com.google.fact.impl.GdFactApplyStrategy"),
TYPE_3(3, "交通银行", "com.google.fact.impl.JTFactApplyStrategy"),
;
private Integer code;
private String mark;
private String className;
private PurposeEnum(Integer code, String mark, String className) {
this.code = code;
this.mark = mark;
this.className = className;
}
public Integer getCode() {
return code;
}
public String getMark() {
return mark;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public static PurposeEnum getEnumObjByCode(Integer code) {
for (PurposeEnum be : values()) {
if (be.getCode().equals(code)) {
return be;
}
}
return null;
}
}
utils:
package com.google.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* @author wk
* @Description:
* @date 2019/12/26 19:11
**/
@Component
public class SpringUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
/**
* 获取applicationContext
*
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 通过name获取Bean
*
* @param name
* @return
*/
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
/**
* 泛型怎么是写在前面
* 通过class获取Bean
*
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
/**
* 通过name class返回指定的Bean
*
* @param name
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}