Differences between revisions 4 and 5
Revision 4 as of 2009-09-20 23:20:14
Size: 3157
Editor: localhost
Comment: converted to 1.6 markup
Revision 5 as of 2015-08-28 21:39:12
Size: 4314
Editor: TamasRev
Comment:
Deletions are marked like this. Additions are marked like this.
Line 133: Line 133:

It's also important to a log4j configuration file. I.e. for maven you can put it to {{{src/main/resources/log4j.properties}}}.
This is an example config file:
{{{#!properties
log4j.rootCategory=WARN, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%p] %c{1} %m%n

log4j.category.org.apache.tapestry=error
log4j.category.tapestry=error
log4j.category.tapestry.ioc.ClassFactory=error

log4j.category.org.apache.tapestry5.ioc.FredModule=debug
log4j.category.org.apache.tapestry5.ioc.AdviceDemoModule.Greeter=debug

log4j.category.com.example=debug
log4j.category.org.apache.tapestry5.ioc.services.TapestryIOCModule.PeriodicExecutor=debug

# This makes things very verbose, but can be useful to figure what's happening and how long its taking.
log4j.category.org.apache.tapestry5.ioc.Registry=debug
log4j.category.org.apache.tapestry5.ioc.RegistryBuilder=debug

# log4j.category.org.apache.tapestry5.ioc.services.TapestryIOCModule.PlasticProxyFactory=debug
}}}

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 }

It's also important to a log4j configuration file. I.e. for maven you can put it to src/main/resources/log4j.properties. This is an example config file:

log4j.rootCategory=WARN, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%p] %c{1} %m%n

log4j.category.org.apache.tapestry=error
log4j.category.tapestry=error
log4j.category.tapestry.ioc.ClassFactory=error

log4j.category.org.apache.tapestry5.ioc.FredModule=debug
log4j.category.org.apache.tapestry5.ioc.AdviceDemoModule.Greeter=debug

log4j.category.com.example=debug
log4j.category.org.apache.tapestry5.ioc.services.TapestryIOCModule.PeriodicExecutor=debug

# This makes things very verbose, but can be useful to figure what's happening and how long its taking.
log4j.category.org.apache.tapestry5.ioc.Registry=debug
log4j.category.org.apache.tapestry5.ioc.RegistryBuilder=debug

# log4j.category.org.apache.tapestry5.ioc.services.TapestryIOCModule.PlasticProxyFactory=debug

Tapestry5HowToIocOnly (last edited 2015-08-28 21:39:12 by TamasRev)