Java/EJB3/Injection

Материал из Java эксперт
Версия от 06:50, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

EJB Tutorial from JBoss: Injection

File: CalculatorBean.java
/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2006, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.tutorial.injection.bean;
import javax.ejb.Stateless;
@Stateless
public class CalculatorBean implements Calculator
{
   public int add(int x, int y)
   {
      return x + y;
   }
   public int subtract(int x, int y)
   {
      return x - y;
   }
}

File: ShoppingCart.java
/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2006, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.tutorial.injection.bean;
import java.util.HashMap;
import javax.ejb.Remove;
public interface ShoppingCart
{
   void buy(String product, int quantity);
   HashMap<String, Integer> getCartContents();
   @Remove void checkout();
}

File: ShoppingCartBean.java
/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2006, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.tutorial.injection.bean;
import java.util.HashMap;
import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Remove;
import javax.ejb.Stateful;

@Stateful
@Remote(ShoppingCart.class)
public class ShoppingCartBean implements ShoppingCart, java.io.Serializable
{
   private HashMap<String, Integer> cart = new HashMap<String, Integer>();
   @EJB
   private Calculator calculator;

   private Calculator set;
   @EJB(beanName="CalculatorBean")
   public void setCalculator(Calculator c)
   {
      set = c;
   }

   public void buy(String product, int quantity)
   {
      if (cart.containsKey(product))
      {
         int currq = cart.get(product);
         currq = calculator.add(currq, quantity);
         cart.put(product, currq);
      }
      else
      {
         cart.put(product, quantity);
      }
   }
   public HashMap<String, Integer> getCartContents()
   {
      return cart;
   }
   @Remove
   public void checkout()
   {
      System.out.println("To be implemented");
   }
}

File: Calculator.java
/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2006, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.tutorial.injection.bean;
public interface Calculator
{
   int add(int x, int y);
   int subtract(int x, int y);
}
File: Client.java
/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2006, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.tutorial.injection.client;

