Java 动态初始化类
程序员文章站
2022-05-23 15:54:00
...
动态初始化类
目录
在调用的方法的时候,需要根据类型,动态初始化类,常用的用注解 @Resource 或是: @Autowired
比如:
@Resource
private ShapeService shapeService;
这是单个的,如果多个的要如何做? 如果是动态初始化,要如何做呢?
多个初始化
多个的时候,比如使用map
@Resource
private Map<String, Shape> shapeMap = new ConcurrentHashMap<>();
这样会对Shape实现的类进行实例化。
代码:
Shape接口:
public interface Shape {
void getShape(String name);
}
Circle
@Component
public class Circle implements Shape {
@Override
public void getShape(String name) {
System.out.println("圆形: "+name);
}
}
Rectangle
@Component
public class Rectangle implements Shape {
@Override
public void getShape(String name) {
System.out.println("正方形: "+name);
}
}
Triangle
@Component
public class Triangle implements Shape {
@Override
public void getShape(String name) {
System.out.println("三角形: "+name);
}
}
ShapeService
@Service
public class ShapeService {
@Resource
private Map<String, Shape> shapeMap = new ConcurrentHashMap<>();
private static Map<String, String> shapeTypeMap = new ConcurrentHashMap<>();
static {
shapeTypeMap.put("1", "circle");
shapeTypeMap.put("2", "rectangle");
shapeTypeMap.put("3", "triangle");
}
public void showShape(String shapeType){
String shapeImplName = shapeTypeMap.get(shapeType);
if(StringUtils.isBlank(shapeImplName)){
shapeImplName = "circle";// 设置默认值
}
Shape shape = this.shapeMap.get(shapeImplName);
shape.getShape(shapeImplName);
}
}
@Resource
private Map<String, Shape> shapeMap = new ConcurrentHashMap<>();
实例化实现Shape的类: Circle,Rectangle,Triangle,再使用的时候,根据shapeType匹配处理。
动态初始化
动态的话,在使用的时候,根据传的shapeName进行实例化。
SpringUtils
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringUtils.applicationContext = applicationContext;
}
public static Object getBean(String name) {
return applicationContext.getBean(name);
}
public static <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz);
}
}
使用:
public void showBeanShape(String shapeType){
String shapeImplName = shapeTypeMap.get(shapeType);
if(StringUtils.isBlank(shapeImplName)){
shapeImplName = "circle";// 设置默认值
}
Shape bean = (Shape) SpringUtils.getBean(shapeImplName);
bean.getShape(shapeImplName);
}
SpringUtils.getBean(shapeImplName); 根据shapeImplName动态实例化
测试:
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class TestBeans {
@Resource
private ShapeService shapeService;
@Test
public void testInit() {
shapeService.showShape("2");
shapeService.showBeanShape("3");
}
}
结果:
正方形: rectangle
三角形: triangle
总结:
在实例化的时候,类比较多的,可以使用Map<type,Class>加注解@Resource方式,相应类增加的后,初始化后也会实例化相应的内容;不过平时更多用动态实例化的方式,也更好理解些。