JBoss EJB3.0-INJECTION

xiaoxiao2021-03-05  25

Calculator.java, CalculatorBean.java and ShoppingCart.java, Client.java These programs have been seen before, where there is a change in ShoppingCartBean.java inserted Calculator.

Calculator.java

Package org.jboss.tutorial.injection.bean;

Public Interface Calculator

{

INT Add (int X, int y);

Int subtract (int X, int y);

}

CalculatorBean.java

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;

}

}

ShoppingCart.java

Package org.jboss.tutorial.injection.bean;

Import javax.ejb.remote;

Import javax.ejb.remove;

Import java.util.hashmap;

@Remote

Public Interface ShoppingCart

{

Void Buy (String Product, Int Quantity);

Hashmap getCartContents ();

@Remove void checkout ();

}

ShoppingCartBean.java

Package org.jboss.tutorial.injection.bean;

Import javax.ejb.ejb;

Import javax.ejb.inject;

Import javax.ejb.remove;

Import javax.ejb.stateful;

Import java.util.hashmap;

@Stateful

Public Class ShoppingCartBean Implements SHOPPINGCART

{

Private Hashmap Cart = New Hashmap ();

@Inject (jndiname = "org.jboss.tutorial.injection.bean.calculator")

Private Calculator Calculator;

PRIVATE CALCULATOR SET;

@Ejb (name = "Calculator", JNDINAME = "org.jboss.tutorial.INJECTION.BEAN.CALCULATOR")

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);

INT TEMP = SET.ADD (CURRQ, Quantity); System.out.Println ("Same Effect:" TEMP);

}

Else

{

Cart.Put (Product, Quantity);

}

}

Public HashMap getCartContents ()

{

Return cart;

}

@Remove

Public void checkout ()

{

System.out.println ("To Be Implement");

}

}

In ShoppingCartBean.java, use Calculator to calculate, we only need to insert (inject) when defined (INJECT), you need to pay attention to this Calculator is already instance, not in New :) or another method can be inserted

@Inject Org.jboss.Tutorial.INJECTION.Bean.calculator

Different when inserting Reference:

@Ejb (name = "Calculator", JNDINAME = "org.jboss.tutorial.INJECTION.BEAN.CALCULATOR")

In the Buy method, add a shaped TEMP to check the role of both calculators (Calculator and SET). The calculation results of the SET will be seen in the JBoss run window.

Client.java

Package Org.jboss.tutorial.INJECTION.CLIENT;

Import org.jboss.tutorial.INJECTION.Bean.shoppingCart;

Import javax.naming.initialcontext;

Import java.util.hashmap;

Public Class Client

{

Public static void main (string [] args) Throws Exception

{

InitialContext CTX = New InitialContext ();

ShoppingCart Cart = (ShoppingCart) ctx.lookup (shoppingcart.class.getname ());

System.out.println ("Buying 1 Memory Stick");

Cart.buy ("Memory Stick", 1);

System.out.println ("Buying ANOTHER MEMORY STICK);

Cart.buy ("Memory Stick", 1);

System.out.println ("Buying a Laptop");

Cart.buy ("Laptop", 1);

System.out.Println ("Print Cart:");

Hashmap fullcart = cart.getCartContents ();

FOR (String Product: fullcart.keyset ())

{

System.out.println (Fullcart.get (Product) " Product);

}

System.out.println ("Checkout");

Cart.checkout ();

}

}

There is no log4j.properties in jboss-ejb-3.0_preview_5.zip, there is no such thing as the lack of appender. With this will generate a replard.log log file in this directory. Log4j.properties

Log4j.Appender.r = org.apache.log4j.rollingfileappender

Log4j.Appender.r.file = record.log

Log4j.Appender.r.Layout = org.apache.log4j.patternlayout

Log4j.Appender.r.Layout.conversionPattern =% p% d {hh: mm: ss}% T% c {1} -% M% N

Log4j.Appender.r.maxbackupindex = 1

Log4j.Appender.r.maxfilesize = 100kb

Log4j.Appender.stdout.Layout = Org.apache.log4j.patternlayout

Log4j.Appender.stdout.Layout.conversionPattern =% 5P [% T] (% F:% L) -% M% N

Log4j.appender.stdout = org.apache.log4j.consoleAppender

Log4j.rootlogger = stdout, r

Run: Refer to Installing.html

Under Windows

Open the command prompt cmd to jboss_home / bin

Run.bat -c all

Use ANT

After Build, run.

discuss:

Write a lot of blog, but I don't know how to upload the source :)

转载请注明原文地址:https://www.9cbs.com/read-36371.html

New Post(0)