Struts2 源码分析——配置管理之ContainerProvider接口
| 本章简言 |
上一章笔者讲到关于Dispatcher类的执行action功能,知道了关于执行action需要用到的信息。而本章将会讲到的内容也跟Dispatcher类有关系。那就是配置管理中的ContainerProvider类。我们都知道在struts2启动的时候,struts2会去加载对应的配置文件。如struts.xml文件等。如果笔者没有记错的话。在《Struts2 源码分析——调结者(Dispatcher)之准备工作》的章节里面讲到过关于ConfigurationManager类的一些知识。可是并没有深入的研究和讲解。先让我们回顾一下吧。ConfigurationManager类里面会存放大量供应者的类。供应者就是专门为Container容器提供数据的中间者。而ContainerProvider接口又是所有供应者的父接口,这也笔者不得不讲解的理由了。
| Container容器 |
在讲解到ContainerProvider接口的时候,笔者要先讲一下Container容器相关的知识。主要是Container容器和供应者的关系太微妙了。但是笔者不想讲解关于Container容器的源码实现。这并不是笔者写这一系列的课题。只是跟这个课题有关系而以。笔者不清楚读者有没有用Spring框架。Spring框架便是JAVA界里面实现IOC框架中最好的框架之一。笔者看过很多IOC的框架。如.NET界的Autofac。所以笔者知道一点:一般实现IOC思想的框架都会有俩个类。即是Container类和ContainerBuilder类。
ContainerBuilder类是用于创建Container容器的。Container容器里面会存放大量的类的实例和类的信息。而Container容器又是什么知道自己本身里面存放了这些类的实例和类的信息。总要有人跟Container容器讲吧。所以ContainerBuilder类的工作就是注册相关将来要在Container容器里面存放的类实例和类的信息。注册的代码体现在不同的作者有着不同的表现。这边表现在factory方法。当然除了factory方法用于注册相关类的信息。还有其他的三个方法。如下
1.factory方法:注册类的信息。
2.alias方法:注册一个别名。即是本身存在一个类的信息,现在想让他拥有一个别名,通过这个别名也能获得这个类的信息。
3.constant方法:注册一个常理。Map类的用法大家都知道吧。这个用法也是一个的。给一个KEY就可以获得对应的VALUE。
4.create方法:用于创建Container容器。
上面的四个方法就是当前struts2最常用到。同时还要注意的是每一个类的信息都会有自己的生命周期。可是一个线程一个实例。也能是每次获得都是最新的实例。也可是从头到尾都是用一个实例。关键字Scope就是最好的代码体现。Scope是一个enum类型。他就是存放了关于类信息的生命周期。有几种的读者请自己查看吧。
Container类是用于存放类的信息和类的实例。他的作用容器一样子。那如何获得呢?这里就要讲到关于IOC思想里面有一个概念。即是依赖注入。也就是inject方法。那读者就会说有没有像用MAP类那样子的用法呢?笔者可以明确的跟你讲:有。
1.inject方法:用于依赖注入。什么是依赖注入呢?简单的讲就是你用了Container类实例的inject方法来作用一个类实例的话。当类实例所有需要注入的属性都会帮你生成。不用你自己动手去实例。在代码中注解@Inject就是最好的体现。如下
Dispatcher类的init方法:
1 Container container = init_PreloadConfiguration();// 初始化相关的配置信息,并加载以上供应者。 2 container.inject(this);// 注入当前类依赖项的信息。
2.getInstance方法:不用笔者讲。大家一看就明白。就是传入一个KEY就可以获得对应的实例了。
| ContainerProvider接口 |
有了上面小节的知识,笔者在来讲解ContainerProvider接口的时候,很多问题就会变得很简单。Dispatcher类里面有一个成员变量configurationManager(ConfigurationManager类)。configurationManager变量在初始化Container容器的时候(在init_PreloadConfiguration方法里面)开始起作用了。在此之前configurationManager变量只是增加相关的供应者而以。为什么在初始化Container容器的时候要用到ConfigurationManager类的实例呢?那就要讲到一个接口Configuration。Configuration接口默认有一个实现类DefaultConfiguration类。可以说Container容器的初始化必须通过Configuration接口。看一下代码吧。
Dispatcher类:
1 /**
2 * @return 获得对应的容器
3 */
4 public Container getContainer() {
5 if (ContainerHolder.get() != null) {
6 return ContainerHolder.get();
7 }
8 ConfigurationManager mgr = getConfigurationManager();
9 if (mgr == null) {
10 throw new IllegalStateException("The configuration manager shouldn't be null");
11 } else {
12 Configuration config = mgr.getConfiguration();
13 if (config == null) {
14 throw new IllegalStateException("Unable to load configuration");
15 } else {
16 Container container = config.getContainer();
17 ContainerHolder.store(container);
18 return container;
19 }
20 }
21 }
从上面的代码笔者就能明白。获得ConfigurationManager类实例,在通过ConfigurationManager类实例来获得对应的Configuration接口实例。即是DefaultConfiguration类实例。Configuration接口里面有一个getContainer方法来获得Container容器。关键就是要停留在ConfigurationManager类的getConfiguration方法上面。为什么呢?看一下代码吧。
ConfigurationManager类:
1 public synchronized Configuration getConfiguration() {
2 if (configuration == null) {
3 setConfiguration(createConfiguration(defaultFrameworkBeanName));
4 try {
5 configuration.reloadContainer(getContainerProviders());//加载初始化Container容器
6 } catch (ConfigurationException e) {
7 setConfiguration(null);
8 throw new ConfigurationException("Unable to load configuration.", e);
9 }
10 } else {
11 conditionalReload();//判断是否重新加载初始化Container容器
12 }
13
14 return configuration;
15 }
看样子笔者不必多讲就能看出实际上加载初始化Container容器在获得Configuration接口实例的时候就已经初始化完成了。这也是本章核心代码部分。即是Configuration接口的reloadContainer方法。只要明白了这方法的工作就可以知道很多事件。代码如下
DefaultConfiguration类:
1 public synchronized List<PackageProvider> reloadContainer(List<ContainerProvider> providers) throws ConfigurationException {
2 packageContexts.clear();
3 loadedFileNames.clear();
4 List<PackageProvider> packageProviders = new ArrayList<>();//用于存放用于实现PackageProvider接口的供应者类
5
6 ContainerProperties props = new ContainerProperties();//用于常量存放,当然这边加工了Properties类。多出一个叫本地化的值。即是KEY-VALUE-LOCATION
7 ContainerBuilder builder = new ContainerBuilder();//用于创建Container容器的类
8 Container bootstrap = createBootstrapContainer(providers);//新建一个Container容器用于初始化信息。
9 for (final ContainerProvider containerProvider : providers)
10 {
11 bootstrap.inject(containerProvider);
12 containerProvider.init(this);//初始化对应的供应者。
13 containerProvider.register(builder, props);//注册供应者的数据
14 }
15 props.setConstants(builder);//把常量注册到builder里面去
16
17 //注册Configuration
18 builder.factory(Configuration.class, new Factory<Configuration>() {
19 public Configuration create(Context context) throws Exception {
20 return DefaultConfiguration.this;
21 }
22 });
23
24 /*****************************************************分界线*****************************************************************/
25 ActionContext oldContext = ActionContext.getContext();
26 try {
27
28 setContext(bootstrap);//创建一个Action上下文
29 container = builder.create(false);//新建一个Container容器
30 setContext(container);//创建一个Action上下文
31 objectFactory = container.getInstance(ObjectFactory.class);
32
33 // 处理用户配置里面的供应者,如果是PackageProvider,就是加载对应的package元素信息
34 for (final ContainerProvider containerProvider : providers)
35 {
36 if (containerProvider instanceof PackageProvider) {
37 container.inject(containerProvider);
38 ((PackageProvider)containerProvider).loadPackages();
39 packageProviders.add((PackageProvider)containerProvider);
40 }
41 }
42
43 // 然后处理当前插件供应者,加载对应的package元素信息
44 Set<String> packageProviderNames = container.getInstanceNames(PackageProvider.class);
45 for (String name : packageProviderNames) {
46 PackageProvider provider = container.getInstance(PackageProvider.class, name);
47 provider.init(this);
48 provider.loadPackages();
49 packageProviders.add(provider);
50 }
51
52 rebuildRuntimeConfiguration();//新建运行时候,用的配置
53 } finally {
54 if (oldContext == null) {
55 ActionContext.setContext(null);
56 }
57 }
58 return packageProviders;
59 }
应该说这个方法可以分为上部分和下部分来讲。上部分是为用于初始化并注册供应者提供的数据。而下部分是为加载对应有package元素的供应者的数据。如strust.xml的package节点。其实这个方法里面总共新建了俩次Container容器。第一个容器是为了初始化时候用到的。而第二个容器才是最终要用的容器。containerProvider.register(builder, props)这句代码就是可以明白了《Struts2 源码分析——调结者(Dispatcher)之准备工作》的章节里面讲到的ContainerProvider接口。也是containerProvider接口和Dispatcher类这间的关系体现。
上部分中第一次初始化了一个Container容器,调用createBootstrapContainer方法来实现Container容器。createBootstrapContainer方法里注册了一些基本要用到的类信息。这里就不细详的讲说。同时我们还发现一个类ContainerProperties,所有常量注册都是先存放在这个类里面,然后在注册到ContainerBuilder类里面。最后在把DefaultConfiguration本身注册进去。关键的代码部分笔者已经在上面代码中标红色了。这个部分就是触发containerProvider接口的注册。
下部分中又在一次初始化Container容器用于加载有package元素节点的配置文件的包供应者。包供应者都是继承了PackageProvider接口。PackageProvider接口(笔者会在下一个章节讲到)
最后我们会看到一个rebuildRuntimeConfiguration方法。这个方法的功能就是创建一个运行时的配置。用于运行时候调用。
看到这里的时候。笔者就是明白了。如果想要知道什么加载配置文件就必须去找到对应的供应者类。那么对应加载配置文件的供应者类有StrutsXmlConfigurationProvider和父类XmlConfigurationProvider。只要了解了这俩个类相信笔者就明白了什么加载了。主要是要看XmlConfigurationProvider类的register方法。代码如下。
XmlConfigurationProvider类:
1 public void register(ContainerBuilder containerBuilder, LocatableProperties props) throws ConfigurationException {
2 LOG.trace("Parsing configuration file [{}]", configFileName);
3 Map<String, Node> loadedBeans = new HashMap<>();
4 for (Document doc : documents) {
5 Element rootElement = doc.getDocumentElement();
6 NodeList children = rootElement.getChildNodes();
7 int childSize = children.getLength();
8
9 for (int i = 0; i < childSize; i++) {
10 Node childNode = children.item(i);
11
12 if (childNode instanceof Element) {
13 Element child = (Element) childNode;
14
15 final String nodeName = child.getNodeName();
16
17 if ("bean".equals(nodeName)) {//加载Bean节点
18 String type = child.getAttribute("type");
19 String name = child.getAttribute("name");
20 String impl = child.getAttribute("class");
21 String onlyStatic = child.getAttribute("static");
22 String scopeStr = child.getAttribute("scope");
23 boolean optional = "true".equals(child.getAttribute("optional"));
24 Scope scope = Scope.SINGLETON;
25 if ("prototype".equals(scopeStr)) {
26 scope = Scope.PROTOTYPE;
27 } else if ("request".equals(scopeStr)) {
28 scope = Scope.REQUEST;
29 } else if ("session".equals(scopeStr)) {
30 scope = Scope.SESSION;
31 } else if ("singleton".equals(scopeStr)) {
32 scope = Scope.SINGLETON;
33 } else if ("thread".equals(scopeStr)) {
34 scope = Scope.THREAD;
35 }
36
37 if (StringUtils.isEmpty(name)) {
38 name = Container.DEFAULT_NAME;
39 }
40
41 try {
42 Class classImpl = ClassLoaderUtil.loadClass(impl, getClass());
43 Class classType = classImpl;
44 if (StringUtils.isNotEmpty(type)) {
45 classType = ClassLoaderUtil.loadClass(type, getClass());
46 }
47 if ("true".equals(onlyStatic)) {
48 // Force loading of class to detect no class def found exceptions
49 classImpl.getDeclaredClasses();
50 containerBuilder.injectStatics(classImpl);
51 } else {
52 if (containerBuilder.contains(classType, name)) {
53 Location loc = LocationUtils.getLocation(loadedBeans.get(classType.getName() + name));
54 if (throwExceptionOnDuplicateBeans) {
55 throw new ConfigurationException("Bean type " + classType + " with the name " +
56 name + " has already been loaded by " + loc, child);
57 }
58 }
59
60 // Force loading of class to detect no class def found exceptions
61 classImpl.getDeclaredConstructors();
62
63 LOG.debug("Loaded type: {} name: {} impl: {}", type, name, impl);
64 containerBuilder.factory(classType, name, new LocatableFactory(name, classType, classImpl, scope, childNode), scope);
65 }
66 loadedBeans.put(classType.getName() + name, child);
67 } catch (Throwable ex) {
68 if (!optional) {
69 throw new ConfigurationException("Unable to load bean: type:" + type + " class:" + impl, ex, childNode);
70 } else {
71 LOG.debug("Unable to load optional class: {}", impl);
72 }
73 }
74 } else if ("constant".equals(nodeName)) {//加载常量节点
75 String name = child.getAttribute("name");
76 String value = child.getAttribute("value");
77 props.setProperty(name, value, childNode);
78 } else if (nodeName.equals("unknown-handler-stack")) {
79 List<UnknownHandlerConfig> unknownHandlerStack = new ArrayList<UnknownHandlerConfig>();
80 NodeList unknownHandlers = child.getElementsByTagName("unknown-handler-ref");
81 int unknownHandlersSize = unknownHandlers.getLength();
82
83 for (int k = 0; k < unknownHandlersSize; k++) {
84 Element unknownHandler = (Element) unknownHandlers.item(k);
85 Location location = LocationUtils.getLocation(unknownHandler);
86 unknownHandlerStack.add(new UnknownHandlerConfig(unknownHandler.getAttribute("name"), location));
87 }
88
89 if (!unknownHandlerStack.isEmpty())
90 configuration.setUnknownHandlerStack(unknownHandlerStack);
91 }
92 }
93 }
94 }
95 }
相关这一段代码笔者并没有注解相关的内容。主要是代码里面的一些方法在文章中都有讲到。这个部分主要是读取XML的内容。然后注册到ContainerBuilder类里面。 看完这一段代码之后,笔者相信朋友们心里面都有一些明白了。
| 本章总结 |
本章主要是讲到了关于ContainerProvider接口的知识点。ContainerProvider接口是所有供应者的父接口。用于注册类信息提供了帮助。值得注意的是要明白Dispatcher类和ConfigurationManager类的关系,ConfigurationManager类和Configuration接口的关系,Configuration接口又跟Container类的关系,Container类又跟ContainerProvider接口的关系。


浙公网安备 33010602011771号