03.01 unidal之foundation-service
plexus & foundation-service
简单的说,就一句话, IOC容器,仅仅是IOC容器。
pleuxs是maven的组件,在cat构建项目及1.x版本时,项目负责人使用 plexus进行 组件的管理。
在2.x之后的版本,去除plexus的支持,自己实现一个plexus机制。使用 foundation-service 可以 直接替换掉 plexus.
包的简单说明
build
,是一个构建包的示例,通过类上的注解 生成/META-INF/plexus/compontents-{project}.xml
文件concurrent
并发相关的类库converter
类对象的转换,类型commons-beanutils
formatter
日期格式化helper
常用的工具类initialization
初始化 接口定义 和 模块的关联关系定义lookup
与 IOC 容器交互的主体类库
示例
pom.xml依赖类库
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.unidal.framework</groupId>
<artifactId>foundation-service</artifactId>
<version>4.0.0</version>
</dependency>
接口定义
public interface HelloWorld {
static final String ROLE = HelloWorld.class.getName();
/**
* 向某人打招呼
* @param name 某人
* @return
*/
String hello(String name);
}
实现类
import org.unidal.lookup.extension.InitializationException;
public class HelloWorldImpl implements HelloWorld , Initializable {
@Override
public String hello(String name) {
return "hello "+ name +" , 你好!!!";
}
@Override
public void initialize() throws InitializationException {
System.out.println("---------");
}
}
public class HelloWorldImpl2 implements HelloWorld {
@Override
public String hello(String name) {
return "22===hello "+ name +" , 你好!!!";
}
}
配置文件
路径: /resouces/META-INF/plexus/components-dal.xml
<?xml version="1.0" encoding="UTF-8"?>
<plexus>
<components>
<component>
<role>org.dal.cat.dalcat.plexus.HelloWorld</role>
<implementation>org.dal.cat.dalcat.plexus.impl.HelloWorldImpl</implementation>
<role-hint>helloworld</role-hint>
</component>
<component>
<role>org.dal.cat.dalcat.plexus.HelloWorld</role>
<implementation>org.dal.cat.dalcat.plexus.impl.HelloWorldImpl2</implementation>
<role-hint>helloworld2</role-hint>
</component>
</components>
</plexus>
测试用例
@Slf4j
public class TestPlexusContainerMain extends ComponentTestCase {
@Test
public void testHelloWorld() {
try {
HelloWorld service = lookup(HelloWorld.class, "helloworld");
String msg = service.hello("aaaa");
log.info("{}", msg);
HelloWorld service2 = lookup(HelloWorld.class, "helloworld2");
msg = service2.hello("bbb");
log.info("222==={}", msg);
List<HelloWorld> helloWorld3 = lookupList(HelloWorld.class);
for( HelloWorld world: helloWorld3){
msg = world.hello("---"+System.currentTimeMillis());
log.info("{}", msg);
}
} catch (Exception e) {
log.error("从容器中查找服务不存在失败!", e);
}
}
}
实验结果
--------- #这是HelloWorld的 initialize 打印
17:26:10.743 [main] INFO org.dal.plexus.TestPlexusContainerMain - hello aaaa , 你好!!!
17:26:10.751 [main] INFO org.dal.plexus.TestPlexusContainerMain - 222===22===hello bbb , 你好!!!
17:26:10.751 [main] INFO org.dal.plexus.TestPlexusContainerMain - hello ---1612257970751 , 你好!!!
17:26:10.751 [main] INFO org.dal.plexus.TestPlexusContainerMain - 22===hello ---1612257970751 , 你好!!!
思考&复盘
1. 如何构建一个IOC容器?
- 对象实例化
- 单实例还是多实例
- 加载时初始化还是懒加载
- 实例缓存
- 内容面板管理
- 生命周期的管理
2. 萃取技术点?
- 反射
jdk
的接口cglib
3. IOC容器还有哪些?
spring
的IOC
google
的guice