What's new in Slice 4.0
Adapting resources to models
Now you can adapt a resource to your model (class annotated by @SliceResource
) - absolutely seamlessly! Simple as the first line of the below snippet. Result of invocation of the second line is exactly the same.
ArticleModel article = resource.adaptTo(ArticleModel.class); ArticleModel article2 = modelProvider.get(ArticleModel.class, resource); //article equals article2
Sightly support
Slice 4.0 works perfectly with Sightly - a new way of component development in AEM 6. The only thing you need to do is to annotate your Java class by @SliceResource
. How could it be simpler? The class can also be reused in JSP scripts if needed. Use power of Slice and dependency injection along with Sightly! Read more about Sightly support.
<div data-sly-use.model="com.example.app.ArticleModel"> ${model.heading} </div>
Multiple applications on same AEM instance
Slice 4.0 allows you to run multiple Slice applications on same AEM instance and inherit injection bindings from other applications (line 4). Read more about multiple injectors.
@Override public void start(BundleContext bundleContext) { InjectorRunner runner = new InjectorRunner(bundleContext, "addonApp", BUNDLE_NAME_FILTER, BASE_PACKAGE); runner.setParentInjectorName("coreApp"); List<Module> sliceModules = SliceModulesFactory.createModules(bundleContext); runner.installModules(sliceModules); runner.start(); }
Context simplification
We significantly simplified Context of execution so you can write less boiler-plate code in your servlets/ OSGi services. Read more about context.
public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) { try (InjectorWithContext injector = InjectorUtil.getInjector("myApp", request)) { ModelProvider modelProvider = injector.getInstance(ModelProvider.class); // do something clever with the model } }
In Java 6:
public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) { InjectorWithContext injector = InjectorUtil.getInjector("myApp", request); try { ModelProvider modelProvider = injector.getInstance(ModelProvider.class); // do something clever with the model } finally { injector.close(); } }
ModelProvider
enhancements
We added a couple of useful methods to ModelProvider so you can instantiate your objects easier than ever.
<T> List<T> getList(final Class<T> type, final Iterator<String> paths); or <T> List<T> getChildModels(final Class<T> type, final Resource parentResource); or <T> T get(final Key<T> key, final Resource resource);
Mapping improvements
We extended mapper capabilities by support of enums, mapping multi-value properties to lists and child resources into lists. We also made instantiation of mapper much simpler so you can easily extend mapper by your own mapping needs.
@Children(type = ArticleModel.class) private List<ArticleModel> articles;
@Provides @ContextScoped public Mapper getMapper() { return mapperBuilder.addDefaultSliceProcessors(). addFieldProcessor(customProcessor).build(); }