Springframework (10)

zhaozj2021-02-16  179

7, EJB

(1) Spring and EJB

l Spring is a lightweight container, which can be used to replace EJB in many occasions.

l Spring makes access and implementation EJB easier

(2) EJB Access

l Using EJB usually:

Ø Service Locator: Care JNDI, initialize Context, EJB Home Find

Ø Business representative: Reduce coupling, hidden implementation details

l Use spring without these design patterns

(3) Access local SLSB

l Use local stateless session beans

Class = "... ejb.access.localStatelessSessionProxyFactoryBean">

Mycomponent

com.mycom.mycomponent

Create a proxy with the service locator to access the EJB (Business representative)

l You can exchange beans's implementation without changing customer code: customer program uses business interface instead of EJB specific interface

(4) Accessing remote SLSB

l Using remote stateless session bean

Class = "... SimpleRemotestatelessSession ProxyFactoryBean">

Mycomponent

com.mycom.mycomponent

(5) EJB implementation

l AbstractEnterpriseBean

Ø Loading BeanFactory: EJB Environment Variable EJB / BeanFactoryPath Specifies the location of the XML BeanFactory file in the classpath; the default BeanFactory is XMLApplicationContext

l Application is just using EJB as the front end

Ø Business logic is postponed to BeanFactory's bean

(6) Implement SLSB

l State SESSION bean

l extension AbstractStateLessSessionBean

Ø Save session context

Ø Empty implementation EJBREMOVEØ EJBCREATE () method

Ø Throw it in ejbactivate () and ejbpassivate ()

L subclasses must implement onejbcreate ()

l example:

Class myslsb extends abstractStateLessSessionBean {

protected void onejbcreate () throws createException {

...

}

Public void businessmethod () {

Beanfactory bf = getBeanFactory ();

MybusinessBean MBB = bf.getBean ("MybusinessBean");

...

}

}

(7) Implement SFSB

l Keep status session bean

l extended AbstractStateFulsessionBean

Ø Save session context

Ø Empty implementation EJBREMOVE

Ø ejbcreate () method

L subclasses must implement ejbcreate (), ejbactivate () and ejbpassivate ()

l example:

Class mysfsb extends abstractStatefulsessionBean {

Public void ejbcreate () throws createException {

Loadbeanfactory ();

...

}

Public void ejbactivate () {

...

}

Public void ejbpassivate () {

...

}

Public void businessmethod () {

Beanfactory bf = getBeanFactory ();

MybusinessBean MBB = bf.getBean ("MybusinessBean");

...

}

}

(8) Implement MDB

l example:

Class mymdb extends abstractjmsmessagedrivenbean {

protected void onejbcreate () throws createException {

...

}

Public void onMessage (Message message) {

Beanfactory bf = getBeanFactory ();

MybusinessBean MBB = bf.getBean ("MybusinessBean");

...

}

}

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

New Post(0)