How to implement Strategy Design Pattern in Java ? (Video Tutorial)



Video tutorial to demonstrate, how to implement Strategy Design Pattern in Java.








Click here to download source code



Events in AngularJS (Video Tutorial)



Video tutorial to demonstrate working of Events in AngularJS.






Bootstrap CSS - Forms in AngularJS (Video Tutorial)




Video tutorial to demonstrate rendering of Bootstrap CSS - Forms





Bootstrap CSS - Tables in AngularJS (Video Tutorial)



Video tutorial to demonstrate rendering of Bootstrap CSS - Tables





Bootstrap CSS - Buttons in AngularJS (Video Tutorial)



Video tutorial to demonstrate rendering of Bootstrap CSS - Buttons.





Modules in AngularJS (Video Tutorial)



Video tutorial to demonstrate working of Modules in AngularJS.





Filters in AngularJS (Video Tutorial)



Video tutorial to demonstrate working of filters in AngularJS.






Controllers in AngularJS (Video Tutorial)



Video tutorial to demonstrate working of Controllers in AngularJS.





Directives in AngularJS (Video Tutorial)



Video tutorial to demonstrate how to work with directives in AngularJS.





Expressions in AngularJS (Video Tutorials)




Video tutorial to demonstrate expressions in AngularJS





How to create an AngularJS application in NetBeans ? (Video Tutorial)




Video tutorial to demonstrate how to create an AngularJS application in NetBeans.





How to create a JavaFX Hello World Application ? (Video Tutorial)



Video tutorial to demonstrate how to create a JavaFX Hello World Application.






How to make a Java class immutable ? (Video Tutorial)



Video tutorial to demonstrate how to make a Java class immutable.







How to implement Observer Design Pattern in Java ? (Video Tutorial)



Video tutorial to demonstrate how to implement Observer Design Pattern in Java.







Click to download Source Code

How to use Combo Box (Drop Down list) in Java using Swing API ? (Video Tutorial)



Video tutorial to demonstrate how to use Combo Box (Drop Down list) in Java using Swing API.






How to convert String to Numeric Values in Java ? (Video Tutorial)



Video tutorial to demonstrate how to convert String to Numeric Values in Java.





How to override equals method in Java ? (Video Tutorial)



Video tutorial to demonstrate how to override equals method in Java.







How to override hashcode method in Java ? (Video Tutorial)




Video tutorial to demonstrate how to override hashcode method in Java.









How Java is complete Programming Platform ? (Video Tutorial)




Video tutorial to demonstrate how Java is complete Programming Platform.







How to implement Abstract Factory Design Pattern in Java ? (Video Tutorial)



Video tutorial to demonstrate how to implement Abstract Factory Design Pattern in Java.







How to check whether a given number is prime number through a Java program ? (Video Tutorial)




Video tutorial to demonstrate how to check whether a given number is prime number through a Java program.










Java Serializable interface : Reading and writing Objects to a file video tutorial




Video tutorial to demonstrate Java Serializable interface : Reading and writing Objects to a file.








How to print Multiplication Table through a Java Program ? (Video Tutorial)




Video tutorial to demonstrate how to print Multiplication Table through a Java Program.







How to implement Factory Design Pattern in Java ? (Video Tutorial)



Video tutorial to demonstrate how to implement Factory Design Pattern in Java.






How to check whether a given string is palindrome or not through a Java program ?. (Video Tutorial)




Video tutorial to demonstrate how to check whether a given string is palindrome or not through a Java program.








How to check whether a given year is a leap year or not through a Java program ?. (Video Tutorial)




Video tutorial to demonstrate how to check whether a given year is a leap year or not through a Java program.







How to browse messages on queue using Java Message Service API ? (Video Tutorial)




Video tutorial to demonstrate how to browse messages on queue using Java Message Service API





How to receive messages from a message queue using Java Message Service API ? (Video Tutorial)




Video tutorial to demonstrate how to receive messages from a message queue using Java Message Service API.





How to create a simple Swing Application in Java ? (Video Tutorial)



Video tutorial to demonstrate how to create a simple Swing Application in Java.





How to send an email through a Java application using Java Mail API ? (Video Tutorial)



Video tutorial to demonstrate how to send an email through a Java application using Java Mail API






How to calculate Area and Perimeter of Parallelogram through a Java program ? (Video Tutorial)



Video tutorial to demonstrate how to calculate Area and Perimeter of Parallelogram through a Java program.






How to send messages to a message queue using Java Message Service API ? (Video Tutorial)



Video tutorial to demonstrate how to send messages to a message queue using Java Message Service API.





How to create a Zip File in Java ? (Video Tutorial)




Video tutorial to demonstrate how to create a Zip File in Java.





