Overview
Other MyFaces Extensions
- ExtVal
- Ext-Script
- [Orchestra]
- [Portlet Bridge]
Community
Development
Sponsorship
Your browser does not support iframes
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
@ProjectStageActivated({Development.class, IntegrationTest.class}) public class SampleDataStartupObserver { protected void createSampleData(@Observes StartupEvent startupEvent, UserRepository userRepository) { User user = new User("Demo", "User"); userRepository.save(user); } } |
The message-module of CODI allows easy, pluggable and therefore advanced message-handling. However, besides messages it's sometimes essential to use resource-bundles in a type-safe manner (e.g. in case of custom configs which don't need be customizable).
@Bundle
allows to inject a ResourceBundle
. This interface is provided by CODI and is a simpler but injectable version of the std. ResourceBundle
.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
import org.apache.myfaces.extensions.cdi.core.api.resource.bundle.ResourceBundle;
//...
public class MyBean
{
@Inject
//@Jsf //optional to use the current locale
@Bundle(MyBundle.class)
private ResourceBundle resourceBundle;
public String getMyValue()
{
return this.resourceBundle.getValue(MyBundle.MyKey.class);
}
}
|
By default a resource-bundle class/interface is mapped to the corresponding bundle-file via naming convention. A class/interface can be annotated with @Bundle
optionally to find it easier via searching for the annotation or to changing the name and/or package of the corresponding bundle-file.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
package mypackage.myconfig;
//@Bundle //optional in this case
//Bundle gets mapped to mypackage.myconfig.my_bundle.properties
public interface MyBundle
{
//mapped to the resource-bundle key (by naming convention): my_key
public class MyKey implements BundleKey, MyBundle {}
//mapped to the resource-bundle key (manually): secondKey
@Named("secondKey")
public class MyKey2 extends BundleValue implements MyBundle {}
}
|
Instead of injecting the resource-bundle and resolving a value by key, it's possible to inject the value directly. That's e.g. useful for configs, because in such cases you are interested in few very specific values.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
@Bundle(name = "mypackage.myconfig.mybundle")
public interface MyBundle
{
//mapped to the resource-bundle key (by naming convention): my_value
//@Named("myKey") //for mapping it to the resource-bundle key (manually): myKey
public class MyValue extends BundleValue implements Messages {}
}
//...
public class MyBean
{
@Inject
private MyBundle.MyValue myValue;
public String getMyValue()
{
return this.myValue.toString();
}
}
|
For creating instances of Annotations, you can use the literal trick. A custom implementation allows to provide custom values (see e.g. the NamedLiteral which is used by CODI internally). If you are fine with the default values of an annotation, you can use DefaultAnnotation
to create an annotation for a given type, instead of a custom literal implementation.
...