跳至主要内容

博文

目前显示的是标签为“CDI”的博文

CDI 2.0: Configurators APIs and Intercept Producers

Configurators APIs and Intercept Producers In CDI 2.0, it is possible to add Interceptor to a producer programmaticially. For example, there is a POJO. public class Greeter { public Greeter() { } public void say(String name) { System.out.println("Hi, " + name); } } We want to count the number of calling the method. Create a Counted quilifier and CountedInterceptor interceptor class. @Inherited @InterceptorBinding @Retention ( RUNTIME ) @Target ({ METHOD , TYPE }) public @interface Counted { public static final class Literal extends AnnotationLiteral< Counted > implements Counted { public static final Literal INSTANCE = new Literal (); private static final long serialVersionUID = 1L ; } } @Interceptor @Counted public class CountedInterceptor { @Inject Logger LOG ; @Inject Counter counter; @AroundConstruct public void aroundConstructed (...

CDI 2.0: Register Beans dynamicially

Register Beans dynamicially Before CDI 2.0 register a bean dynamicially is a little complex. CDI 2.0 simple the work. public void addABean( @Observes AfterBeanDiscovery event) { // get an instance of BeanConfigurator event . addBean() // set the desired data .types( Greeter . class) .scope( ApplicationScoped . class) .addQualifier( Default . Literal . INSTANCE ) // .addQualifier(Custom.CustomLiteral.INSTANCE); // finally, add a callback to tell CDI how to instantiate this bean .produceWith(obj - > new Greeter ()); } AfterBeanDiscovery event add addBean method to register a bean manually. You can check if the bean is existed in CDI container. Set< Bean<?> > greeters = CDI . current() . getBeanManager() . getBeans( Greeter . class); assertTrue(greeters . size() == 1 ); assertNotNull(greeter); Grab the source codes from my github account, and have a try.

CDI 2.0: Fire events asynchronously

Fire events asynchronously CDI 2.0 add the capability of firing an CDI event asynchronously, this will free the client from long-awaited blocking rquest. Create a simple backing bean. @ViewScoped @Named ( " eventBean " ) public class EventBean implements Serializable { private static final Logger LOG = Logger . getLogger( EventBean . class . getName()); @Inject Event< Message > event; private String message; private String notification; public String getMessage () { return message; } public void setMessage ( String message ) { this . message = message; } public String getNotification () { return notification; } public void setNotification ( String notification ) { this . notification = notification; } public void fireEvent () { LOG . log( Level . INFO , " fire event async... " ); event . fireAsync...

CDI 2.0: Handle Events in desired Priority

Handle Events in desired Priority If there are multi CDI @Obsevers emthods defined, in the former CDI, there is no way to ensure they are excuted in a certain order. In CDI 2.0, this gap is filled by @Priority . Let's create a simple example to demonstrate it. Create a bean to fire a CDI Event . @ViewScoped @Named ( " eventBean " ) public class EventBean implements Serializable { private static final Logger LOG = Logger . getLogger( EventBean . class . getName()); @Inject Event< Message > event; private String message; public String getMessage () { return message; } public void setMessage ( String message ) { this . message = message; } public void fireEvent () { LOG . log( Level . INFO , " fire event async... " ); event . fire( new Message ( this . message)); } } The event payload Message . public class Message implements Serializa...

CDI 2.0: Java SE support

Java SE support The Java SE support in weld is now standardized, it is useful when you want to get CDI support out of Java EE application servers. Given a simple CDI bean. @Named @ApplicationScoped public class Greeter { public void say ( String name ) { System . out . println( " Hi, " + name); } } You can start CDI SeContainer like this. SeContainerInitializer initializer = SeContainerInitializer . newInstance(); try ( SeContainer container = initializer . initialize()) { assertTrue(container . isRunning()); Set< Bean<?> > greeters = container . getBeanManager() . getBeans( " greeter " ); assertTrue(greeters . size() == 1 ); } There is a try-resources statement, SeContainer is a AutoClosable and can be closed automaticially at the end. To bootstrap CDI container for Java SE, you have to add the following dependencies in project. < dependency > < groupId >org.jboss.weld.se</ g...

What is new in Java EE8(by example)

A developer's notes about upgrading to Java EE 8 Java EE 8 brings a plenty of new features which are valuable to build modern applications. I have spent some time on updating myself to the newest Java EE 8 technology stack. This mini book is my learning notes when I refreshed my knowledge to Java EE 8. It will not cover the existing content in Java EE 7. If you are new to Java EE or need a comprehensive guide of Java EE, I suggest you read the official Java EE Tutorial carefully. This mini book will cover JSF 2.3, CDI 2.0, JSON-B, Java EE Secuirty API 1.0, Servlet 4.0, JAX-RS 2.1 etc. Sample codes All sample codes mentioned in this book can be found here . And the source codes of this book itself are also hosted on my github, check here . Read it online This book will be synchronized to Gitbook, go here to read it online. Contribution This is an open source book, if you have some suggestions or find some issues (even grammar errors, I am a non-English gu...

Getting started with Java EE 8 MVC

Getting started with Java EE 8 MVC MVC is a new specification introduced in the upcoming Java EE 8. It is based on the existing JAXRS. At the moment I wrote down these posts, most of Java EE 8 specficitaions are still in the early disscussion stage, and MVC 1.0 is also not finalized, maybe some changes are included in future. I will update the Wiki pages and codes aglined with final Java EE 8 specficitaions when it is released. I will use the latest Java 8, Glassfish 4.1.1, and NetBeans IDE for these posts. Prequisition Oracle JDK 8 or OpenJDK 8 Oracle Java 8 is required, go to Oracle Java website to download it and install into your system. Optionally, you can set JAVA_HOME environment variable and add <JDK installation dir>/bin in your PATH environment variable. The latest Apache Maven Download the latest Apache Maven from http://maven.apache.org , and uncompress it into your local system. Optionally, you can set M2_HOME environment va...

JSF 2.2: Embrace CDI?

JSF 2.2: Embrace CDI? CDI 1.0 is part of Java EE 6, it is the standard Dependency Injection specification for Java EE platform. But unfortunately, JSF 2.0 which is also part of Java EE 6 did not adopt CDI as its Dependency Injection/IOC container, but invented its IOC container. Dependency injection in JSF 2.0 In the package javax.faces.bean , there are several annotations provided. You can annotate your JSF backend bean with annotation @ManagedBean and put it in a reasonable scope( @ApplicationScoped , @RequestScoped , @SessionScoped , @ViewScoped , @NoneSopced ). @ManagedBean(name="foo") @RequestScoped public class FooBean{ } The attribute name specified in the @ManagedBean can be accessed via EL in facelets views. You can inject it in other beans via @ManagedProperty . @ManagedBean(name="bar") @RequestScoped public class BarBean{ @ManagedPropertiy("foo") FooBean foo; } In JSF 2.0, it also supports to annotate the fiel...

JSF 2.2: View Action

JSF 2.2: View Action JSF 2.2 introduced a new view action feature, which had been existed in JBoss Seam 2 and Seam 3 for a long time. In fact, JSF 2.2 copied the Seam 3 view action exactly. An example An example is better than thousands of words. @Model public class ViewActionBean { @Inject Logger log; private String flag="page1"; public String init(){ log.info("call init"); switch(flag){ case "page1": return "page1"; default: return "page2"; } } public String getFlag() { return flag; } public void setFlag(String flag) { this.flag = flag; } } Create a facelets view and use a view action to invoke the init method. <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:f="...

Generate reports with Seam 3 Reports and Apache Velocity

Seam 3 provides a collection of standard CDI extensions. Seam3 report module bridges CDI and several report engines, such as JasperReports Pentaho XDocReport Basic Configuration Assume you have already created a Maven based Java EE 6 application. If not, you can create one using JBoss Forge. Add Seam 3 reports related dependencies into your pom.xml . <dependency> <groupId>org.jboss.seam.reports</groupId> <artifactId>seam-reports-api</artifactId> <version>${seam-reports-version}</version> </dependency> <!-- If you are using Jasper Reports, add the following dependency --> <dependency> <groupId>org.jboss.seam.reports</groupId> <artifactId>seam-reports-jasper</artifactId> <version>${seam-reports-version}</version> </dependency> Generally, in order to generate a JasperReports based report in a Seam 3/Java EE6 project, y...

Send email with Seam 3 Mail and JMS

Seam 3 Mail module provides simple API to use Java Mail API to send email message. Basic Configuration Assume you have already created a Maven based Java EE 6 application. Add seam mail dependency to your pom.xml file. <dependency> <groupId>org.jboss.seam.mail</groupId> <artifactId>seam-mail-api</artifactId> <scope>compile</scope> </dependency> <dependency> <groupId>org.jboss.seam.mail</groupId> <artifactId>seam-mail</artifactId> <scope>compile</scope> </dependency> Add basic mail configuration in your META-INF/seam-beans.xml . <mail:MailConfig serverHost="smtp.gmail.com" serverPort="587" auth="true" enableTls="true" username="<your gmail account>" password="<your password>"> <ee:modifies /> </mail:MailConfig> In your Java codes, inject ...