Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

Java EE

  1. Java Server Pages (JSP) : Advantages over Servlets and other technologies
  2. How to create simple Hello World Servlet application in Java ?
  3. What is a Web Application in Java programming language?
  4. A Simple HTML and JSP Email Subscription List Application
  5. How to get real path for a file in JSP and Servlet ?.
  6. How to use various types of JSP tags in a JSP page ?
  7. How to set and get an attribute to/from request object in JSP and Servlets ?
  8. How to redirect responses to a different JSP or Servlets ?.
  9. How to add and retrieve Sessions in JSP and Servlets using Session Management API ?
  10. How to destroy a session in JSP and Servlets using Session Management API ?
  11. How to expire and destroy Cookies in JSP and Servlets ?
  12. A simple application demonstrating Request, Session, Context differences and usages in JSP and Servlets
  13. How to check whether which Web-Browser have been used to run JSP and Servlets ?.
  14. How to add and retrieve Sessions in JSP and Servlets using Session Management API ?
  15. How to destroy a session in JSP and Servlets using Session Management API ?
  16. How to perform create sql query in JSP and Servlets using JDBC ?
  17. How to perform insert sql query in JSP and Servlets using JDBC ?
  18. How to perform update sql query in JSP and Servlets using JDBC ?
  19. How to create a Filter that adds url and time of request to a database in JSP and Servlets ?.
  20. How to perform delete sql query in JSP and Servlets using JDBC ?
  21. How to create error page in a JSP and Servlet to handle exceptions ?
  22. How to use filters in JSP and Servlets for logging information in ServletContext logs ?
  23. How to include JSP page dynamically into another JSP page?
  24. How to display HTTP Request Headers through a Servlet ?.
  25. How to display Request url Information through a Servlet ?
  26. How to display request parameters in a JSP coming in a request ?.
  27. How to use model / Java / Pojo classes with JSP and Servlets ?.
  28. How to forward a request to a JSP using RequestDispatcher ?.
  29. How to do Session Management in JSP and Servlets using its methods and API ?.
  30. How to forward a request from one Jsp to another Jsp ?.
  31. How to include one JSP into another JSP ?.
  32. How to use Jsp action elements useBean, setProperty and getProperty to add, set and get property of a bean in a JSP page ?.
  33. How to decorate form in a JSP using fieldset and legend tag ?.
  34. How to write a Servlet code to download a Jar or file from the Server at a specified location ?.
  35. How to use Servlets Initialization Parameters through ServletConfig object in Java EE Application ?.
  36. How to use Context Parameters in Servlets through ServletContext object in Java EE Application ?.
  37. "Online Tweeter Enterprise Application" : Creating a JSP page in NetBeans Web module - Part 6
  38. "Online Tweeter Enterprise Application" : Creating a Servlet named DisplayTweets in NetBeans Web module - Part 9
  39. How to use Arithmetic Operations in Expression Language ( el ) in a simple jsp page ?.
  40. "Online Tweeter Enterprise Application" : Creating a Servlet named TweetsSubmit in NetBeans Web module - Part 8
  41. How to use param implicit object in a JSP page ?. 
  42. "Online Tweeter Enterprise Application" : Creating an Enterprise Application with EJB 3.1 in Netbeans - Part 0
  43. "Online Tweeter Enterprise Application" : Creating an Enterprise Application Project in NetBeans - Part 1
  44. "Online Tweeter Enterprise Application" : Creating a Persistence Unit in NetBeans - Part 2
  45. "Online Tweeter Enterprise Application" : Creating an Entity Class in NetBeans EJB module - Part 3
  46. "Online Tweeter Enterprise Application" : Creating a Message-Driven Bean in NetBeans EJB module - Part 4
  47. "Online Tweeter Enterprise Application" : Creating a Stateless Session Bean in NetBeans EJB module - Part 5
  48. "Online Tweeter Enterprise Application" : Creating a JSP page in NetBeans Web module - Part 6
  49. "Online Tweeter Enterprise Application" : Creating a Singleton Session Bean in NetBeans EJB module - Part 7
  50. "Online Tweeter Enterprise Application" : Creating a Servlet named TweetsSubmit in NetBeans Web module - Part 8
  51. "Online Tweeter Enterprise Application" : Creating a Servlet named DisplayTweets in NetBeans Web module - Part 9
  52. "Online Tweeter Enterprise Application" : Building and Running the Enterprise Application - Part 10
  53.  


@Required Annotation in Spring Framework

@Required annotation is placed in Spring API package org.springframework.beans.factory.annotation.* . This annotation is used for checking Spring Dependency. It checks whether properties of bean are set or not. @Required annotation is placed over the setter for the properties which is to be checked for Spring Dependency. 

A Spring Bean by the name "RequiredAnnotationBeanPostProcessor" checks if properties annotated with @Required have been set. Before a bean is initialized into a Spring Container this bean post processor checks for properties whether are set or not (marked as @Required).

Let's look at it by a simple example -

(WITHOUT @Required ANNOTATION)

1. Create a class say "Address.java"

package com.hubberspot.spring.ioc;

public class Address {

 private String street;
 private String city;
 
 public String getStreet() {
  return street;
 }
 public void setStreet(String street) {
  this.street = street;
 }
 public String getCity() {
  return city;
 }
 public void setCity(String city) {
  this.city = city;
 } 
 
}


2. Create a class say "Employee.java"

package com.hubberspot.spring.ioc;

public class Employee {

 private String firstName; 
 private Address address;

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public Address getAddress() {
  return address;
 }

 public void setAddress(Address address) {
  this.address = address;
 } 
 
}




3. Create a Spring Configuration file say "spring.xml"

<beans xmlns:context="http://www.springframework.org/schema/context" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.springframework.org/schema/beans" 
xsi:schemalocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  
 <bean class="com.hubberspot.spring.ioc.Employee" id="employee">
  <property name="firstName" value="Dinesh"></property>
 </bean>

 <bean class="com.hubberspot.spring.ioc.Address" id="address">
     <property name="street" value="Park Street"></property>
     <property name="city" value="Pune"></property>
 </bean>

</beans>



4. Create a Test class say "Test.java"

package com.hubberspot.spring.ioc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

 public static void main(String[] args) {

  // ApplicationContext is a Spring interface which
  // provides with the configuration for an application.
  // It provides us with all the methods that BeanFactory
  // provides. It loads the file resources in a older
  // and generic manner. It helps us to publish events to the
  // listener registered to it. It also provides quick support
  // for internationalization. It provides us with the object
  // requested, it reads the configuration file and provides
  // us with the necessary object required.
  // We are using concrete implementation of ApplicationContext
  // here called as ClassPathXmlApplicationContext because this
  // bean factory reads the xml file placed in the classpath of
  // our application. We provide ClassPathXmlApplicationContext
  // with a configuration file called as spring.xml placed
  // at classpath of our application.
  ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

  // In order to get a object instantiated for a particular bean
  // we call getBean() method of ClassPathXmlApplicationContext
  // passing it the id for which the object is to be needed.
  // Here getBean() returns an Object. We need to cast it back
  // to the Employee object. Without implementing new keyword we
  // have injected object of Employee just by reading an xml
  // configuration file.  
  Employee employee = (Employee) context.getBean("employee");

  System.out.println("FirstName : " + employee.getFirstName());

  System.out.println("Address : " + employee.getAddress().getStreet() + " , "
    + employee.getAddress().getCity());


 }

}


As we haven't provided Dependency Injection for Address bean in Employee bean. When we run the above Test class the output comes out to be as -

Exception in thread "main" FirstName : Dinesh
java.lang.NullPointerException
    at com.hubberspot.spring.ioc.Test.main(Test.java:36)


Its a null pointer exception because Address bean is not set in Employee bean. When we access the properties of Address bean through Address we get null pointer exception.

(WITH @Required ANNOTATION)

1. Create a class say "Address.java"

package com.hubberspot.spring.ioc;

public class Address {

 private String street;
 private String city;
 
 public String getStreet() {
  return street;
 }
 public void setStreet(String street) {
  this.street = street;
 }
 public String getCity() {
  return city;
 }
 public void setCity(String city) {
  this.city = city;
 } 
 
}


2. Create a class say "Employee.java" having @Required annotation on Address setter method.

package com.hubberspot.spring.ioc;

import org.springframework.beans.factory.annotation.Required;

public class Employee {

 private String firstName; 
 private Address address;

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public Address getAddress() {
  return address;
 }

 @Required
 public void setAddress(Address address) {
  this.address = address;
 }  
 
}




3. Create a Spring Configuration file say "spring.xml" having a tag context:annotation-config for registering RequiredAnnotationBeanPostProcessor which checks if all the bean properties with the @Required annotation have been set.

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  
 <context:annotation-config />
  
 <bean id="employee" class="com.hubberspot.spring.ioc.Employee">
  <property name="firstName" value="Dinesh"></property>
 </bean>

 <bean id="address" class="com.hubberspot.spring.ioc.Address">
     <property name="street" value="Park Street"></property>
     <property name="city" value="Pune"></property>
 </bean>

</beans>



4. Create a Test class say "Test.java"

package com.hubberspot.spring.ioc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

 public static void main(String[] args) {

  // ApplicationContext is a Spring interface which
  // provides with the configuration for an application.
  // It provides us with all the methods that BeanFactory
  // provides. It loads the file resources in a older
  // and generic manner. It helps us to publish events to the
  // listener registered to it. It also provides quick support
  // for internationalization. It provides us with the object
  // requested, it reads the configuration file and provides
  // us with the necessary object required.
  // We are using concrete implementation of ApplicationContext
  // here called as ClassPathXmlApplicationContext because this
  // bean factory reads the xml file placed in the classpath of
  // our application. We provide ClassPathXmlApplicationContext
  // with a configuration file called as spring.xml placed
  // at classpath of our application.
  ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

  // In order to get a object instantiated for a particular bean
  // we call getBean() method of ClassPathXmlApplicationContext
  // passing it the id for which the object is to be needed.
  // Here getBean() returns an Object. We need to cast it back
  // to the Employee object. Without implementing new keyword we
  // have injected object of Employee just by reading an xml
  // configuration file.  
  Employee employee = (Employee) context.getBean("employee");

  System.out.println("FirstName : " + employee.getFirstName());

  System.out.println("Address : " + employee.getAddress().getStreet() + " , "
    + employee.getAddress().getCity());


 }

}


As we haven't provided Dependency Injection for Address bean in Employee bean but this time we have marked setter method of Address with @Required annotation. When we run the above Test class the output comes out to be as -

Exception in thread "main" org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'employee' defined in class path resource [spring.xml]:
Initialization of bean failed; nested exception is

org.springframework.beans.factory.BeanInitializationException:
Property 'address' is required for bean 'employee'



It checks this time before initialization of Employee bean that Address bean is not injected and exception is thrown.


 
© 2021 Learn Java by Examples Template by Hubberspot