Wednesday, May 30, 2012

Reflection API in java

Java Reflection API

Reflection is a feature in the Java programming language. Java's Reflection API's makes it possible to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time. It is also possible to instantiate new objects, invoke methods and get/set field values using reflection.



Example.java

package com.jsl.core;
import java.lang.reflect.Method;
class Example{
 public void display(){
  System.out.println("The Display Method");
 }
 public void show(){
  System.out.println("The Show Method");
 }
 private void print(){
  System.out.println("The Print Method");
 }
}

GetMethods.java

public class GetMethods {
 public static void main(String[] args) {
  try{
       Class c=Class.forName("com.jsl.core.Example");
       Method method[]=c.getDeclaredMethods();
       for(Method name:method)
        System.out.println(name);
     }catch (Exception e) {
       System.out.println(e);
     }
 }
}

output

private void com.jsl.core.Example.print()
public void com.jsl.core.Example.display()
public void com.jsl.core.Example.show()
getDeclaredClasses()

Returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object. This includes public, protected, default access, and private classes and interfaces declared by the class, but excludes inherited classes and interfaces. This method returns an array of length 0 if the class declares no classes or interfaces as members, or if this Class object represents a primitive type, an array class, or void.


try{
    Class c=Class.forName("com.jsl.core.Example");
    Method method[]=c.getMethods();
    for(Method name:method)
      System.out.println(name);
}catch (Exception e) {
     System.out.println(e);
}
Returns an array containing Method objects reflecting all the public member methods of the class or interface represented by this Class object, including those declared by the class or interface and and those inherited from superclasses and superinterfaces. The elements in the array returned are not sorted and are not in any particular order. This method returns an array of length 0 if this Class object represents a class or interface that has no public member methods, or if this Class object represents an array class, primitive type, or void.


isInstance( )

Determines if the specified Object is assignment-compatible with the object represented by this Class. This method is the dynamic equivalent of the Java language instanceof operator. The method returns true if the specified Object argument is non-null and can be cast to the reference type represented by this Class object without raising a ClassCastException. It returns false otherwise.

public class GetMethods {
  public static void main(String[] args) {
   try{
       Class c=Class.forName("com.jsl.core.Example");
       boolean b1=c.isInstance(new Example());
       System.out.println(b1);
       boolean b2=c.isInstance(new String());
       System.out.println(b2);
  }catch (Exception e) {
    System.out.println(e);
  }
 }
}

output:

true
false

Program to find the methods of the class.

package com.jsl.core;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

class Example{
 public void display(){
  System.out.println("The Display Method");
 }
 public void show(int a,int b){
  System.out.println("The Show Method");
 }
 private void print(Object object){
  System.out.println("The Print Method");
 }
}

public class GetMethods {
 public static void main(String[] args) {
  try{
     Class c=Class.forName("com.jsl.core.Example");
     Method methods[]=c.getDeclaredMethods();
     for(Method method:methods){
      System.out.println("***********************************************"); 
      System.out.println("Method modifier :" + Modifier.toString(method.getModifiers()));
      System.out.println("Method name : "+method.getName());
      System.out.println("Metho return type: "+method.getReturnType());
      Class params[]=method.getParameterTypes();
      for(int i=0;i < params.length;i++){
        System.out.println("Param [i] "+" = "+params[i]);
      }
    }
  }catch (Exception e) {
    System.out.println(e);
  }
 }
}

output:

******************************************************
Method modifier :private
Method name : print
Metho return type: void
Param 0 = class java.lang.Object
******************************************************
Method modifier :public
Method name : display
Metho return type: void
******************************************************
Method modifier :public
Method name : show
Metho return type: void
Param 0 = int
Param 1 = int

Obtaining Information About Constructors
A similar approach is used to find out about the constructors of a class.
package com.jsl.core;
import java.lang.reflect.Constructor;
class Example{
  private Example(){
  }
  private Example(int a,int b){
  }
  private Example(String name,String email){
  }
}
public class GetMethods {
 public static void main(String[] args) {
  try{
    Class c=Class.forName("com.jsl.core.Example");
    Constructor constructors[]=c.getDeclaredConstructors();
    for(Constructor constructor:constructors)
     System.out.println(constructor);
  }catch (Exception e) {
    System.out.println(e);
  }
 }
}

