picoContainer的字段注入

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
package test;

import static org.picocontainer.injectors.NamedFieldInjection.injectionFieldNames;

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;

import junit.framework.TestCase;

import org.picocontainer.DefaultPicoContainer;
import org.picocontainer.injectors.NamedFieldInjection;


public class TestAssignment
extends TestCase
{
static EntityManager em = Persistence.createEntityManagerFactory("x_system").createEntityManager();

DefaultPicoContainer pico = new DefaultPicoContainer(new NamedFieldInjection());


@Override
protected void setUp()
throws Exception
{
em.getTransaction().begin();
pico.addComponent(em);
}


@Override
protected void tearDown()
throws Exception
{
em.getTransaction().commit();
}


public void testBatchMark()
{
pico.as(injectionFieldNames("em")).addComponent(A.class);
pico.as(injectionFieldNames("em", "a")).addComponent(B.class);
pico.as(injectionFieldNames("em", "b")).addComponent(C.class);
C cBean = pico.getComponent(C.class);
Collection<X> xs = new ArrayList<X>();

cBean.someMethod("test", xs);
}
}