1 Introduction
minbox is a java micro-container, which can manage bean definition and supply bean instance for outside caller, then the developers needn't create object instance with 'new' keyword. The container is very easy to study, and fast development in java application.
2 Bean elements
Before creation, bean definition must be existed in container. Some descript object are used to define bean part definition and work as instruction to container, we call them as elements. Some elements are listed below
1:InstanceCreation
2:InstanceSingleton
3:InitializeMethodName
4:DestroyMethodName
5:InjectionProperty
6:InjectionInvocation
7:InvocationInterception
8:ParentReferenceId
An element factory can be used to create element object, for example code
BeanContainer container = new BeanContainerImpl ();
BeanElementFactory beanElementFactory =container.getBeanElementFactory ();
InitializeMethodName initMehtod=beanElementFactory.createInitializeMethod ("init");
3 Injection parameters
After bean instance created, container will inject parameters into them. There are four kinds’ Ioc parameters types; container will convert them to real value before injection.
1:Primitive type
2:Instance type
3:Refrence type
4:Container type
Also, container has a parameter factory to create Ioc parameter, for example code
BeanContainer container = new BeanContainerImpl ();
BeanParameterFactory beanParameterFactory =container.getBeanParameterFactory ();
Bean Parameter nameParmeter = beanParameterFactory.createStringParameter ("Chris");
4 How to call
Two ways for it.
1: Call directly container
2: Use xml context, the follow chapter will talk about it
5 Demo show
//A Java Bean
public class Man {
private String name;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
A:call directly container
//invocation code with container
import org.jmin.ioc.BeanContainer;
import org.jmin.ioc.BeanElementFactory;
import org.jmin.ioc.BeanParameter;
import org.jmin.ioc.BeanParameterFactory;
import org.jmin.ioc.element.InjectionProperty;
import org.jmin.ioc.impl.BeanContainerImpl;
public class PropertyCase{
public static void test()throws Throwable{
BeanContainer container = new BeanContainerImpl();
BeanElementFactory beanElementFactory =container.getBeanElementFactory();
BeanParameterFactory beanParameterFactory =container.getBeanParameterFactory();
BeanParameter nameParmeter = beanParameterFactory.createStringParameter("Chris");
BeanParameter ageParmeter = beanParameterFactory.createIntegerParameter(28);
InjectionProperty nameProperty = beanElementFactory.createInjectionProperty("name",nameParmeter);
InjectionProperty ageProperty = beanElementFactory.createInjectionProperty("age",ageParmeter);
container.registerClass("Bean1", Man.class, new InjectionProperty[] {nameProperty, ageProperty});
Man man = (Man)container.getBean("Bean1");
if(man!=null){
if("Chris".equals(man.getName()) && (28== man.getAge())){
System.out.println("[Container].........Porperty injection ok..........");
}else{
throw new Error("[Container]...........Porperty injection failed............");
}
}
}
public static void main(String[] args)throws Throwable{
test();
}
}
B:xml configeruation
<beans>
<bean id="user-content-Bean1" class="">
<property name="name" value="Chris">
<property name="age" value="28">
</property></property></bean>
</beans>
//invocation code with xml context
import org.jmin.ioc.impl.config.BeanContext;
import org.jmin.test.TestCase;
public class PropertyXMLCase{
public static void test()throws Throwable{
BeanContext context=new BeanContext("org/jmin/test/ioc/property/pojo.xml");
Man man = (Man)context.getBean("Bean1");
if(man!=null){
if("Chris".equals(man.getName()) && (28== man.getAge())){
System.out.println("[XML].........Porperty injection ok..........");
}else{
throw new Error("[XML]...........Porperty injection failed............");
}
}
}
public static void main(String[] args)throws Throwable{
test();
}
}
7:Source Download
http://dl.vmall.com/c0u7lix55w