How to create and setup JMS Queue and JMS Topic in GlassFish application server ? (Video Tutorial)



Video tutorial to demonstrate how to create and setup JMS Queue and JMS Topic in GlassFish application server.





How to calculate Greatest Common Divisor or Highest Common Factor of numbers in Java ? (Video Tutorial)




Video tutorial to demonstrate how to calculate Greatest Common Divisor or Highest Common Factor of numbers in Java.








How to convert Decimal number to Hexadecimal number in Java ? (Video Tutorial)




Video tutorial to demonstrate how to convert Decimal number to Hexadecimal number in Java.







How to perform Linear and Binary Search in Java ? (Video Tutorial)




Video tutorial to demonstrate how to perform Linear and Binary Search in Java.







How to sort an array using Bubble Sort algorithm in Java ? (Video Tutorial)



Video tutorial to demonstrate how to sort an array using Bubble Sort algorithm in Java.







How to calculate distance between two given points in Java ? (Video Tutorial)




Video tutorial to demonstrate how to calculate distance between two given points in Java.







Java 7 new feature - Using String literals in switch statements (Video Tutorial)




Video tutorial to demonstrate Java 7 new feature - using String literals in switch statements.






Java 7 new feature - Using the try-with-resources block to improve exception handling code (Video Tutorial)




Video tutorial to demonstrate Java 7 new feature - using the try-with-resources block to improve exception handling code.







How to create and setup JMS Connection Factory in GlassFish application server ? (Video Tutorial)




Video tutorial to demonstrate how to create and setup JMS Connection Factory in GlassFish application server.







How to send an Email using Java Mail API in a Java web application ? (Video Tutorial)




Video tutorial to demonstrate how to send an Email using Java Mail API in a Java web application.





Click here to download source code 


How to deploy and undeploy a WAR file in GlassFish application server ? (Video Tutorial)



Video tutorial to demonstrate how to deploy and undeploy a WAR file in GlassFish application server.





How to change GlassFish server administrator username and password through command prompt and admin console ? (Video Tutorial)



Video tutorial to demonstrate how to change GlassFish server administrator username and password through command prompt and admin console.





How to start - stop GlassFish application server through command prompt and NetBeans ? (Video Tutorial)



Video tutorial to demonstrate how to start - stop GlassFish application server through command prompt and NetBeans.





How to install and configure GlassFish application server ? (Video Tutorial)



Video tutorial to demonstrate how to install and configure GlassFish application server.






How to create, deploy and test JAX-WS SOAP based Web Services in NetBeans ? (Video Tutorial)



Video tutorial to demonstrate how to create, deploy and test JAX-WS SOAP based Web Services in NetBeans.






How to automatically generate JPA Entities from a database table in NetBeans ? (Video Tutorial)




Video tutorials to demonstrate how to automatically generate JPA Entities from a database table in NetBeans.






How to implement Form-based Authentication in a web application ? (Video Tutorial)



Video tutorial to demonstrate how to implement Form-based Authentication in a web application.





How to integrate NetBeans with a third party Relational Database ? (Video Tutorial)



Video tutorial to demonstrate how to integrate NetBeans with a third party Relational Database.





How to integrate NetBeans with a third party Application Server ? (Video Tutorial)



Video tutorial to demonstrate how to integrate NetBeans with a third party Application Server.





How to create your first Java Server Faces (JSF) Framework Web Application ? (Video Tutorial)




Video tutorial to demonstrate how to create your first Java Server Faces (JSF) Framework Web Application.






How to create a Java EE Stateless Session Bean (EJB) in an Enterprise Application using NetBeans ? (Video Tutorial)



Video tutorial to demonstrate how to create a Java EE Stateless Session Bean (EJB) in an Enterprise Application using NetBeans.





How to Import Eclipse projects into NetBeans IDE ? (Video Tutorial)



Video tutorial to demonstrate how to import Eclipse projects into NetBeans IDE.





How to generate WAR file of a Web Application project in NetBeans IDE ? (Video Tutorial)



Video tutorial to demonstrate how to generate WAR file of a Web Application project in NetBeans IDE.






How to create dedicated folder for storing libraries in NetBeans IDE ? (Video Tutorial)



Video tutorial to demonstrate how to create dedicated folder for storing libraries in NetBeans IDE.






How to create a Java project in NetBeans IDE ? (Video Tutorial)



Video tutorial to demonstrate how to create a Java project in NetBeans IDE.




How To Create A Java Project Using Maven ? (Video Tutorial)



Video tutorial to demonstrate how to create a Java project using Maven.





How to Import existing Maven Project into NetBeans IDE ? (Video Tutorial)



Video tutorial to demonstrate how to import existing Maven project into NetBeans IDE.





How to round float and double values to integers in Java ? (Video Tutorial)



