Monday, 14 September 2015

Java Source World: Difference between Bean Factory and Application Context




Difference between Bean Factory and Application Context



Following are some of the differences:

Application contexts provide a means for resolving text messag es, including support for i18n of those messages.

Application contexts provide a generic way to load file resources, such as images.

Application contexts can publish events to beans that are registered as listeners.

Certain operations on the container or beans in the container, which have to be handled in a programmatic fashion with a bean factory, can be handled declaratively in an application context.

The application context implements MessageSource, an interface used to obtain localized messages, with the actual implementation being plug g able.

Read: What are spring beans
Read More »

Java Source World: Enterprise beans

Enterprise beans | Java Source World


                                                                                               Java Source World          A Java quick reference blog

 Enterprise beans:




What are the different kinds of enterprise beans

Session Bean: is a non-persistent object that implements some business logic running on the server. Session beans do not survive system shut down. There are two types of session beans
  • Stateless session beans (each session bean can be reused by multiple EJB clients)
  •  Stateful session beans (each session bean is associated with one EJB client)


Entity Bean: is a persistent object that represents object views of the data, usually a row in a database.

 They have the primary key as a unique identifier. Multiple EJB clients can share each entity bean.  Entity beans can survive system shut shutdowns. Entity beans can have two types of persistence
  •  Container-managed persistence (CMP) - The container is responsible for saving the bean’s     state.
  •  Bean-managed persistence (BMP) – The Entity Bean is responsible for saving its own state.


Message-driven Bean: is integrated with the Java Message Service (JMS) to provide the ability to act as a message consumer and perform asynchronous processing between the server and the message producer.



What is the difference between session and entity beans?

Session Beans
Entity Beans
Use session beans for application logic.
Use entity beans to develop persistent object model.
Expect little reuse of session beans.
Insist on reuse of entity beans.
Session beans control the workflow and transactions of a
group of entity beans.
Domain objects with a unique identity (ie-primary key) shared by multiple clients.
Life is limited to the life of a particular client. Handle
database access for a particular client.
Persist across multiple invocations. Handles database access for multiple clients.
Do not survive system shut downs or server crashes.
Do survive system shut downs or server crashes.


What is the difference between stateful and stateless session beans

Stateless Session Beans
Stateful Session Bean
Do not have an internal state. Can be reused by different
clients.
Do have an internal state. Reused by the same client.
Need not be activated or passivated since the beans are
pooled and reused.
Need to handle activation and passivation to conserve system
memory since one session bean object per client.


What is the difference between Container Managed Persistence (CMP) and Bean Managed Persistence (BMP)

Container Managed Persistence (CMP)
Bean Managed Persistence (BMP)
The container is responsible for persisting state of the bean.
The bean is itself responsible for persisting its own state.
Container needs to generate database (SQL) calls.
The bean needs to code its own database (SQL) calls.
The bean persistence is independent of its database (e.g.
DB2, Oracle, Sybase etc). So it is portable from one data
source to another.
The bean persistence is hard coded and hence may not be
portable between different databases (e.g. DB2, Oracle etc).


Can an EJB client invoke a method on a bean directly

An EJB client should never access an EJB directly. Any access is done through the container. The container will intercept the client call and apply services like transaction, security etc prior to invoking the actual EJB.
This relationship between the EJB and the container is like “don’t call us, we will call you”.



What is a business delegateWhy should you use a business delegate 

Problem: When presentation tier components interact directly with the business services components like EJB, the presentation components are vulnerable to changes in the implementation of business services components.

Solution: Use a Business Delegate to reduce the coupling between the presentation tier components and the business services tier components. Business Delegate hides the underlying implementation details of the business service, such as look-up and access details of the EJB architecture.

Business delegate is responsible for:
  •  Invoking session beans in Session Facade.
  •  Acting as a service locator and cache home stubs to improve  performance.
  •  Handling exceptions from the server side. (Unchecked exceptions get wrapped into the remote exception,checked exceptions can be thrown as an application exception or wrapped in the remote exception.unchecked exceptions do not have to be caught but can be caught and should not be used in the method signature.)

  •  Re-trying services for the client (For example when using optimistic  locking business delegate will retry the method call when there is a  concurrent access.).



Also Read: Session beans

Also Read: Difference between HTTPSession and Stateful Session Bean

                                                                        Java Source World        A Java quick reference blog





Read More »

Sunday, 6 September 2015

