本文是<
在进行面向对象编程的时候,我们不可避免地要使用继承实现等等java提供的语法支持。但是复杂的对象关系也为对象生命周期的管理带来了至少以下两个问题。
1 程序运行时,应如何双肩我们所需要的对象。public class Person{
private Car car;
public Person(Car c){
this.car=c;
}
public void drive(){
car.drive();
}
}
上面的例子很简单吧,一个人能开车。对于车的产生,需要person自己来创造。
public class Person{
private Car car;
public Person(){
//其他代码
}
public void setCar(Car c){
this.car=c;
}
public void drive(){
car.drive();
}
}
就这么简单
public interface Container extends Serializable {
/**
* Default dependency name.
*/
String DEFAULT_NAME = "default";
/**
* 创建类的实例,并进行注入 在上面说的person与car的例子中
* injcet的参数就应该是person
* object内部声明有@inject的字段和方法都将被注
* 入容器所托管的对象(例如上面说的car)
*/
void inject(Object o);
T inject(Class implementation);
/**
* 根据type与name获得容器中的java类实例
*/
T getInstance(Class type, String name);
/**
* 根据type和默认的name(default)获得容器中的java类实例
*/
T getInstance(Class type);
/**
* 根据type获得所有注册这个type的name
*/
Set 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();
}
接口中的方法可以分为三类
public class Person{
@Inject
private Car car;
public Person(){
//其他代码
}
@Inject
public void setCar(Car c){
this.car=c;
}
public void drive(){
car.drive();
}
}
在person里面的car参数或者setCar方法上加上 @Inject标签
@Target({METHOD, CONSTRUCTOR, FIELD, PARAMETER})
@Retention(RUNTIME)
public @interface Inject {
/**
* Dependency name. Defaults to {@link Container。DEFAULT_NAME}.
*/
String value() default DEFAULT_NAME;
/**
* Whether or not injection is required. Applicable only to methods and
* fields (not constructors or parameters).
*/
boolean required() default true;
}
public class DefaultUnknownHandlerManager implements UnknownHandlerManager {
protected ArrayList unknownHandlers;
private Configuration configuration;
private Container container;
@Inject
public void setConfiguration(Configuration configuration) {
this.configuration = configuration;
build();
}
@Inject
public void setContainer(Container container) {
this.container = container;
build();
}
/**
* Builds a list of UnknowHandlers in the order specified by the configured "unknown-handler-stack".
* If "unknown-handler-stack" was not configured, all UnknowHandlers will be returned, in no specific order
*/
protected void build() {
if (configuration != null && container != null) {
List unkownHandlerStack = configuration.getUnknownHandlerStack();
unknownHandlers = new ArrayList();
if (unkownHandlerStack != null && !unkownHandlerStack.isEmpty()) {
//get UnknownHandlers in the specified order
// 根据一定顺序获取UnknownHandler实例
for (UnknownHandlerConfig unknownHandlerConfig : unkownHandlerStack) {
//通过容器的getInstance方法获得容器内部脱光光的type为UnknownHandler.class
//name为传入的参数的UnknownHandler
UnknownHandler uh = container.getInstance(UnknownHandler.class, unknownHandlerConfig.getName());
unknownHandlers.add(uh);
}
} else {
//add all available UnknownHandlers
Set unknowHandlerNames = container.getInstanceNames(UnknownHandler.class);
for (String unknowHandlerName : unknowHandlerNames) {
UnknownHandler uh = container.getInstance(UnknownHandler.class, unknowHandlerName);
unknownHandlers.add(uh);
}
}
}
}
.....
}
public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable {
private TextProvider getTextProvider() {
if (textProvider == null) {
TextProviderFactory tpf = new TextProviderFactory();
if (container != null) {
container.inject(tpf);
}
textProvider = tpf.createInstance(getClass(), this);
}
return textProvider;
}
@Inject
public void setContainer(Container container) {
this.container = container;
}
}
我们在下面可以看到container.inject(tpf)的参数,也就是TextProviderFactory里面有一个 @Inject标签。
public class TextProviderFactory {
private TextProvider textProvider;
@Inject
public void setTextProvider(TextProvider textProvider) {
this.textProvider = textProvider;
}
.....
}
感谢glt