Posts

Showing posts from April, 2016

Proxy Design Pattern in Java

Image
Gang of Four Definition:    Provide a surrogate or placeholder for another object to control access to it. Computer World Example: Consider an ATM implementation for a bank. Here we will find multiple proxy objects. Actual bank information will be stored in a remote server. We must remember that in the real programming world, the creation of multiple instances of a complex object (heavy object) is very costly. In such situations, we can create multiple proxy objects (which must point to an original object) and the total creation of actual objects can be carried out on a demand basis. Thus we can save both memory and creational time. Explanation: In the following program, we are calling the doSomework() function of the proxy object, which in turn calls the doSomwork() of the concrete object. With the output, we are getting the result directly through the concrete object. For this example, I have created different packages, proxy implementations will be reside in ...

What are the design patterns used in Spring Framework?

There are loads of different design patterns used, but there are a few obvious ones: 1. Dependency Injection : which is central to the whole BeanFactory/ ApplicationContext. 2. Proxy : Used heavily in AOP (Aspect Oriented Programming) and remoting. 3. Singleton : Beans defined in Spring configuration files are singletons by default. 4. Template Method : used extensively to deal with boilerplate code such as closing connections cleanly, for example, JdbcTemplate, JmsTemplate, JpaTemplate. 5. MVC (Model View Controller) : The advantage with Spring MVC is that your controllers are POJOS as opposed to being servlets. This makes for easier testing of controllers. One thing is that hte controller is only required to return a logical view name, and the view selection is left to a separate ViewResolver. This makes it easier to reuse controllers for different view technologies. 6. Front Controller : Spring provides DispatcherServlet to ensure an incoming requests gets dispatched to yo...

How to Inject Application Context to a bean in Spring?

Some times, we need to access the Application Context in a bean itself, for example, if you want to get another bean using Application Context. Injecting Application Context to a bean is easy, your bean just has to implement ApplicationContextAware interface, ApplicationContextAware interface has a single method, which has the signature like below. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {         // TODO Auto-generated method stub   } you have to override this method, see the example below public class A implements ApplicationContextAware {      private ApplicationContext context;      public void setApplicationContext( ApplicationContext context)      {           this.context = context;      } } That's it, now you can play with Application Context.

Difference Between Application Context and Bean Factory in Spring

Image
Even though both are responsible for bean instantiation and wiring but there are some differences between them. Bean Factory Application Context Instantiate a bean when ever required. i.e Lazy Instantiation Pre instantiate all the beans at the time of container creation Doesn't support annotation based dependency injection Supports annotation based injection Doesn't publish events to listeners Application contexts can publish events to beans that are registered as listeners Doesn't provide support for internationalization i.e i18n supports internationalization Provides Basic IOC and DI (Dependency Injection) features Application Context provides advanced features such as JNDI access, EJB integration, remoting, along with IOC and DI