output:

private com.jsl.core.Example()
private com.jsl.core.Example(int,int)
private com.jsl.core.Example(java.lang.String,java.lang.String)


Finding Out About Class Fields
It's also possible to find out which data fields are defined in a class.
package com.jsl.core;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
class Example{
 private int val=23;
 final String name="jsltech";
 private final int id=1001;
 private static int count=9999;
 }
 public class GetMethods {
  public static void main(String[] args) {
   try{
      Class c=Class.forName("com.jsl.core.Example");
      Field fields[]=c.getDeclaredFields();
      for(int i=0;i < fields.length;i++){
        Field field=fields[i];
        System.out.println("Field Name : "+field.getName());
        System.out.println("Field Modifier: "+Modifier. toString(field.getModifiers()));
        System.out.println("Filed Type :"+field.getType());
        System.out.println("************************************");
      }
   }catch (Exception e) {
    System.out.println(e);
   }
 }
}

Output:

Field Name : val
Field Modifier: private
Filed Type :int
************************************
Field Name : name
Field Modifier: final
Filed Type :class java.lang.String
************************************
Field Name : id
Field Modifier: private final
Filed Type :int
************************************
Field Name : count
Field Modifier: private static
Filed Type :int
************************************


To know about the super class and implemented interfaces
package com.jsl.core;
class One{
}

interface InterfaceTwo{
}

interface InterfaceOne{
}

class Two extends One implements InterfaceOne{
}

public class GetMethods {
 public static void main(String[] args) {
  try{
     Class c=Class.forName("com.jsl.core.Two");
     System.out.println("The super class is : "+c.getSuperclass());
     Class cls[]=c.getInterfaces();
     System.out.println("The Implemented interface are :");
     for(Class cl:cls){
       System.out.println(cl);
     }
  }catch (Exception e) {
    System.out.println(e);
  }
 }
}

output:

The super class is : class com.jsl.core.One
The Implemented interface are :
interface com.jsl.core.InterfaceOne
interface com.jsl.core.InterfaceTwo

Invoking the method by Name
so far the method can be called by using object name or classes name, we can invoke the method by using reflection api, here is simple example to invoke the method
package com.jsl.core;
import java.lang.reflect.Method;
class One{
 public String fullName(String fname,String lname){
  return fname.concat(" ").concat(lname);
 }
}
public class GetMethods {
 public static void main(String[] args) {
  try{
    Class c=Class.forName("com.jsl.core.One");
    Method method=c.getMethod("fullName",new Class[] {String.class,String.class});
    String fullName=(String)method.invoke(new One(),new Object[]{"JSL","Tech"});
    System.out.println("The full name is :"+fullName);
  }catch (Exception e) {
    System.out.println(e);
  }
 }
}
Output:
The full name is : JSL Tech
Creating New Objects
There is no equivalent to method invocation for constructors, because invoking a constructor is equivalent to creating a new object (to be the most precise, creating a new object involves both memory allocation and object construction).
package com.jsl.core;
import java.lang.reflect.Constructor;
class One{
  public One(){
   System.out.println(" The constructor is invoked ");
  }
  public String fullName(String fname,String lname){
   return fname.concat(" ").concat(lname);
  }
}
public class GetMethods {
 public static void main(String[] args) {
  try{
     Class c=Class.forName("com.jsl.core.One");
     Constructor constructor=c.getDeclaredConstructor();
     One obj=(One) c.newInstance();
     System.out.println(obj.fullName("JSL", "Tech"));
  }catch (Exception e) {
    System.out.println(e);
  }
 }
}

Output:

The constructor is invoked
JSL Tech
Changing the values of field by using refilection api
package com.jsl.core;
import java.lang.reflect.Field;
class One{
  public String name="miani";
}
public class GetMethods {
 public static void main(String args[]){
  try {
      Class c=Class.forName("com.jsl.core.One");
      Field filed=c.getField("name");
      One obj=new One();
      System.out.println("The name is :"+obj.name);
      filed.set(obj, "Jsl Tech");
      System.out.println("The name after modification :"+obj.name);
  }catch (Throwable e) {
    System.err.println(e);
  }
 }
}

