MentaContainer

Compare Revisions

Ignore whitespace Rev 70 → Rev 69

/trunk/src/test/java/org/mentacontainer/impl/InstanceComponentTest.java
File deleted
\ No newline at end of file
/trunk/src/test/java/org/mentacontainer/impl/MentaContainerTest.java
131,43 → 131,6
}
@Test
public void testScopes() {
Container c = new MentaContainer();
// if you don't provide a scope the Scope is NONE
// meaning a new instance is created on every
// request for the component
c.ioc("myString", String.class).addInitValue("saoj");
String s1 = c.get("myString");
String s2 = c.get("myString");
Assert.assertTrue(s1 != s2);
// then you can use SINGLETON
// always get the same instance no matter what
c.ioc("myString", String.class, Scope.SINGLETON).addInitValue("saoj");
s1 = c.get("myString");
s2 = c.get("myString");
Assert.assertTrue(s1 == s2);
// then you can use THREAD
// each thread will get a different instance
c.ioc("myString", String.class, Scope.THREAD).addInitValue("saoj");
s1 = c.get("myString");
s2 = c.get("myString");
Assert.assertTrue(s1 == s2); // same thread
}
@Test
public void testThreadLocal() throws Exception {
final Container c = new MentaContainer();
/trunk/src/main/java/org/mentacontainer/Container.java
131,8 → 131,7
/**
* Clear all cached instances for that scope. If you have a thread pool for example you will
* have to clear the THREAD scope when your thread is returned to the pool. It does not make
* sense to clear a NONE scope (the method returns doing nothing).
* have to clear the THREAD scope when your thread is returned to the pool.
*
* @param scope The scope to be cleared.
*/
142,7 → 141,7
* Clear a single key from cache and return the instance that was cached.
*
* @param key The key representing the bean inside the container.
* @return The value that was cached and it is not anymore (was cleared) or null if nothing was cleared
* @return The value that was cached and it is not anymore (was cleared)
*/
public <T> T clear(String key);
}
/trunk/src/main/java/org/mentacontainer/Clearable.java
1,24 → 1,5
package org.mentacontainer;
 
/**
* Some components can also implement this interface to perform some cleanup
* when the instance is cleared. For example, a connection pool will want
* to know when the connection instance is cleared so it can return it to
* the pool.
*
* It makes more sense to use this interface for components that will be placed
* in the THREAD scope, but you can also use it with components in the SINGLETON
* scope.
*
* This is particular useful for the THREAD scope for dealing with thread pools, so
* when the thread is returned to the thread pool you will want to clear the THREAD
* scope. That's pretty much how web containers work: one thread per request coming from
* a thread pool.
*
* @author sergio.oliveira.jr@gmail.com
*
* @param <E>
*/
public interface Clearable<E> {
public void onCleared(E clearedObject);