Video tutorial to demonstrate how to round float and double values to integers in Java.





How to determine class modifiers dynamically using Java Reflection API ? (Video Tutorial)



Video tutorial to demonstrate how to determine class modifiers dynamically using Java Reflection API.






How to determine System properties in Java ? (Video Tutorial)



Video tutorial to demonstrate how to determine System properties in Java.





How to get values of Environment Variables through a Java program ? (Video Tutorial)



Video tutorial to demonstrate how to get values of Environment Variables through a Java program.





How to install and setup Netbeans IDE ? (Video Tutorial)



Video tutorial to demonstrate how to install and setup Netbeans IDE






How to set initialization parameters through @WebInitParam annotation in Java Servlets? (Video Tutorial)



Video tutorial to demonstrate how to set initialization parameters through @WebInitParam annotation in Java Servlets.





How to set initialization parameters in Java Servlets through web.xml ? (Video Tutorial)




Video tutorial to demonstrate how to set initialization parameters in Java Servlets through web.xml





How to create and configure Java Servlets using @WebServlet annotation ? (VIdeo Tutorial)



Video tutorial to demonstrate how to create and configure Java Servlets using @WebServlet annotation.





How to create and configure Java Servlets using web.xml ? (Video Tutorial)



Video tutorial to demonstrate how to create and configure Java Servlets using web.xml






How to handle Request parameters in Java Servlets ? (Video Tutorial)



Video tutorial to demonstrate how to handle Request parameters in Java Servlets






How to setup Spring Framework environment with Maven using NetBeans IDE ? (Video Tutorial)



Video tutorial to demonstrate how to setup Spring Framework environment with Maven using NetBeans IDE





How to create and configure Java Filters using @WebFilter annotation ? (Video Tutorial)



Video tutorial to demonstrate how to create and configure Java Filters using @WebFilter annotation.




How to create and configure Java Filters using web.xml file ?. (VIdeo Tutorial)



Video tutorial to demonstrate how to create and configure Java Filters using web.xml file.





How to configure beans in Spring configuration file ? (Video Tutorial)



Video tutorial to demonstrate how to configure beans in Spring configuration file





Java 7 new feature - Catching multiple exception types to improve type checking (Video Tutorial)


A Java program to demonstrate how catching multiple exception types to improve type checking.





How to randomly shuffle an array in Java ?. (Video Tutorial)


Java video tutorial to demonstrate how to randomly shuffle an array in Java.




How to install Maven on Windows ?. (Video Tutorial)


A video tutorial to demonstrate how to install Maven on Windows.



How to create user-defined or application specific exceptions in Java ?.

A simple java tutorial to create user-defined or application specific exceptions.


 

How AutoWiring works in Spring Framework ?

Program to demonstrate how AutoWiring works in Spring Framework.

In the Spring Framework bean model, beans have relationships with other beans. In order to satisfy these dependencies of injecting one bean into another we use autowire functionality of Spring Framework. In this tutorial we are taking 6 scenarios by which Spring autowires one bean to another. The Scenarios are changed based on Spring Configuration files and rest all the source code given in this tutorial is the same.

In order to run each scenario individually we just change Spring Configuration xml file and rest code remains the same. So for each scenario to run individual we need

1. Employee POJO class which has-a Address class object
2. Address POJO class which will be wired to Employee class through auto-wiring
3. Test class to test the application
4. Scenario based xml file provided below individually for each scenario


Address POJO class -

package com.hubberspot.spring.autowire;


public class Address {

 private String street;
 private String city;
 private String state;

 public Address() {

 }


 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;
 }


 public String getState() {
  return state;
 }


 public void setState(String state) {
  this.state = state;
 }

}





Employee POJO class -

package com.hubberspot.spring.autowire;

// Create a POJO class Employee which has a 
// Address Object reference as instance variable
public class Employee {

 private String name;
 private int age;
 private Address address;

 public Employee( String name ) {

  this.name = name;

 }

 public Employee( Address address) {
  this.address = address;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }


 public Address getAddress() {
  return address;
 }


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

}



Scenario 1 - autowire="no"


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.

<?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"
 xmlns:c="http://www.springframework.org/schema/c"

 xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

 <!-- Scenario 1 -->

 <bean id="employee" class="com.hubberspot.spring.autowire.Employee"
  autowire="no">

  <property name="name" value="Jonty" />
  <property name="age" value="28" />
  <property name="address" ref="address"></property>
 </bean>

 <bean id="address" class="com.hubberspot.spring.autowire.Address">

  <property name="street" value="Town Hall Street" />
  <property name="city" value="Pune" />
  <property name="state" value="Maharashtra"></property>
 </bean>
 
 </beans>


In the above scenario we are using autowire attribute value as "no". It means we are using no auto-wiring, so we have to provide ref attribute for referring any bean.

