订阅所有JSP/Servlet的日志 订阅 | 这是最新一篇日志 上一篇 | 下一篇日志 下一篇 ]
java

Jmin IOC非文件配置使用例子

组件容器,个人理解是将一些组件的相关描述信息导入容器,并由容器对组件进行管理.
下面一个使用例子,直接介绍接口的使用/
第1步:准备一个Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Copyright (c) jmin Organization. All rights reserved.
 */
package org.jmin.j2ee.ioc.test;
 
/**
 * A bean class for IOC test.
 *
 * @author Chris Liao
 */
public class Man implements java.io.Serializable{
 
  /**
   * name
   */
  private String name;
 
  /**
   * age
   */
  private int age;
 
  /**
   * Default constructor
   */
  public Man(String name,int age){
    this.name=name;
    this.age =age;
  }
 
  /**
   * Return man age
   */
  public int getAge() {
    return age;
  }
 
  /**
   * set Age
   */
  public void setAge(int age) {
    this.age = age;
  }
 
  /**
   * return name
   */
  public String getName()throws Throwable {
    return name;
  }
 
  /**
   * Set name
   */
  public void setName(String name) {
    this.name = name;
  }
 
  /**
   * Print hello
   * @param target
   */
  public void sayHello(String target){
    System.out.println("Hello,"+ target);
  }
 
  /**
   * destroy method
   */
  public void destroy(){
    System.out.println("destroy");
  }
 
  /**
   * Static factory method to create a man
   */
  public static Man create(String name,int age){
    return new Man(name,age);
  }
}


第2步:编写一个拦截器的实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
*Copyright (c) jmin Organization. All rights reserved.
 */
package org.jmin.j2ee.ioc.test;
 
import org.jmin.j2ee.ioc.element.Interceptor;
 
/**
 * A interceptor class for IOC test
 *
 * @author Chris Liao
 */
 
public class ManInterceptor implements Interceptor{
 
  /**
   * intercept method
   */
  public void before(Object bean,String method,Object[] parameters){
    System.out.println("before");
  }
 
  /**
   * intercept method
   */
  public void returning(Object bean,String method,Object[] parameter,Object resultValue){
    System.out.println("returning: " +resultValue);
  }
 
  /**
   * intercept method
   */
  public void throwing(Object bean,String method,Object[] parameter,Throwable throwable){
    System.out.println("throwing");
  }
 
  /**
   * intercept method
   */
  public void after(Object bean,String method,Object[] parameter,Object result){
    System.out.println("fianlly: " + result);
  }
}



第3步:编写测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright (c) jmin Organization. All rights reserved.
 */
package org.jmin.j2ee.ioc.test;
 
import org.jmin.j2ee.ioc.BeanElementFactory;
import org.jmin.j2ee.ioc.IocContainer;
import org.jmin.j2ee.ioc.Parameter;
import org.jmin.j2ee.ioc.ParameterFactory;
import org.jmin.j2ee.ioc.element.Construction;
import org.jmin.j2ee.ioc.element.Interception;
import org.jmin.j2ee.ioc.element.Invocation;
import org.jmin.j2ee.ioc.element.Property;
import org.jmin.j2ee.ioc.impl.DefaultIocContainer;
 
/**
 * A IOC tester for Man class.Some bean information will be registered in IOC
 * container with bean class.
 *
 * @author Chris Liao
 * @version 1.0
 */
public class ManIocTest {
 
  /**
   * Registers Man class into ioc container,then get a instance of Man class
   */
  private Object registerMan() {
 
    /**
     * Create A IOC container
     */
    IocContainer container = new DefaultIocContainer();
 
    /**
     * Create primitive type ioc parameter
     */
    Parameter nameParmeter = ParameterFactory.createStringType("Chris");
 
    /**
     * Create primitive type ioc parameter
     */
    Parameter ageParmeter = ParameterFactory.createIntType(28);
 
    /**
     * Create a construnction of bean: Constructor Injection
     */
    Construction construction =
        BeanElementFactory.createConstruction(new Parameter[] {nameParmeter, ageParmeter});
 
    /**
     * Create a ioc property,which can macth set injection
     */
    Property nameProperty =
        BeanElementFactory.createProperty("name", nameParmeter);
 
    /**
     * Create a construnction of bean: set Injection
     */
    Property ageProperty =
        BeanElementFactory.createProperty("age", ageParmeter);
 
    /**
     * Create String type ioc parameter
     */
    Parameter helloParmeter = ParameterFactory.createStringType("China");
 
    /**
     * Crreate a Invocation applied in bean: Invocation injection
     */
    Invocation invocation =
        BeanElementFactory.createInvocation( "sayHello",new Parameter[] { helloParmeter });
 
    /**
     * create a Interception on bean
     */
    Interception interception =
        BeanElementFactory.createInterception("sayHello",new String[] {"java.lang.String" });
 
    /**
     * Add a interceptor on the interception
     */
    interception.addInterceptorByClass(ManInterceptor.class);
 
    /**
     * Register Bean class and some information
     */
    Object refKey =
        container.register(
        Man.class,
        construction,
        new Property[] { nameProperty, ageProperty },
        new Invocation[] { invocation });
 
    /**
     * Add a interception bean calss method: sayHello
     */
    container.addInterception(refKey, interception);
 
    /**
     * Return a bean instance from io container
     */
    return container.getBean(refKey);
 
  }
 
  /**
   * Entrance method to launch current application.
   */
  public static void main(String[] args) {
 
    /**
     * Create instance of current test class
     */
    ManIocTest test = new ManIocTest();
 
    /**
     * Register bean class and return a instance.
     */
    Object bean = test.registerMan();
 
    /**
     * Cast bean instance
     */
    Man me = (Man) bean;
  }
}



用Javac 命令编译这些代码,并运行它们,将会在屏幕上打印:
1
2
3
4
5
6
before
Hello,China
returning: null
fianlly: null
 

当然以上例子是在正确导入Jmin的IOC包才可以的.

如果您对此感兴趣.请联系jmin520@21cn.com

平均得分
(0 次评分)





文章来自: 本站原创
标签:
评论: 0 | 查看次数: 309
发表评论
昵 称:  登录
内 容:
选 项:
字数限制 1000 字 | UBB代码 开启 | [img]标签 开启