Showing posts with label Interface. Show all posts
Showing posts with label Interface. Show all posts

Tuesday, November 04, 2008

静态成员类与非静态成员类的区别

/**
* 静态成员类与非静态成员类的区别
*/
public class StaticMemberType {

// Interfaces, enumerated types, 和annotation types 无论是否声明static,它们都是static的。
static class StaticInnerClass {
public void test() {
System.out.println("Static Nested Class Method.");

}
}

public void test() {
new NonStaticInnerClass().test();
}

public class NonStaticInnerClass {
// 非静态成员类不可以包含任何static字段、methods或者类型,除非同时使用了static和final的常量字段之外。
// static String CONST1 = "TEST"; // error
final static String CONST2 = "TEST";
public void test() {
System.out.println("Non Static Inner Class.");
}
}

public static void main(String[] args) {
new StaticMemberType.StaticInnerClass().test();

StaticMemberType staticMemberType = new StaticMemberType();
staticMemberType.test();
// new StaticMemberType.NonStaticInnerClass().test(); // error
}
}

Sunday, July 27, 2008

@Marker annotation of tapestry5

Used to define one or more marker annotations for a service implementation. This allows for injection based on the combination of type and marker interface. These marker interfaces should not have any values. The mere presence of the marker annotation is all that is needed.
当一个interface有多个实现的service时,可以为每个service实现定义一个标记(@Marker),然后就可结合对象接口的类型和这个标记来确认service并注入此service实现,当然也可以用@Inject结合@Service("serviceID")这种方式注入。

Tapestry defines two such services, in the TapestryModule.


@Marker(ClasspathProvider.class)
public AssetFactory buildClasspathAssetFactory(ResourceCache resourceCache,

ClasspathAssetAliasManager aliasManager)
{
ClasspathAssetFactory factory = new ClasspathAssetFactory(resourceCache, aliasManager);

resourceCache.addInvalidationListener(factory);

return factory;
}

@Marker(ContextProvider.class)
public AssetFactory buildContextAssetFactory(ApplicationGlobals globals)
{
return new ContextAssetFactory(request, globals.getContext());
}

Here's an example, you can see how Tapestry is figuring out which service to inject based on the presence of those annotations:

public void contributeAssetSource(MappedConfiguration configuration,
@ContextProvider
AssetFactory contextAssetFactory,

@ClasspathProvider
AssetFactory classpathAssetFactory)
{
configuration.add("context", contextAssetFactory);
configuration.add("classpath", classpathAssetFactory);
}

Reference: http://tapestry.apache.org/tapestry5/tapestry-ioc/cookbook/basics.html