/** * Creates and initializes Petite container. It will be auto-magically configured by scanning the * classpath. Also, all 'app*.prop*' will be loaded and values will be injected in the matched * beans. At the end it registers this instance of core into the container. */ protected void startPetite() { log.info("petite initialization"); petite = createPetiteContainer(); log.info("app in web: " + Boolean.valueOf(isWebApplication)); if (isWebApplication == false) { // make session scope to act as singleton scope // if this is not a web application (and http session is not available). petite.registerScope(SessionScope.class, new SingletonScope()); } // load parameters from properties files petite.defineParameters(appProps); // adds a scanner bean, so it can be immediately configured from props petite.addBean(PETITE_SCAN, appScanner); // automagic configuration registerPetiteContainerBeans(petite); // add AppCore instance to Petite petite.addBean(PETITE_CORE, this); petite.addBean(PETITE_PROPS, appProps); }
@Test public void testAdd() { PetiteContainer pc = new PetiteContainer(); Foo foo = new Foo(); pc.addBean("foo", foo); Foo foo2 = (Foo) pc.getBean("foo"); assertNotNull(foo2); assertSame(foo, foo2); }
@Test public void testNoAdd2WithCircDep() { Foo.instanceCounter = 0; PetiteContainer pc = new PetiteContainer(); pc.registerBean(Foo.class); pc.registerBean(Zoo.class); pc.registerBean(Boo.class); Boo boo = (Boo) pc.getBean("boo"); Foo foo = (Foo) pc.getBean("foo"); Zoo zoo = (Zoo) pc.getBean("zoo"); assertNotNull(boo.getFoo()); assertSame(foo, boo.getFoo()); assertNotNull(zoo.boo); assertSame(boo, zoo.boo); assertSame(zoo, boo.zoo); Boo boo2 = (Boo) pc.getBean("boo"); assertNotNull(boo2); assertSame(boo, boo2); assertFalse(boo.orders.isEmpty()); assertNotNull(boo2.getFoo()); assertSame(foo, boo2.getFoo()); assertEquals(1, boo2.getFoo().hello()); assertEquals(1, boo2.getFoo().getCounter()); assertEquals("[first, second, third, init, beforeLast, last]", boo.orders.toString()); }
/** * Initializes database. First, creates connection pool. and transaction manager. Then, Jodds * DbOomManager is configured. It is also configured automagically, by scanning the class path for * entities. */ @SuppressWarnings("unchecked") protected void startDb() { if (!useDatabase) { log.info("database is not used"); return; } log.info("database initialization"); // connection pool petite.registerPetiteBean(CoreConnectionPool.class, PETITE_DBPOOL, null, null, false); connectionProvider = (ConnectionProvider) petite.getBean(PETITE_DBPOOL); connectionProvider.init(); // transactions manager jtxManager = createJtxTransactionManager(connectionProvider); jtxManager.setValidateExistingTransaction(true); AnnotationTxAdviceManager annTxAdviceManager = new AnnotationTxAdviceManager(jtxManager, jtxScopePattern); annTxAdviceManager.registerAnnotations(jtxAnnotations); AnnotationTxAdviceSupport.manager = annTxAdviceManager; DbSessionProvider sessionProvider = new DbJtxSessionProvider(jtxManager); // global settings DbManager dbManager = DbManager.getInstance(); dbManager.setConnectionProvider(connectionProvider); dbManager.setSessionProvider(sessionProvider); petite.addBean(PETITE_DB, dbManager); DbOomManager dbOomManager = DbOomManager.getInstance(); petite.addBean(PETITE_DBOOM, dbOomManager); // automatic configuration registerDbEntities(dbOomManager); }
@Before public void init() { petite = new PetiteContainer(); // load parameters Props appProps = new Props(); PropsUtil.loadFromClasspath(appProps, "/props.props"); petite.defineParameters(appProps); // add appCore to Petite (and resolve parameters) AutomagicPetiteConfigurator pcfg = new AutomagicPetiteConfigurator(); pcfg.setIncludedEntries(this.getClass().getPackage().getName() + ".*"); pcfg.configure(petite); }
/** * Injects all matching parameters to target instance. Matching parameters are named as given base * name. * * @param scopeData scope data is not used! */ public void injectContext(Target target, ScopeData[] scopeData, PetiteContainer madpc) { Class targetType = target.resolveType(); String baseName = targetType.getName(); ParamManager madvocPetiteParamManager = madpc.getParamManager(); String[] params = madvocPetiteParamManager.resolve(baseName, true); for (String param : params) { Object value = madvocPetiteParamManager.get(param); String propertyName = param.substring(baseName.length() + 1); target.writeValue(propertyName, value, madvocConfig.isInjectionErrorThrowsException()); } }
@Test public void testAdd2WithCircDep() { Foo.instanceCounter = 0; PetiteContainer pc = new PetiteContainer(); pc.registerBean(Foo.class); pc.registerBean(Zoo.class); Foo foo = (Foo) pc.getBean("foo"); Boo boo = new Boo(); assertNull(boo.getFoo()); pc.addBean("boo", boo, null); assertNotNull(boo.getFoo()); assertSame(foo, boo.getFoo()); assertNotNull(boo.zoo); Zoo zoo = (Zoo) pc.getBean("zoo"); assertNotNull(zoo.boo); assertSame(zoo, boo.zoo); // circular dependecy assertSame(boo, zoo.boo); Boo boo2 = (Boo) pc.getBean("boo"); assertNotNull(boo2); assertSame(boo, boo2); assertFalse(boo.orders.isEmpty()); assertEquals(6, boo.orders.size()); assertEquals("[first, second, third, init, beforeLast, last]", boo.orders.toString()); assertNotNull(boo2.getFoo()); assertSame(foo, boo2.getFoo()); assertEquals(1, boo2.getFoo().hello()); assertEquals(1, boo2.getFoo().getCounter()); pc.addBean("boo", boo); boo2 = (Boo) pc.getBean("boo"); assertNotNull(boo2); assertSame(boo, boo2); assertNotNull(boo2.getFoo()); assertSame(foo, boo2.getFoo()); assertEquals(1, boo2.getFoo().hello()); assertEquals(2, boo2.getFoo().getCounter()); assertEquals(12, boo.orders.size()); // init methods are called again due to re-add }
@Test public void testTwo() { PetiteContainer pc = new PetiteContainer(); pc.registerBean(DefaultBizImpl.class); assertEquals(1, pc.getTotalBeans()); Object bizI = pc.getBean("biz"); assertTrue(bizI instanceof Biz); assertFalse(bizI instanceof DefaultBiz); assertTrue(bizI instanceof DefaultBizImpl); // pc = new PetiteContainer(); // same container!!! pc.registerBean(DefaultBiz.class); // override! instance will be removed from the scope assertEquals(1, pc.getTotalBeans()); bizI = pc.getBean("biz"); assertTrue(bizI instanceof Biz); assertFalse(bizI instanceof DefaultBizImpl); assertTrue(bizI instanceof DefaultBiz); }
public <T> T getBeen(Class<T> type) { return petite.getBean(type); }
/** Starts with defining injection points (i.e. wiring) for existing bean. */ public BeanWire wire(String beanName) { petiteContainer.lookupExistingBeanDefinition(beanName); return new BeanWire(beanName); }
@Test public void testOne() { PetiteContainer pc = new PetiteContainer(); pc.registerBean(DefaultBizImpl.class); assertEquals(1, pc.getTotalBeans()); Object bizI = pc.getBean("biz"); assertTrue(bizI instanceof Biz); assertTrue(bizI instanceof DefaultBizImpl); pc = new PetiteContainer(); pc.registerBean(DefaultBizImpl.class); pc.registerBean(DefaultBiz.class); // override! assertEquals(1, pc.getTotalBeans()); pc.registerBean(Foo.class); pc.registerPropertyInjectionPoint("biz", "foo"); pc.registerInitMethods("biz", "init", "init2"); assertEquals(2, pc.getTotalBeans()); bizI = pc.getBean("biz"); assertTrue(bizI instanceof Biz); assertFalse(bizI instanceof DefaultBizImpl); assertTrue(bizI instanceof DefaultBiz); assertNotNull(((DefaultBiz) bizI).getFoo()); assertEquals(2, ((DefaultBiz) bizI).initCount); }
/** Initializes business part of the application. Simply delegates to {@link AppInit#init()}. */ protected void startApp() { appInit = (AppInit) petite.getBean(PETITE_INIT); if (appInit != null) { appInit.init(); } }