@Override annotation

@Override annotation is placed above the method. This tells the compiler to use this methods instead the one in the super class. That means if you don’t have the method with same name in super class you don’t need to add @Override at all.

Beside that if you @override the method, compiler will warn you through the log, and this is useful to trace which method to use.

Eclipse debugging

Debugging is used to find the place where the bug occurs. This is done by setting up break points in the code, so when you run your application it will run the code up until point where you put your first break point. Also debugging in Eclipse provides with window that shows the values of the variables at the current breakpoints.

F8 is most used button while debugging since it allows to resume to next break point. Dev should focus most on proper places to put the break points.

Java annotations

Java annotations are one of the main ease-of-development features introduced in JDK 5. Annotations are like meta-tags that you can add to your code and apply to package declarations, type declarations, constructors, methods, fields, parameters and variables. They provide a helpful way to indicate whether your methods are dependent on other methods, whether they are incomplete, whether your classes have references to other classes, and so on.

Dependecny Injection(DI) to Access EJBs

There are two ways to access the EJBs: 1. JNDI 2. DI

Advantages:

  1. Removes JNDI lookups
  2. DI can inject this:
    1. @EJB
    2. @Resource, @PersistenceContext, @EntityManager

DI is supported within:

  1. Enterprise Java Beans
  2. Servlets/JSPs/Filters/Managed Beans/
  3. Applicaiton Client Container clients

Annotations:

@EJB

Example of dependency injection EJB -> Servlet

// this is servlet
@WebServlet(name="DICalculator", urlPatterns = {"/DICalculator"})
public class DICalculatorServerlet extends HttpServlet {

// Line below injects RemoteCalculator bean into this servlet
@EJB
private RemoteCalculator calculator;
// now we can use the injected object
int sum = calculator.multiply(5,5);
RequestDispatcher dispatcher = request.getRequestDispatcher("calc-result.jsp");

@Resource

JNDI

Java Naming and Naming Interface if part of Java SE:

  1. Standard API to interact with naming and directory services
  2. Provide the ability to look up things in a “registry”
  3. Used in enterprise solutions to locate resources such as EJBs, JMS queues or Web Services

JNDI resources are organized in tree structure:

  1. Analogues to the folder structure of a computer’s file system
  2. Supports events, lookups, and complex searching against structure

App->JNDI->JNDI IMPL->DS

Finding EJB with JNDI uses four key concepts:

  1. Context
  2. Initial context
  3. Path & Name
  4. Search

Simple JNDI lookup:

String jndiPath = com.development.tutorials.ejb3.RemoteCalculator;

try{

   calculator = (RemoteCalculator) c.lookup(jndiPath);

}

Problems with JNDI
1. Keep track of JNDI path
2. Bean name