xWork框架作为struts2的核心框架知识,驱动整个struts2的业务链.那么xWork容器是如何来管理对象的呢?首先我们来看看container源码:
public interface Container extends Serializable
{
/**
* Default dependency name.
* 定义默认的对象获取标识
*/
String DEFAULT_NAME = "default";
/**
* Injects dependencies into the fields and methods of an existing object.
* 运行对象依赖注入的基本操作接口,作为参数的Object将被XWork容器进行处理.
* object内部申明有@Inect的字段和方法,都将被注入收到容器托管的对象,
* 从而建立起依赖关系
*
*/
void inject(Object o);
/**
* Creates and injects a new instance of type {@code implementation}.
* 创建一个类的实例并进行对象依赖注入
*/
public <T> T inject(Class<T> implementation);
/**
* Gets an instance of the given dependency which was declared in
* {@link com.opensymphony.xwork2.inject.ContainerBuilder}.
* 根据类和名称获得实例
*/
public <T> T getInstance(Class<T> type, String name);
/**
* Convenience method. Equivalent to {@code getInstance(type,
* DEFAULT_NAME)}.
* 根据默认的DEFAULT_NAME来获得实例
*/
<T> T getInstance(Class<T> type);
/**
* Gets a set of all registered names for the given type
* @param type The instance type
* @return A set of registered names or empty set if no instances are registered for that type
* 根据给出的type来获得所有注册的name
*/
Set<String> getInstanceNames(Class<?> type);
/**
* Sets the scope strategy for the current thread.
*/
void setScopeStrategy(Scope.Strategy scopeStrategy);
/**
* Removes the scope strategy for the current thread.
*/
void removeScopeStrategy();
}
Container接口提供了对象的创建规范.根据type和name(由struts2.xml配置文件定义) 来形成映射通过反射来创建具体的实例对象.
下面将展示xWork的具体实现细节ContainerImpl.java
class ContainerImpl implements Container
{
//此factories对象中key是由Key对象,value是InternalFactory构成的键值对形式来组成,
//Key对象中存储着type,name信息,是从配置文件中或者注解方式的属性对
//而InternalFactory则是创建对象的内部工厂,
//那么我们就知道这个factories是存储bean的type,name信息和创建的方式.
final Map<Key<?>, InternalFactory<?>> factories;
final Map<Class<?>, Set<String>> factoryNamesByType;
//构造方法
ContainerImpl(Map<Key<?>, InternalFactory<?>> factories)
{
//对factories进行赋值
this.factories = factories;
Map<Class<?>, Set<String>> map = new HashMap<Class<?>, Set<String>>();
//整个for循环的过程就是将factories中存储的key对象以type和name遍历到factoryNamesByType中
for (Key<?> key : factories.keySet())
{
Set<String> names = map.get(key.getType());
if (names == null)
{
names = new HashSet<String>();
map.put(key.getType(), names);
}
names.add(key.getName());
}
for (Entry<Class<?>, Set<String>> entry : map.entrySet())
{
entry.setValue(Collections.unmodifiableSet(entry.getValue()));
}
this.factoryNamesByType = Collections.unmodifiableMap(map);
}
//此处省略部分代码...
//注入器(作用是创建对象时,起数据流作用,驱动bean工厂的执行)
void addInjectors(Class clazz, List<Injector> injectors)
{
if (clazz == Object.class)
{
return;
}
// Add injectors for superclass first.
//首先递归遍历所有的父类存储到injectors
addInjectors(clazz.getSuperclass(), injectors);
// TODO (crazybob): Filter out overridden members.
//然后将所有的字段添加到注入器中
addInjectorsForFields(clazz.getDeclaredFields(), false, injectors);
//将所有的方法添加到注入器中存储
addInjectorsForMethods(clazz.getDeclaredMethods(), false, injectors);
}
<pre name="code" class="html"> //此处省略部分代码...
此factories中的key.java源码如下:
class Key<T> {
final Class<T> type;
final String name;
final int hashCode;
private Key(Class<T> type, String name) {
if (type == null) {
throw new NullPointerException("Type is null.");
}
if (name == null) {
throw new NullPointerException("Name is null.");
}
this.type = type;
this.name = name;
hashCode = type.hashCode() * 31 + name.hashCode();
}
Class<T> getType() {
return type;
}
String getName() {
return name;
}
@Override
public int hashCode() {
return hashCode;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Key)) {
return false;
}
if (o == this) {
return true;
}
Key other = (Key) o;
return name.equals(other.name) && type.equals(other.type);
}
@Override
public String toString() {
return "[type=" + type.getName() + ", name='" + name + "']";
}
static <T> Key<T> newInstance(Class<T> type, String name) {
return new Key<T>(type, name);
}
}
此key中存储的type和name对应于配置文件中如下:
<!DOCTYPE struts (View Source for full doctype...)>
- <struts>
<bean class="com.opensymphony.xwork2.ObjectFactory" name="struts" />
<bean type="com.opensymphony.xwork2.factory.ResultFactory" name="struts" class="org.apache.struts2.factory.StrutsResultFactory" />
<bean type="com.opensymphony.xwork2.factory.ActionFactory" name="struts" class="com.opensymphony.xwork2.factory.DefaultActionFactory" />
<bean type="com.opensymphony.xwork2.factory.ConverterFactory" name="struts" class="com.opensymphony.xwork2.factory.DefaultConverterFactory" />
<bean type="com.opensymphony.xwork2.factory.InterceptorFactory" name="struts" class="com.opensymphony.xwork2.factory.DefaultInterceptorFactory" />
<bean type="com.opensymphony.xwork2.factory.ValidatorFactory" name="struts" class="com.opensymphony.xwork2.factory.DefaultValidatorFactory" />
<bean type="com.opensymphony.xwork2.FileManager" class="com.opensymphony.xwork2.util.fs.DefaultFileManager" name="system" scope="singleton" />
<bean type="com.opensymphony.xwork2.FileManagerFactory" class="com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory" name="struts" scope="singleton" />
<bean type="com.opensymphony.xwork2.ActionProxyFactory" name="struts" class="org.apache.struts2.impl.StrutsActionProxyFactory" />
<bean type="com.opensymphony.xwork2.ActionProxyFactory" name="prefix" class="org.apache.struts2.impl.PrefixBasedActionProxyFactory" />
<bean type="com.opensymphony.xwork2.conversion.ObjectTypeDeterminer" name="struts" class="com.opensymphony.xwork2.conversion.impl.DefaultObjectTypeDeterminer" />
<bean type="com.opensymphony.xwork2.util.PatternMatcher" name="struts" class="com.opensymphony.xwork2.util.WildcardHelper" />
<bean type="com.opensymphony.xwork2.util.PatternMatcher" name="namedVariable" class="com.opensymphony.xwork2.util.NamedVariablePatternMatcher" />
struts2通过builder工厂加载配置文件信息,然后生成factories,此对象中的key就是每一个bean节点的type和name,此InternalFactory则存储着改bean的创建方式.正所谓"授人以鱼不如授人以渔"把bean的创建方法存储可以更灵活,更高校的创建对象.
而Struts2中的依赖注入正是由ContainerImpl中注入器这一核心技术来实现.factories和injector相辅相成.由injector来记录各个bean对象之间的关联关系(依赖),然后由factories来记录各个bean对象的构造方式.