Scenario 2 - autowire="byName"


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.

<?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"
 xmlns:c="http://www.springframework.org/schema/c"

 xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

 <!-- Scenario 2 -->

 <bean id="employee" class="com.hubberspot.spring.autowire.Employee"
  autowire="byName">

  <property name="name" value="Jonty" />
  <property name="age" value="28" />

 </bean>

 <bean id="address" class="com.hubberspot.spring.autowire.Address">

  <property name="street" value="Town Hall Street" />
  <property name="city" value="Pune" />
  <property name="state" value="Maharashtra"></property>
 </bean>

</beans>


In the above scenario we are using autowire attribute value as "byName". It means we are using auto-wiring based on name of property, so here Employee has a Address property. Here we have a property in Employee by name as : Address address. So when we say that auto-wire by name Spring sees in configuration that whether there is any bean with id equal to name of property. As soon as it finds this property and its matching id, it uses its setter method to set the property .

Scenario 3 - autowire="byType"


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.

<?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"
 xmlns:c="http://www.springframework.org/schema/c"

 xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

 <!-- Scenario 3 -->

 <bean id="employee" class="com.hubberspot.spring.autowire.Employee"
  autowire="byType">

  <property name="name" value="Jonty" />
  <property name="age" value="28" />

 </bean>

 <bean id="address" class="com.hubberspot.spring.autowire.Address">

  <property name="street" value="Town Hall Street" />
  <property name="city" value="Pune" />
  <property name="state" value="Maharashtra"></property>
 </bean>

</beans>


In the above scenario we are using autowire attribute value as "byType". It means we are using auto-wiring based on type of property, so here Employee has a Address property. Here we have a property in Employee by type as : com.hubberspot.spring.autowire.Address . So when we say that auto-wire by type Spring sees in configuration that whether there is any bean with class equal to type of property. As soon as it finds any matching, it uses its setter method to set the property.


Scenario 4 - autowire="constructor"


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.

<?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"
 xmlns:c="http://www.springframework.org/schema/c"

 xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

<!-- Scenario 4 -->

 <bean id="employee" class="com.hubberspot.spring.autowire.Employee"
  autowire="constructor">

  <property name="name" value="Jonty" />
  <property name="age" value="28" />

 </bean>

 <bean id="address" class="com.hubberspot.spring.autowire.Address">

  <property name="street" value="Town Hall Street" />
  <property name="city" value="Pune" />
  <property name="state" value="Maharashtra"></property>
 </bean>


</beans>


In the above scenario we are using autowire attribute value as "constructor". It means we are using auto-wiring based on constructor arguments , so here Employee has a Address property. Here we have a constructor in Employee class which takes a argument as : Address address. So when we say that auto-wire by constructor Spring sees in configuration that whether there is any bean with id equal to constructor argument. As soon as it finds any matching , it uses its setter method to set the property .


Scenario 5 - p:namespace


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.

<?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"
 xmlns:c="http://www.springframework.org/schema/c"

 xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

<!-- Scenario 5 -->

 <bean id="employee" class="com.hubberspot.spring.autowire.Employee"
  p:name="Jonty">

  <property name="age" value="28" />
  <property name="address" ref="address" />

 </bean>

 <bean id="address" class="com.hubberspot.spring.autowire.Address">

  <property name="street" value="Town Hall Street" />
  <property name="city" value="Pune" />
  <property name="state" value="Maharashtra"></property>
 </bean>

</beans>


Scenario 6 - c:namespace


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.

<?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"
 xmlns:c="http://www.springframework.org/schema/c"

 xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

 <!-- Scenario 6 -->

 <bean id="employee" class="com.hubberspot.spring.autowire.Employee"
  c:name="Jonty">

  <property name="age" value="28" />
  <property name="address" ref="address" />

 </bean>

 <bean id="address" class="com.hubberspot.spring.autowire.Address">

  <property name="street" value="Town Hall Street" />
  <property name="city" value="Pune" />
  <property name="state" value="Maharashtra"></property>
 </bean>


</beans>


Scenario 5 and 6 are using p:namespace and c:namespace which have been introduced in Spring 3.1 . In order to use these namespaces we have to first provide these namespace schemas in the header of xml files as :
"xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"

After providing the namespace we can directly use it as p:(property name) and c:(constructor argument) which are same like using property and constructor-arg tag.

Test class -

package com.hubberspot.spring.autowire;

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("The name of Employee is : " + employee.getName());
  System.out.println("The age of Employee is : " + employee.getAge());
  System.out.println("The address of Employee is : " + 
    employee.getAddress().getStreet() +" "+
    employee.getAddress().getCity() +" "+
    employee.getAddress().getState());

 }

}



Output of the program : 


 
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot