RE: [picocontainer-dev] comments on injection
"Putrycz, Erik" <[email protected]>
| Newsgroups | gmane.comp.java.picocontainer.devel |
|---|---|
| Message-ID | <[email protected]> |
Ok here we go!
The locator interface can allow to easily implement things like
@ImplementedBy (no need to panic, it's not in the patch) and also
dynamic registration (a component not found is directly added to the
container). I find this very handy. This doesn't affect pico's behavior
by default.
MutablePicoContainer pico = new
DefaultPicoContainer();
pico.addLocator(new
DynamicRegistrationLocator());
LinkedList<String> list =
pico.getComponent(LinkedList.class);
assertNotNull(list);
I have in my code also a @NonInstantiable annotation so that pico can
throw an exception if it tries to create one of these annotated classes.
Another small change, I updated junit to 4 so that you can use the
annotations.
Erik Putrycz, Ph.D - Research Associate / [email protected]
<mailto:[email protected]> / (613) 990 0681
Institute for Information Technology - Software Engineering Group
National Research Council, Canada - Building M-50, 1200 Montreal Road
Ottawa, Ontario, CANADA K1A 0R6
________________________________
From: Paul Hammant [mailto:[email protected]]
Sent: May 31, 2007 16:24
To: dev-qxt/[email protected]
Subject: Re: [picocontainer-dev] comments on injection
Give us patches dude, for both member injection and hooks - you mean
@implementedBy(...) right ?
-ph
ps - be aware there are renames going on presently.
For one, I'm killing org.picocontainer.defaults.* class by class. Either
moving them to elsewhere or deleting them as they've proven after four
years to not be need at the current level of type-safety (exceptions)
On May 31, 2007, at 11:48 AM, Putrycz, Erik wrote:
I moved most of my project to pico 2 and was hoping to use the new
injection but currently the only injection available is on methods...
I'm not very keen on the method injection, it creates IMO often too much
verbose code whereas a member injection is quicker to write. Often I
don't even need a getter and setter...
However I think both would be nice to have.
Any objection on adding member injection?
I also think I figured out a way to inject members of an existing object
without modifying the container. I'll see how that turns out.
Last comment... I know this is very unpopular here but I'd be nice if
there was some kind of hook in Pico to be able to setup auto
registration of classes. Somekind of callback on getComponent - when not
found - would be nice.
---------------------------------------------------------------------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email
Pico2-locator.patch
(application/octet-stream, 15.7 KB)
Index: /home/putrycze/workspaces/workspace-cobol/Pico2/container/pom.xml
===================================================================
--- /home/putrycze/workspaces/workspace-cobol/Pico2/container/pom.xml (revision 3459)
+++ /home/putrycze/workspaces/workspace-cobol/Pico2/container/pom.xml (working copy)
@@ -26,6 +26,7 @@
<plugin>
<groupId>com.thoughtworks.paranamer</groupId>
<artifactId>paranamer-maven-plugin</artifactId>
+
<configuration>
<sourceDirectory>src/test</sourceDirectory>
<outputDirectory>target/test-classes</outputDirectory>
Index: /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/containers/AbstractDelegatingMutablePicoContainer.java
===================================================================
--- /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/containers/AbstractDelegatingMutablePicoContainer.java (revision 3459)
+++ /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/containers/AbstractDelegatingMutablePicoContainer.java (working copy)
@@ -11,6 +11,7 @@
import org.picocontainer.ComponentAdapter;
import org.picocontainer.ComponentCharacteristic;
+import org.picocontainer.ComponentLocator;
import org.picocontainer.MutablePicoContainer;
import org.picocontainer.Parameter;
import org.picocontainer.PicoContainer;
@@ -28,7 +29,7 @@
*/
public abstract class AbstractDelegatingMutablePicoContainer implements MutablePicoContainer, Serializable {
- private MutablePicoContainer delegate;
+ private MutablePicoContainer delegate;
public AbstractDelegatingMutablePicoContainer(MutablePicoContainer delegate) {
this.delegate = delegate;
@@ -137,4 +138,9 @@
public MutablePicoContainer as(ComponentCharacteristic... characteristics) {
return delegate.as(characteristics);
}
+
+ public void addLocator(ComponentLocator locator) {
+ delegate.addLocator(locator);
+ }
+
}
Index: /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/defaults/DefaultPicoContainer.java
===================================================================
--- /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/defaults/DefaultPicoContainer.java (revision 3459)
+++ /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/defaults/DefaultPicoContainer.java (working copy)
@@ -12,6 +12,7 @@
import org.picocontainer.ComponentAdapter;
import org.picocontainer.ComponentCharacteristic;
import org.picocontainer.ComponentFactory;
+import org.picocontainer.ComponentLocator;
import org.picocontainer.ComponentMonitor;
import org.picocontainer.ComponentMonitorStrategy;
import org.picocontainer.Disposable;
@@ -90,6 +91,8 @@
private boolean disposed = false;
// Keeps track of child containers started status
private Set<Integer> childrenStarted = new HashSet<Integer>();
+
+ private List<ComponentLocator> locators = new ArrayList<ComponentLocator>();
private LifecycleManager lifecycleManager = new OrderedComponentAdapterLifecycleManager();
private LifecycleStrategy lifecycleStrategy;
@@ -278,6 +281,12 @@
if (componentType == null) {
return Collections.emptyList();
}
+ // try to process the locator
+ for (ComponentLocator locator:locators) {
+ if (locator.locateComponentType(componentType, this))
+ break;
+ }
+
List<ComponentAdapter<T>> found = new ArrayList<ComponentAdapter<T>>();
for (ComponentAdapter<?> componentAdapter : getComponentAdapters()) {
if (componentType.isAssignableFrom(componentAdapter.getComponentImplementation())) {
@@ -804,5 +813,10 @@
}
}
+ public void addLocator(ComponentLocator locator) {
+ locators.add(locator);
+
+ }
+
}
Index: /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/Inject.java
===================================================================
--- /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/Inject.java (revision 3459)
+++ /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/Inject.java (working copy)
@@ -6,6 +6,6 @@
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
-@Target(ElementType.METHOD)
+@Target(value={ElementType.METHOD,ElementType.FIELD})
public @interface Inject {
}
Index: /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/MutablePicoContainer.java
===================================================================
--- /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/MutablePicoContainer.java (revision 3459)
+++ /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/MutablePicoContainer.java (working copy)
@@ -170,4 +170,11 @@
*/
MutablePicoContainer as(ComponentCharacteristic... characteristics);
+ /**
+ * If a component has not been found, then locators have a chance to provide mecanisms to locate it
+ *
+ * @param locator
+ */
+ void addLocator(ComponentLocator locator);
+
}
Index: /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/test/org/picocontainer/PicoBuilderTestCase.java
===================================================================
--- /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/test/org/picocontainer/PicoBuilderTestCase.java (revision 3459)
+++ /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/test/org/picocontainer/PicoBuilderTestCase.java (working copy)
@@ -58,6 +58,7 @@
" cdiDelegate\n" +
" sdiDelegate\n" +
" parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+ " locators\n" +
" lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
" componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
"PICO",foo);
@@ -71,6 +72,7 @@
" cdiDelegate\n" +
" sdiDelegate\n" +
" parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+ " locators\n" +
" lifecycleStrategy=org.picocontainer.lifecycle.StartableLifecycleStrategy\n" +
" componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
" lifecycleStrategy\n" +
@@ -86,6 +88,7 @@
" cdiDelegate\n" +
" sdiDelegate\n" +
" parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+ " locators\n" +
" lifecycleStrategy=org.picocontainer.lifecycle.ReflectionLifecycleStrategy\n" +
" methodNames\n" +
" stringstartstring\n" +
@@ -107,6 +110,7 @@
" cdiDelegate\n" +
" sdiDelegate\n" +
" parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+ " locators\n" +
" lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
" componentMonitor=org.picocontainer.monitors.ConsoleComponentMonitor\n" +
" delegate=org.picocontainer.monitors.NullComponentMonitor\n" +
@@ -122,6 +126,7 @@
" cdiDelegate\n" +
" sdiDelegate\n" +
" parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+ " locators\n" +
" lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
" componentMonitor=org.picocontainer.monitors.ConsoleComponentMonitor\n" +
" delegate=org.picocontainer.monitors.NullComponentMonitor\n" +
@@ -147,6 +152,7 @@
" cdiDelegate\n" +
" sdiDelegate\n" +
" parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+ " locators\n" +
" lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
" componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
"PICO",foo);
@@ -161,6 +167,7 @@
" cdiDelegate\n" +
" sdiDelegate\n" +
" parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+ " locators\n" +
" lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
" componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
"PICO",foo);
@@ -175,6 +182,7 @@
" delegate=org.picocontainer.adapters.ImplementationHidingBehaviorFactory\n" +
" delegate=org.picocontainer.adapters.SetterInjectionFactory\n" +
" parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+ " locators\n" +
" lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
" componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
"PICO",foo);
@@ -191,6 +199,7 @@
" cdiDelegate\n" +
" sdiDelegate\n" +
" parent=org.picocontainer.PicoBuilderTestCase_CustomParentcontainer\n" +
+ " locators\n" +
" lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
" componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
"PICO",foo);
@@ -212,6 +221,7 @@
assertEquals("PICO\n" +
" componentAdapterFactory=org.picocontainer.adapters.SetterInjectionFactory\n" +
" parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+ " locators\n" +
" lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
" componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
"PICO",foo);
@@ -223,6 +233,7 @@
assertEquals("PICO\n" +
" componentAdapterFactory=org.picocontainer.adapters.AnnotationInjectionFactory\n" +
" parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+ " locators\n" +
" lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
" componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
"PICO",foo);
@@ -234,6 +245,7 @@
assertEquals("PICO\n" +
" componentAdapterFactory=org.picocontainer.adapters.ConstructorInjectionFactory\n" +
" parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+ " locators\n" +
" lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
" componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
"PICO",foo);
@@ -246,6 +258,7 @@
" componentAdapterFactory=org.picocontainer.adapters.ImplementationHidingBehaviorFactory\n" +
" delegate=org.picocontainer.adapters.SetterInjectionFactory\n" +
" parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+ " locators\n" +
" lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
" componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
"PICO",foo);
@@ -259,6 +272,7 @@
" delegate=org.picocontainer.adapters.ImplementationHidingBehaviorFactory\n" +
" delegate=org.picocontainer.adapters.SetterInjectionFactory\n" +
" parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+ " locators\n" +
" lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
" componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
"PICO",foo);
@@ -273,6 +287,7 @@
" cdiDelegate\n" +
" sdiDelegate\n" +
" parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+ " locators\n" +
" lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
" componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
"PICO",foo);
@@ -286,6 +301,7 @@
" cdiDelegate\n" +
" sdiDelegate\n" +
" parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+ " locators\n" +
" lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
" componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
"org.picocontainer.PicoBuilderTestCase_-TestPicoContainer",foo);
Index: /home/putrycze/workspaces/workspace-cobol/Pico2/gems/src/java/org/picocontainer/gems/containers/CommonsLoggingTracingContainerDecorator.java
===================================================================
--- /home/putrycze/workspaces/workspace-cobol/Pico2/gems/src/java/org/picocontainer/gems/containers/CommonsLoggingTracingContainerDecorator.java (revision 3459)
+++ /home/putrycze/workspaces/workspace-cobol/Pico2/gems/src/java/org/picocontainer/gems/containers/CommonsLoggingTracingContainerDecorator.java (working copy)
@@ -2,6 +2,7 @@
import org.picocontainer.ComponentAdapter;
import org.picocontainer.ComponentCharacteristic;
+import org.picocontainer.ComponentLocator;
import org.picocontainer.MutablePicoContainer;
import org.picocontainer.Parameter;
import org.picocontainer.PicoContainer;
@@ -462,4 +463,9 @@
public MutablePicoContainer as(ComponentCharacteristic... characteristics) {
return delegate.as(characteristics);
}
+
+
+ public void addLocator(ComponentLocator locator) {
+ delegate.addLocator(locator);
+ }
}
Index: /home/putrycze/workspaces/workspace-cobol/Pico2/gems/src/java/org/picocontainer/gems/containers/Log4jTracingContainerDecorator.java
===================================================================
--- /home/putrycze/workspaces/workspace-cobol/Pico2/gems/src/java/org/picocontainer/gems/containers/Log4jTracingContainerDecorator.java (revision 3459)
+++ /home/putrycze/workspaces/workspace-cobol/Pico2/gems/src/java/org/picocontainer/gems/containers/Log4jTracingContainerDecorator.java (working copy)
@@ -12,6 +12,7 @@
import org.picocontainer.ComponentAdapter;
import org.picocontainer.ComponentCharacteristic;
+import org.picocontainer.ComponentLocator;
import org.picocontainer.MutablePicoContainer;
import org.picocontainer.Parameter;
import org.picocontainer.PicoContainer;
@@ -523,4 +524,8 @@
public MutablePicoContainer as(ComponentCharacteristic... characteristics) {
return delegate.as(characteristics);
}
+
+ public void addLocator(ComponentLocator locator) {
+ delegate.addLocator(locator);
+ }
}
Index: /home/putrycze/workspaces/workspace-cobol/Pico2/pom.xml
===================================================================
--- /home/putrycze/workspaces/workspace-cobol/Pico2/pom.xml (revision 3459)
+++ /home/putrycze/workspaces/workspace-cobol/Pico2/pom.xml (working copy)
@@ -141,7 +141,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
- <version>3.8.1</version>
+ <version>4.0</version>
</dependency>
<dependency>
<groupId>jmock</groupId>