You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by Apache Wiki <wi...@apache.org> on 2007/12/10 23:40:19 UTC
[Tapestry Wiki] Update of "Tapestry5HowToIocOnly" by DavorHrg
Dear Wiki user,
You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for change notification.
The following page has been changed by DavorHrg:
http://wiki.apache.org/tapestry/Tapestry5HowToIocOnly
New page:
Here is a simple example on how to use only tapestry-ioc in your app.[[BR]]
Using IOC has many advantages, so why not get used to it even in the smallest apps.
if you are comfortable with maven: pom.xml
{{{
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>tapestry.mini-app</groupId>
<artifactId>mini-app</artifactId>
<version>0.0.1</version>
<build>
<finalName>mini-app</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<optimize>true</optimize>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.tapestry</groupId>
<artifactId>tapestry-ioc</artifactId>
<version>${tapestry-release-version}</version>
</dependency>
</dependencies>
<properties>
<tapestry-release-version>5.0.6</tapestry-release-version>
</properties>
</project>
}}}
or add these libraries to your path
{{{
javassist-3.4.ga.jar
log4j-1.2.14.jar
tapestry-ioc-5.0.5.jar
slf4j-api-1.4.3.jar
slf4j-log4j12-1.4.3.jar
}}}
Your main class is fairly simple(Main.java)
{{{#!java
package tapestry.mini;
import org.apache.tapestry.ioc.Registry;
import org.apache.tapestry.ioc.RegistryBuilder;
import tapestry.mini.services.Hello;
import tapestry.mini.services.MiniAppModule;
public class Main {
public static void main(String[] args) {
RegistryBuilder builder = new RegistryBuilder();
builder.add(MiniAppModule.class);
Registry registry = builder.build();
registry.performRegistryStartup();
Hello hello = registry.getService(Hello.class);
hello.sayHello();
}
}
}}}
!MiniAppModule.java:
{{{#!java
package tapestry.mini.services;
import org.apache.tapestry.ioc.ServiceBinder;
public class MiniAppModule {
public static void bind(ServiceBinder binder){
binder.bind(Hello.class);
binder.bind(Output.class, OutputImpl.class);
}
}
}}}
A small class that also shows automatic dependency injection: Hello.java
{{{#!java
package tapestry.mini.services;
public class Hello {
private final Output _output;
public Hello(Output output) {
_output = output;
}
public void sayHello(){
_output.say("Hello world");
}
}
}}}
a very simple dependancy: Output.java
{{{#!java
package tapestry.mini.services;
public interface Output {
public void say(String text);
}
}}}
A simple implementation of course: !OutputImpl.java
{{{#!java
package tapestry.mini.services;
public class OutputImpl implements Output{
public void say(String text){
System.out.println(text);
}
}
}}}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tapestry.apache.org
For additional commands, e-mail: dev-help@tapestry.apache.org