output:

The name is :miani
The name after modification :Jsl Tech

Monday, May 28, 2012

Spring Framework IOC


Spring Framework


Introduction:
                                                                     
  1. Spring is an Open Source framework, created by Rod Johnson in the year 2004
  2. Spring framework is created to address the complexity of Enterprise Application development
  3. Spring framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform
  4. Spring framework is a light-weight for the development of enterprise-ready applications.
  5. Spring makes it possible to use Java Beans (POJO) to achieve things that were previously only possible with EJBs.
  6. Spring framework is well-organized architecture consisting of modules. This modules are

     
    The core container

    The Core Container consists of the Core, Beans, Context, and Expression Language modules.

    The Core and Beans modules provide the fundamental parts of the framework, including the IoC and Dependency Injection features. The BeanFactory is a sophisticated implementation of the factory pattern. It removes the need for programmatic singletons and allows you to decouple the configuration and specification of dependencies from your actual program logic.

    The Context module builds on the solid base provided by the Core and Beans modules: it is a means to access objects in a framework-style manner that is similar to a JNDI registry.

    The Expression Language module provides a powerful expression language for querying and manipulating an object graph at runtime. It is an extension of the unified expression language (unified EL) as specified in the JSP 2.1 specification.

    Spring AOP

    The Spring AOP module integrates aspect-oriented programming functionality directly into the Spring framework, through its configuration management feature. As a result you can easily AOP-enable any object managed by the Spring framework. The Spring AOP module provides transaction management services for objects in any Spring-based application. With Spring AOP you can incorporate declarative transaction management into your applications without relying on EJB components.

    Spring DAO

    The Spring JDBC DAO abstraction layer offers a meaningful exception hierarchy for managing the exception handling and error messages thrown by different database vendors. The exception hierarchy simplifies error handling and greatly reduces the amount of exception code you need to write, such as opening and closing connections. Spring DAOs JDBC-oriented exceptions comply to its generic DAO exception hierarchy.
     

    Spring ORM

    The Spring framework plugs into several ORM frameworks to provide its Object Relational tool, including JDO, Hibernate, and iBatis SQL Maps. All of these comply to Springs generic transaction and DAO exception hierarchies.
     

    Spring Web module

    The Web context module builds on top of the application context module, providing contexts for Web-based applications. As a result, the Spring framework supports integration with Jakarta Struts. The Web module also eases the tasks of handling multi-part requests and binding request parameters to domain objects.

    Spring MVC framework

    The Model-View-Controller (MVC) framework is a full-featured MVC implementation for building Web applications. The MVC framework is highly configurable via strategy interfaces and accommodates numerous view technologies including JSP, Velocity, Tiles, iText, and POI.
    Test

    The Test module supports the testing of Spring components with JUnit or TestNG. It provides consistent loading of Spring ApplicationContexts and caching of those contexts. It also provides mock objects that you can use to test your code in isolation.
    Inversion of control
    In Spring framework, the Inversion of Control (IoC) principle is implemented using the Dependency Injection (DI)design pattern.

    The IoC container manages java objects – from instantiation to destruction – through its BeanFactory. Java components that are instantiated by the IoC container are called beans, and the IoC container manages a bean's scope, lifecycle events, and any AOP features for which it has been configured and coded.

    IoC example:

    Note: before exectuing the IoC example, Make sure that you have been installed spring plugins for your eclipse, and set the spring jars in the class path.
     
    1.Go go the help ---> Install new Software



    Enter the following url ins Work with: http://www.springide.org/updatesite

     
    Install the softwares.... then restart the you eclipse.. (It takes long time to install)

    Spring jars downloads



    It ask to register or without registration also you can download.

     

    Please download any one the file based according to your need.

    51.1 MB
    27.4 MB

    IoC Example:

    Welcome.java

    package com.jsl.spring.ioc;
    public class Welcome {
    private String message;
     public void setMessage(String message) {
                 this.message = message;
    }
    
    public String getMessage() {
                  return message;
         }
    }
    

    Client.java

    package com.jsl.spring.ioc;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class Client {
       public static void main(String[] args) {
         ClassPathXmlApplicationContext context =
              new ClassPathXmlApplicationContext("welcome.xml");
              Welcome welcome = (Welcome) context.getBean("welcome");
              System.out.println(welcome.getMessage());
         }
    }
    

    welcome.xml


     

    The container

             The org.springframework.beans.factory.BeanFactory is the actual representation of the Spring IoC container that is responsible for containing and otherwise managing the aforementioned beans.
            
            The BeanFactory interface is the central IoC container interface in Spring. Its responsibilities include instantiating or sourcing application objects, configuring such objects, and assembling the dependencies between these objects.

    There are a number of implementations of the BeanFactory
            
              ClassPathXmlApplicationContext
              FileSystemXmlApplicationContex
              GenericWebApplicationContext


    The most commonly used BeanFactory implementation is the XmlBeanFactory class. This implementation allows you to express the objects that compose your application, and the doubtless rich interdependencies between such objects, in terms of XML. The XmlBeanFactory takes this XML configuration metadata and uses it to create a fully configured system or application.


    Bean scopes

          In Spring 2.x or later, a bean’s scope is set in the scope attribute of the <bean> element.
          By default, Spring creates exactly one instance for each bean declared in the IoC container, and this instance will be shared in the scope of the entire IoC container.

           This unique bean instance will be returned for all subsequent getBean() calls and bean references.This scope is called singleton, which is the default scope of all beans.

    There are total 5 types of bean scopes supported :
    1. Singleton
                      Creates a single bean instance per Spring IoC container
    2. Prototype
                     Creates a new bean instance each time when requested
    3. Request
                     Creates a single bean instance per HTTP request; only valid in the context of a web application
    4. Session
                   Creates a single bean instance per HTTP session; only valid in the context of a web application
    5.GlobalSession
                   Creates a single bean instance per global HTTP session; only valid in the context of a portal application
    Singleton scope example:


    Product.java

    package com.jsl.spring.ioc;
    public class Product {
     private int pid;
     private String pname;
     private float price;
    
     public void setPid(int pid) {
       this.pid = pid;
     }
    
     public int getPid() {
       return pid;
     }
    
     public void setPname(String pname) {
       this.pname = pname;
     }
    
     public String getPname() {
        return pname;
     }
    
     @Override
     public String toString() {
        return pid + " " + pname + " " + getPrice();
     }
    
     public void setPrice(float price) {
        this.price = price;
     }
    
     public float getPrice() {
         return price;
     }
    }
    
    

    product.xml

    
    
    

    Client.java

     
    import org.springframework.context.support.ClassPathXmlApplicationContext;
     public class Client {
       public static void main(String[] args) {
        ClassPathXmlApplicationContext context =
                       new ClassPathXmlApplicationContext("product.xml"); 
     
     Product p1=(Product) context.getBean("product");
        Product p2=(Product) context.getBean("product");
        System.out.println(p1==p2);
        p1.setPrice(4500f);
        System.out.println(p1);
        System.out.println(p2);
      }
    }
    
    
    The output is:
    true
     1001 Laptop 4500.0
     1001 Laptop 4500.0
    

    if you change the bean scope to prototype .
    
       
     
    

     The ouput is :
    false
     1001 Laptop 4500.0
     1001 Laptop 3000.0
    


    Bean Naming:
    Every bean has one or more ids (also called identifiers, or names; these terms refer to the same thing). These ids must be unique within the container the bean is hosted in. A bean will almost always have only one id, but if a bean has more than one id, the extra ones can essentially be considered aliases.
    
    
    
    

    Inner beans

    A <bean/> element inside the <property/> or <constructor-arg/> elements is used to define a so-called inner bean. An inner bean definition does not need to have any id or name defined, and it is best not to even specify any id or name value because the id or name value simply will be ignored by the container.
    Example InnerBean

    Person.java

    public class Person {
      private String name;
      private String address;
      private int age;
      public void setName(String n){name=n;}
      public void setAge(int n){age=n;}
      public void setAddress(String n){address=n;}
      
      @Override
      public String toString() {
       return "Person [address=" + address + ",age=" + age + ",name=" + name + "]";
      }
    }
    
    

    Customer.java

    public class Customer
     {
       private Person person;
       public Customer(){}
       public Customer(Person person) {
         this.person = person;
       }
       public void setPerson(Person person) {
         this.person = person;
       }
       @Override
       public String toString() {
         return "Customer [person=" + person + "]";
       }
     }
    

    customer.xml

    
    
     
       
          
            
            
            
          
       
     
    
    
    Also we can use the inner bean as constructor argument.
    
       
           
           
           
         
           


    Dependencies


    Dependency injection (DI) is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean.
    This process is fundamentally the inverse, hence the name Inversion of Control(IoC), of the bean itself controlling the instantiation or location of its dependencies on its own by using direct construction of classes, or the Service Locator pattern.


    Code is cleaner with the DI principle and decoupling is more effective when objects are provided with their dependencies. The object does not look up its dependencies, and does not know the location or class of the dependencies.


    DI exists in two major variants, Constructor-based dependency injection and Setter-based dependency injection.


    Employee.java

    package com.jsl.spring.di;
    public class Employee {
     private String empno;
     private String ename;
     private Address address;
     public void setEmpno(String empno) {
      this.empno = empno;
     }
     public String getEmpno() {
      return empno;
     }
     public void setEname(String ename) {
      this.ename = ename;
     }
     public String getEname() {
      return ename;
     }
     public void setAddress(Address address) {
      this.address = address;
     }
     public Address getAddress() {
      return address;
     }
    }
    

    Address.java


    package com.jsl.spring.di;
    
    public class Address {
     private String city;
     private String state;
     private String country;
     public void setCity(String city) {
      this.city = city;
     }
     public String getCity() {
      return city;
     }
     public void setState(String state) {
      this.state = state;
     }
     public String getState() {
      return state;
     }
     public void setCoutry(String country) {
      this.country = country;
     }
     public String getCountry() {
      return country;
     }
    }
    

    MainClass.java

    package com.jsl.spring.di;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class MainClass {
     public static void main(String[] args) {
       ClassPathXmlApplicationContext context=
                                  new ClassPathXmlApplicationContext("employee.xml");
       Employee emp=context.getBean(Employee.class);
       System.out.println("The Employee Details are : ");
       System.out.println("Empno is :"+emp.getEmpno());
       System.out.println("Ename is :"+emp.getEname());
       System.out.println("City is :"+emp.getAddress().getCity());
       System.out.println("State is :"+emp.getAddress().getState());
       System.out.println("Country is :"+emp.getAddress().getCoutry());
     }
    }
    

    employee.xml

    
     
       
       
       
     
     
       
       
       
     
    
    
    The output is :
    The Employee Details are :
    Empno is :1001
    Ename is :JSLTech
    City is :Bangalore
    State is :Karnataka
    Country is :India
    

    Constructor Injection

    Employee.java

    package com.jsl.spring.di;
    public class Employee {
      private String empno;
      private String ename;
      private Address address;
      public Employee(String empno,String ename,Address address){
        this.empno=empno;
        this.ename=ename;
        this.address=address;
      }
      public String getEmpno() {
        return empno;
      }
      public String getEname() {
        return ename;
      }
      public Address getAddress() {
        return address;
      }
    }
    

    Address.java

    package com.jsl.spring.di;
    public class Address {
     private String city;
     private String state;
     private String country;
     public Address(String city,String state,String country) {
       this.city = city;
       this.state=state;
       this.country=country;
     }
     public String getCity() {
       return city;
     }
     public String getState() {
       return state;
     }
     public String getCountry() {
       return country;
     }
    }
    

    MainClass

    package com.jsl.spring.di;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class MainClass {
      public static void main(String[] args) {
        ClassPathXmlApplicationContext context=
                                    new ClassPathXmlApplicationContext("employee.xml");
        Employee emp=context.getBean(Employee.class);
        System.out.println("The Employee Details are : ");
        System.out.println("Empno is :"+emp.getEmpno());
        System.out.println("Ename is :"+emp.getEname());
        System.out.println("City is :"+emp.getAddress().getCity());
        System.out.println("State is :"+emp.getAddress().getState());
        System.out.println("Country is :"+emp.getAddress().getCountry());
      }
    }
    

    employee.xml

    
     
               
    The output is :
    The Employee Details are :
    Empno is :1001
    Ename is :JSLTech
    City is :Bangalore
    State is :Karnataka
    Country is :India
    

    Constructor argument type matching

    package com.jsl.spring.di;
    public class Employee {
      private String empno;
      private String name;
      private double salary;
      public String getEmpno() {
       return empno;
      }
      public String getName() {
       return name;
      }
      public double getSalary() {
       return salary;
      }
      public Employee(String empno, String name, double salary) {
       this.empno = empno;
       this.name = name;
       this.salary = salary;
      }
    }
    
    
          


    It leads to the exception, because of arguments mismatched. To over come this problem we can use the constructor arguments type

    
           


    we can Use the index attribute to specify explicitly the index of constructor arguments. The constructor argument starts with the 0.

    
          


    Lifecycle interfaces

    The Spring Framework provides several marker interfaces to change the behavior of your bean in the container; they include InitializingBean and DisposableBean. Implementing these interfaces will result in the container calling afterPropertiesSet( ) for the former and destroy( ) for the latter to allow the bean to perform certain actions upon initialization and destruction.
    Internally, the Spring Framework uses BeanPostProcessor implementations to process any marker interfaces it can find and call the appropriate methods. If you need custom features or other lifecycle behavior Spring doesn't offer out-of-the-box, you can implement a BeanPostProcessor yourself.

    Initialization callbacks

    Implementing the org.springframework.beans.factory.InitializingBean interface allows a bean to perform initialization work after all necessary properties on the bean are set by the container.
    org.springframework.beans.factory.DisposableBean interface allows a bean to get a callback when the container containing it is destroyed.

    Example:

    package com.jsl.spring;
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
    public class ExampleBean implements DisposableBean,InitializingBean{
    
     public void somemethod(){
      System.out.println("---------Some other method-------");
     }
     @Override
     public void afterPropertiesSet() throws Exception {
      System.out.println("----- Initilization--------");
     }
     @Override
     public void destroy() throws Exception {
      System.out.println("-----------The distructmethod-------------");
     }
    }
    

    The bean element init-method and destory-method work exact same as the The interfaces DisposableBean, InitializingBean.

    Example:

    package com.jsl.spring;
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
    public class ExampleBean implements DisposableBean,InitializingBean{
    
     public void somemethod(){
      System.out.println("---------Some other method-------");
     }
     @Override
     public void afterPropertiesSet() throws Exception {
      System.out.println("----- Initilization--------");
     }
     @Override
     public void destroy() throws Exception {
      System.out.println("-----------The distructmethod-------------");
     }
    }
    

    The bean element init-method and destory-method work exact same as the The interfaces DisposableBean, InitializingBean.

    Example:

    package com.jsl.spring;
    public class ExampleBean {
     public void initilization(){
      System.out.println("----- Initilization--------");
     }
     public void somemethod(){
     }
     public void distructMethod(){
      System.out.println("-----------The distructmethod-------------");
     }
    }
    
    
    
    
    

    with annotation also we change the behavior of your bean in the container

    Example:

    package com.jsl.spring;
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    public class ExampleBean {
     @PostConstruct
     public void initilization(){
      System.out.println("----- Initilization--------");
     }
     public void somemethod(){
      System.out.println("----------SomeMethod---------");
     }
     @PreDestroy
     public void distructMethod(){
      System.out.println("-----------The distructmethod-------------");
     }
    }
    

    MainClass.java

    package com.jsl.spring;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class MainClass {
    public static void main(String[] args) {
      ClassPathXmlApplicationContext context=
                                      new ClassPathXmlApplicationContext("example.xml");
      ExampleBean obj=context.getBean(ExampleBean.class);
      obj.somemethod();
      context.close();
     }
    }
    

    The output:

    ----- Initilization--------
    ------SomeMethod-----------
    ------The distructmethod-------------
    



    Autowiring:


    The Spring IoC container can help you to wire your beans automatically. You only have to specify the
    auto-wiring mode in the autowire attribute of <bean>.
    no

    No autowiring at all. Bean references must be defined via a ref element. This is the default, and changing this is discouraged for larger deployments, since explicitly specifying collaborators gives greater control and clarity. To some extent, it is a form of documentation about the structure of a system.
    byName
    Autowiring by property name. This option will inspect the container and look for a bean named exactly the same as the property which needs to be autowired. For example, if you have a bean definition which is set to autowire by name, and it contains a master property (that is, it has a setMaster(..) method), Spring will look for a bean definition named master, and use it to set the property.
    byType
    Allows a property to be autowired if there is exactly one bean of the property type in the container. If there is more than one, a fatal exception is thrown, and this indicates that you may not use byType autowiring for that bean. If there are no matching beans, nothing happens; the property is not set. If this is not desirable, setting the dependency-check="objects" attribute value specifies that an error should be thrown in this case.
    constructor
    This is analogous to byType, but applies to constructor arguments. If there isn't exactly one bean of the constructor argument type in the container, a fatal error is raised.
    Autodetect
    Chooses constructor or byType through introspection of the bean class. If a default constructor is found, the byType mode will be applied.


    Auto-wiring byName Example

    Member.java

    package com.jsl.spring;
    public class Member {
     private String mid;
     private String name;
     private Address address;
     public String getMid() {
      return mid;
     }
     public void setMid(String mid) {
      this.mid = mid;
     }
     public String getName() {
      return name;
     }
     public void setName(String name) {
      this.name = name;
     }
     public Address getAddress() {
      return address;
     }
     public void setAddress(Address address) {
      this.address = address;
     }
    }
    

    Address.java

    package com.jsl.spring;
    public class Address {
     private String city;
     private String state;
     private String country;
     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;
     }
     public String getCountry() {
      return country;
     }
     public void setCountry(String country) {
      this.country = country;
     }
    }
    

    MainClass.java

    package com.jsl.spring;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class MainClass {
      public static void main(String[] args) {
       ClassPathXmlApplicationContext context=
                                       new ClassPathXmlApplicationContext("member.xml");
       Member member=context.getBean(Member.class);
       System.out.println(member.getMid());
       System.out.println(member.getName());
       System.out.println(member.getAddress().getCity());
       System.out.println(member.getAddress().getState());
       System.out.println(member.getAddress().getCountry());
     }
    }
    

    member.xml

    
     
       
       
     
     
       
       
       
     
    
    

    Output:

    SA1001
    JSLTech
    Bangalore
    Karnataka
    India
    
    When specify auto-wire byName, auto-wire bean name and property bean name should be same and it also case sensitive.
    Auto wire by constructor :

    Member.java


    package com.jsl.spring;
    public class Member {
     private String name;
     private Address address;
     public Member(String name,Address address) {
      this.name=name;
      this.address=address;
     }
     public String getName() {
      return name;
     }
     public Address getAddress() {
      return address;
     }
    }
    

    Address.java

    package com.jsl.spring;
    public class Address {
     private String city;
     private String state;
     private String country;
     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;
     }
     public String getCountry() {
      return country;
     }
     public void setCountry(String country) {
      this.country = country;
     }
    }
    

    MainClass.java

    package com.jsl.spring;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class MainClass {
      public static void main(String[] args) {
        ClassPathXmlApplicationContext context=
                                       new ClassPathXmlApplicationContext("member.xml");
        Member member=context.getBean(Member.class);
        System.out.println(member.getName());
        System.out.println(member.getAddress().getCity());
        System.out.println(member.getAddress().getState());
        System.out.println(member.getAddress().getCountry());
      }
    }
    

    member.xml

    
      
                      

    output

    JSLTech
    Bangalore
    Karnataka
    India
    










Spring Boot 3 : JWT with SecurityFilterChain, AuthorizeHttpRequests, RequestMatchers

pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"...