| /////////////////////////////////////////////////////////////// |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| /////////////////////////////////////////////////////////////// |
| |
| [[howto-create-concern,Create a Concern]] |
| = Create a Concern = |
| |
| Concerns are defined in <<def-concern>>. |
| |
| If you want to reproduce what's explained in this tutorial, remember to depend on the Core Bootstrap artifact: |
| |
| include::../../../../core/bootstrap/build/docs/buildinfo/artifact.txt[] |
| |
| At runtime you will need the Core Runtime artifact too. See the <<howto-depend-on-zest>> tutorial for details. |
| |
| == Typed Concern == |
| |
| A typed Concern is a Java class that implements the MixinType it can be used on: |
| |
| [snippet,java] |
| ----------- |
| source=tutorials/introduction/tenminutes/src/main/java/org/qi4j/demo/tenminute/InventoryConcern.java |
| tag=allClass |
| ----------- |
| |
| Note that we could have implemented the InventoryConcern as an abstract class if we were not interested in __all__ the methods in the Order interface. |
| Extending the ConcernOf is a convenience mechanism, instead of an explicit @ConcernFor annotation on |
| a private field, which can be used in rare occasions when you are not able to extend. This base class defines the next |
| field, which is set up by the Zestâ„¢ runtime and points to the next fragment in the call stack. We can also see that the |
| InventoryService is provided to the concern, which is done with dependency injection. Zestâ„¢ also supports dependency |
| injection via constructors and methods. |
| |
| It can be used as follows; |
| |
| [snippet,java] |
| ----------- |
| source=manual/src/main/java/org/qi4j/manual/recipes/concern/Order.java |
| tag=class |
| ----------- |
| |
| Methods of the Concern Fragment will be called before the Mixin invocation. |
| |
| |
| == Generic Concern == |
| |
| A generic Concern is a Java class that implements java.lang.reflect.InvocationHandler which allows it to be used on any |
| arbitrary MixinType. |
| |
| [snippet,java] |
| ----------- |
| source=manual/src/main/java/org/qi4j/manual/recipes/concern/MyGenericConcern.java |
| tag=class |
| ----------- |
| |
| It can be used as follows; |
| |
| [snippet,java] |
| ----------- |
| source=manual/src/main/java/org/qi4j/manual/recipes/concern/AnyMixinType.java |
| tag=class |
| ----------- |
| |
| Methods of the Concern Fragment will be called before the Mixin invocation. |
| |
| === AppliesTo === |
| |
| For generic Concerns that should only trigger on methods with specific annotations or fulfilling some expression, add |
| @AppliesTo annotation to the Concern class which points to either triggering annotation(s), or to AppliesToFilter |
| implementation(s). |
| |
| The Concern is invoked if one of the triggering annotations is found or one of the AppliesToFilter accepts the |
| invocation. In other words the AppliesTo arguments are OR'ed. |
| |
| Here is how the declaration goes ; |
| |
| [snippet,java] |
| ----------- |
| source=manual/src/main/java/org/qi4j/manual/recipes/concern/MyGenericConcern.java |
| tag=appliesTo |
| ----------- |
| |
| And how to use the annotation ; |
| |
| [snippet,java] |
| ----------- |
| source=manual/src/main/java/org/qi4j/manual/recipes/concern/AnyMixinType.java |
| tag=annotationUse |
| ----------- |
| |
| Here only the doSomething() method will see the Concern applied whereas the doSomethingElse() method won't. |
| |
| Finally here is how to implement an AppliesToFilter: |
| |
| [snippet,java] |
| ----------- |
| source=manual/src/main/java/org/qi4j/manual/recipes/concern/MyAppliesToFilter.java |
| tag=filter |
| ----------- |