import org.jboss.tutorial.injection.bean.ShoppingCart;
import javax.naming.InitialContext;
import java.util.HashMap;
/**
 * Comment
 *
 * @author 



== Field Injection ==






   
  <!-- start source code -->
   
    <source lang="java">

File: AnotherBean.java
import javax.ejb.Stateless;
@Stateless
public class AnotherBean implements AnotherBeanLocal {
  public void doAnother(){
    System.out.println("from another bean");
  }
}

File: AnotherBeanLocal.java

public interface AnotherBeanLocal {
  public void doAnother();
}

File: Employee.java

import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;

@Entity
public class Employee implements java.io.Serializable {
  private int id;
  private String firstName;
  private String lastName;
  @Id
  @GeneratedValue
  public int getId() {
    return id;
  }

  @PostRemove
  public void postRemove()
  {
     System.out.println("@PostRemove");
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String first) {
    this.firstName = first;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String last) {
    this.lastName = last;
  }
}

File: EmployeeBean.java
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
@Stateless
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
  @EJB AnotherBeanLocal a;
  public EmployeeBean() {
  }
  public void doAction() {
    System.out.println("doAction");
    a.doAnother();
  }
}

File: EmployeeServiceLocal.java
import java.util.Map;
import javax.ejb.Local;

@Local
public interface EmployeeServiceLocal {
  public void doAction();
}

File: EmployeeServiceRemote.java

import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote {
  public void doAction();
}

File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099

File: Main.java
import javax.ejb.EJB;
import javax.naming.InitialContext;
public class Main {
  
  public static void main(String[] a) throws Exception {
    EmployeeServiceRemote service = null;
    // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
    // service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
    service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");
    service.doAction();
  }
}





Setter Injection

File: AnotherBean.java
import javax.ejb.Stateless;
@Stateless
public class AnotherBean implements AnotherBeanLocal {
  public void doAnother(){
    System.out.println("from another bean");
  }
}

File: AnotherBeanLocal.java

public interface AnotherBeanLocal {
  public void doAnother();
}

File: Employee.java

import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;

@Entity
public class Employee implements java.io.Serializable {
  private int id;
  private String firstName;
  private String lastName;
  @Id
  @GeneratedValue
  public int getId() {
    return id;
  }

  @PostRemove
  public void postRemove()
  {
     System.out.println("@PostRemove");
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String first) {
    this.firstName = first;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String last) {
    this.lastName = last;
  }
}

File: EmployeeBean.java
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
@Stateless
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
  private AnotherBeanLocal a;
  @EJB
  public void setAuditService(AnotherBeanLocal audit) {
      this.a = audit;
  }
  public EmployeeBean() {
  }
  public void doAction() {
    System.out.println("doAction");
    a.doAnother();
  }
}

File: EmployeeServiceLocal.java
import java.util.Map;
import javax.ejb.Local;

@Local
public interface EmployeeServiceLocal {
  public void doAction();
}

File: EmployeeServiceRemote.java

import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote {
  public void doAction();
}

File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099

File: Main.java
import javax.ejb.EJB;
import javax.naming.InitialContext;
public class Main {
  
  public static void main(String[] a) throws Exception {
    EmployeeServiceRemote service = null;
    // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
    // service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
    service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");
    service.doAction();
  }
}





Use EJB To Reference Another EJB In One EJB

File: AnotherBeanLocal.java

public interface AnotherBeanLocal {
  public void doAnother();
}

File: Employee.java

import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;

@Entity
public class Employee implements java.io.Serializable {
  private int id;
  private String firstName;
  private String lastName;
  @Id
  @GeneratedValue
  public int getId() {
    return id;
  }

  @PostRemove
  public void postRemove()
  {
     System.out.println("@PostRemove");
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String first) {
    this.firstName = first;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String last) {
    this.lastName = last;
  }
}

File: EmployeeBean.java
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
@Stateless
@EJB(name = "audit", beanInterface = AnotherBeanLocal.class)
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
  public EmployeeBean() {
  }
  public void doAction() {
    System.out.println("doAction");
    try {
      Context ctx = new InitialContext();
      AnotherBeanLocal a = (AnotherBeanLocal) ctx.lookup("java:comp/env/audit");
      a.doAnother();
    } catch (NamingException e) {
      throw new EJBException(e);
    }
  }
}

File: EmployeeServiceLocal.java
import java.util.Map;
import javax.ejb.Local;

@Local
public interface EmployeeServiceLocal {
  public void doAction();
}

File: EmployeeServiceRemote.java

import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote {
  public void doAction();
}

File: AnotherBean.java
import javax.ejb.Stateless;
@Stateless
public class AnotherBean implements AnotherBeanLocal {
  public void doAnother(){
    System.out.println("from another bean");
  }
}

File: Main.java
import javax.ejb.EJB;
import javax.naming.InitialContext;
public class Main {
  
  public static void main(String[] a) throws Exception {
    EmployeeServiceRemote service = null;
    // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
    // service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
    service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");
    service.doAction();
  }
}

File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099





Use Injected Context To Loopup An other EJB

File: AnotherBeanLocal.java

public interface AnotherBeanLocal {
  public void doAnother();
}

File: Employee.java

import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;

@Entity
public class Employee implements java.io.Serializable {
  private int id;
  private String firstName;
  private String lastName;
  @Id
  @GeneratedValue
  public int getId() {
    return id;
  }

  @PostRemove
  public void postRemove()
  {
     System.out.println("@PostRemove");
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String first) {
    this.firstName = first;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String last) {
    this.lastName = last;
  }
}

File: EmployeeBean.java
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
@Stateless
@EJB(name = "audit", beanInterface = AnotherBeanLocal.class)
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
  @Resource SessionContext context;
  public EmployeeBean() {
  }
  public void doAction() {
    System.out.println("doAction");
    AnotherBeanLocal a = (AnotherBeanLocal) context.lookup("audit");;
    a.doAnother();
  }
}

File: EmployeeServiceLocal.java
import java.util.Map;
import javax.ejb.Local;

@Local
public interface EmployeeServiceLocal {
  public void doAction();
}

File: EmployeeServiceRemote.java

import javax.ejb.Remote;
@Remote
public interface EmployeeServiceRemote {
  public void doAction();
}

File: AnotherBean.java
import javax.ejb.Stateless;
@Stateless
public class AnotherBean implements AnotherBeanLocal {
  public void doAnother(){
    System.out.println("from another bean");
  }
}

File: Main.java
import javax.ejb.EJB;
import javax.naming.InitialContext;
public class Main {
  
  public static void main(String[] a) throws Exception {
    EmployeeServiceRemote service = null;
    // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
    // service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
    service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");
    service.doAction();
  }
}

File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099