- Create a new Enterprise Application Project as ‘SpringTutorial’. Add two new modules to it:
a) Application Client module named as ‘SpringTutorialClient’.
b) EJB module named as ‘SpringTutorialEJB’. - First we will work on EJB module and try to create an Stateless bean as Spring resource. In SpringTutorialEJB stateless EJB will have 3 classes as usual. Named as SpringEJB,SpringHome and SpringRemote.
- Service logic can be associated with bean using interface. In our case,it will be UserNameService interface and implementation will be in UserNameServiceImpl class.
- Creating UserNameService interface:
package com.company.service;
public interface UserNameService {
public boolean checkName (String name);
} - Creating UserNameServiceImpl class:
package com.company.service;
public class UserNameServiceImpl implements UserNameService{
public boolean checkName(String name) {
if (name.equals("admin"))
return true;
else
return false;
}
} - Next step is to create Remote interface SpringRemote for our SpringEJB:
package com.mycompany.ejb;
import javax.ejb.EJBObject;
import com.company.service.UserNameService;
public interface SpringRemote extends UserNameService , EJBObject{
}
Here no business method is defined as we are extending UserNameService interface to it. - Home Interface will look like:
package com.mycompany.ejb;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface SpringHome extends EJBHome{
public SpringRemote create() throws CreateException,java.rmi.RemoteException;
} - Now comes the bean class:
package com.mycompany.ejb;
import javax.ejb.CreateException;
import org.springframework.ejb.support.AbstractStatelessSessionBean;
import com.company.service.UserNameService;
public class SpringEJB extends AbstractStatelessSessionBean implements UserNameService{
public static final String BEAN_NAME = "userNameService";
private UserNameService service;
@Override
protected void onEjbCreate() throws CreateException {
service = (UserNameService)getBeanFactory().getBean(BEAN_NAME);
}
public boolean checkName(String name) {
return service.checkName(name);
}
}
Few points to be noted here:
a) Bean class SpringEJB extends the Spring Base class AbstractStatelessSessionBean and implements the UserNameService.
b) This has no ejbXXX () methods as these are all implemented in AbstractStatelessSessionBean.
c) onEjbCreate() method is called during ejbCreate(). It is used to obtain any Spring-managed resources the EJB bean needs. - Bean configuration in EJB Deployment Descriptor:
<session>
<description>User Name Service </description>
<ejb-name>UserNameService</ejb-name>
<home>com.mycompany.ejb.SpringHome</home>
<remote>com.mycompany.ejb.SpringRemote</remote>
<ejb-class>com.mycompany.ejb.SpringEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
<env-entry>
<env-entry-name>ejb/BeanFactoryPath</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>]
<env-entry-value>applicationContext.xml</env-entry-value>
</env-entry>
</session>
The important part in DD is the <env-entry> tag that sets the value of ejb/BeanFactoryPath JNDI location to applicationContext.xml. - applicationContext will look like:
<beans>
<bean id="userNameService" class="com.mycompany.service.UserNameServiceImpl"/>
</beans> - You can deploy this ejb on desired server and test it using JNDI lookup.
Till then Happy Coding!!!
Awesome blog! Is your theme custom made or did you download it from somewhere?
ReplyDeleteA design like yours with a few simple tweeks would really make my blog jump out.
Please let me know where you got your design. Many thanks
Here is my blog post: costume de baie (http://costumedebaie2013.ro/)