Java Source World: Spring Framework - Setter Injection with Map Example

Setter Injection with Map Example




In this example, we are using map as the answer for a question that have answer as the key and username as the value. Here, we are using key and value pair both as a string.

Like previous examples, it is the example of forum where one question can have multiple answers.

Question.java

This class contains three properties, getters & setters and displayInfo() method to display the information.
    package com.springexample; 
    import java.util.Iterator; 
    import java.util.Map; 
    import java.util.Set; 
    import java.util.Map.Entry;       
    public class Question { 
    private int id; 
    private String name; 
    private Map<String,String> answers; 
     
    //getters and setters 
     
    public void displayInfo(){ 
        System.out.println("question id:"+id); 
        System.out.println("question name:"+name); 
        System.out.println("Answers...."); 
        Set<Entry<String, String>> set=answers.entrySet(); 
        Iterator<Entry<String, String>> itr=set.iterator(); 
        while(itr.hasNext()){ 
            Entry<String,String> entry=itr.next(); 
            System.out.println("Answer:"+entry.getKey()+" Posted By:"+entry.getValue()); 
        } 
    } 
    } 
applicationContext.xml
The entry attribute of map is used to define the key and value information.
    <?xml version="1.0" encoding="UTF-8"?> 
    <beans 
        xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:p="http://www.springframework.org/schema/p" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">       
    <bean id="q" class="com.springexample.Question"> 
    <property name="id" value="1"></property> 
    <property name="name" value="What is Java?"></property> 
    <property name="answers"> 
    <map>  
    <entry key="Java is a programming language"  value="Sonoo Jaiswal"></entry> 
    <entry key="Java is a Platform" value="Sachin Yadav"></entry> 
    </map> 
    </property> 
    </bean>       
    </beans> 

Test.java
This class gets the bean from the applicationContext.xml file and calls the displayInfo() method.
    package com.springexample; 
    import org.springframework.beans.factory.BeanFactory; 
    import org.springframework.beans.factory.xml.XmlBeanFactory; 
    import org.springframework.core.io.ClassPathResource; 
    import org.springframework.core.io.Resource;       
    public class Test { 
    public static void main(String[] args) { 
        Resource r=new ClassPathResource("applicationContext.xml"); 
        BeanFactory factory=new XmlBeanFactory(r);           
        Question q=(Question)factory.getBean("q"); 
        q.displayInfo();           
    } 
    }   

Read More »

Java Source World: Spring Framework - Setter Injection with Dependent Object Example

Setter Injection with Dependent Object Example


 Setter Injection with Dependent Object Example


Like Constructor Injection, we can inject the dependency of another bean using setters. In such case, we use property element. Here, our scenario is Employee HAS-A Address. The Address class object will be termed as the dependent object. Let's see the Address class first:
Address.javaThis class contains four properties, setters and getters and toString() method.
    package com.springexample;    
    public class Address {
    private String addressLine1,city,state,country;
       //getters and setters
       public String toString(){
        return addressLine1+" "+city+" "+state+" "+country;
    }
Employee.javaIt contains three properties id, name and address(dependent object) , setters and getters with displayInfo() method.
    package com.springexample;    
    public class Employee {
    private int id;
    private String name;
    private Address address;
       //setters and getters
       void displayInfo(){
        System.out.println(id+" "+name);
        System.out.println(address);
    }
    }




applicationContext.xml:The ref attribute of property elements is used to define the reference of another bean.
    <?xml version="1.0" encoding="UTF-8"?>
    <beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">    
    <bean id="address1" class="com.springexample.Address">
    <property name="addressLine1" value="51,Lohianagar"></property>
    <property name="city" value="Ghaziabad"></property>
    <property name="state" value="UP"></property>
    <property name="country" value="India"></property>
    </bean>    
    <bean id="obj" class="com.springexample.Employee">
    <property name="id" value="1"></property>
    <property name="name" value="Sachin Yadav"></property>
    <property name="address" ref="address1"></property>
    </bean>    
    </beans>

Test.javaThis class gets the bean from the applicationContext.xml file and calls the displayInfo() method.
    package com.springexample;    
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext; 
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.Resource;    
    public class Test {
    public static void main(String[] args) {
        Resource r=new ClassPathResource("applicationContext.xml");
        BeanFactory factory=new XmlBeanFactory(r);
               Employee e=(Employee)factory.getBean("obj");
        e.displayInfo();        
    }
    
Read More »