source: mini_app.zip(8kB)
Here is a simple example on how to use only tapestry-ioc in your app.
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)
1 package tapestry.mini;
2
3 import org.apache.tapestry.ioc.Registry;
4 import org.apache.tapestry.ioc.RegistryBuilder;
5
6 import tapestry.mini.services.Hello;
7 import tapestry.mini.services.MiniAppModule;
8
9 public class Main {
10
11 public static void main(String[] args) {
12 RegistryBuilder builder = new RegistryBuilder();
13 builder.add(MiniAppModule.class);
14
15 Registry registry = builder.build();
16 registry.performRegistryStartup();
17
18
19 Hello hello = registry.getService(Hello.class);
20 hello.sayHello();
21
22 //for operations done from this thread
23 registry.cleanupThread();
24 //call this to allow services clean shutdown
25 registry.shutdown();
26 }
27 }
MiniAppModule.java:
1 package tapestry.mini.services;
2
3 import org.apache.tapestry.ioc.ServiceBinder;
4
5 public class MiniAppModule {
6
7 public static void bind(ServiceBinder binder){
8 binder.bind(Hello.class);
9 binder.bind(Output.class, OutputImpl.class);
10 }
11 }
A small class that also shows automatic dependency injection: Hello.java
1 package tapestry.mini.services;
2
3 public class Hello {
4
5 private final Output _output;
6
7 public Hello(Output output) {
8 _output = output;
9 }
10
11 public void sayHello(){
12 _output.say("Hello world");
13 }
14 }
a very simple dependancy: Output.java
1 package tapestry.mini.services;
2
3 public interface Output {
4 public void say(String text);
5 }
A simple implementation of course: OutputImpl.java
1 package tapestry.mini.services;
2
3 public class OutputImpl implements Output{
4 public void say(String text){
5 System.out.println(text);
6 }
7 }