It is the only abstract interface in the java.lang package. It is the basic interface containing non-implemented methods all regarding and involving characters only like length() , charAt() etc and a special method i.e toString() method. It is the super class of many of the classes and interfaces, which are shown below.

Implementation class for the abstract method declared in the in the CharSequence interface are:-

AbstractStringBuilder (c)   of [ java.lang package ]

String (c)                             of [ java.lang package ]

StringBuffer (c)                   of [ java.lang package ]

StringBuilder (c)                 of [ java.lang package ]

CharBuffer (c)                     of [ java.nio package ]

Segment (c)                         of [ javax.swing.text package ]

Note : (c) represent the class. All the above mentioned names are java class.


Methods of the CharSequence interface :

public abstract int length();

public abstract char charAt(int i);

public abstract CharSequence subSequence(int i, int j);

public abstract String toString();


Some of the important points of CharSequence interface :

* It was first introduced with the release of JDK 1.4.
* CharSequence is originally is the sequence of character and which combines to form a String only,
so we can call a string as the CharSequence.



Java is using java.lang as the default package.All the basic functionality for writing a simple java program are there written in the form of classes and interfaces and also designed some of the possible and probable exception and error raised while compiling and executing programs. Here i am categorizing all the exception, error , abstract class , final classes, interfaces etc....

Classes in java.lang packages


List of Abstract Interfaces (1)

CharSequence

List of Abstract classes (5)

AbstractStringBuilder

ClassLoader

Enum

Number

Process

List of Final classes (18)

Boolean 

Byte

Character

Class

ConditionalSpecialCasing

Double

Float

Integer

Long

Math

ProcessBuilder

ProcessEnvironment

ProcessImpl

Short

String

StringBuilder

StringBuffer

System


List of Classes (18)

CharacterData00

CharacterData01

CharacterData02

CharacterData0E

CharacterDataLatin1

CharacterDataPrivateUse

CharacterDataUndefined

Error

Exception

Object

Runtime

SecurityManager

Shutdown

StringCoding

Thread

ThreadGroup

ThreadLocal

Throwable

List of Exception Classes (25)

ArithmeticException

ArrayIndexOutOfBoundsException

ArrayStoreException

ClassCastException

ClassNotFoundException

CloneNotSupportedException 

EnumConstantNotPresentException

IllegalAccessException

IllegalArgumentException

llegalMonitorStateException

IllegalStateException

IllegalThreadStateException

IndexOutOfBoundsException

InstantiationException

InterruptedException

NegativeArraySizeException

NoSuchFieldException

NoSuchMethodException

NullPointerException

NumberFormatException

RuntimeException

SecurityException

StringIndexOutOfBoundsException

TypeNotPresentException

UnsupportedOperationException


List of Error Classes (18)

AbstractMethodError

AssertionError

ClassCircularityError

ClassFormatError

ExceptionInInitializerError

IllegalAccessError

InstantiationError

InternalError

LinkageError

NoClassDefFoundError

NoSuchFieldError

NoSuchMethodError

StackOverflowError

UnknownError

UnsatisfiedLinkError

UnsupportedClassVersionError 

VirtualMachineError

VerifyError 



Java Packages is the collection of pre-defined classes interfaces and thus all these classes contains both the implemented and non-implemented methods. Java is using java.lang package as the default package. Below are the list of some of the packages in java.

java.lang package

java.util package

java.io package

java.math package

java.rmi package

java.sql package

A java package is the collection of similar types. After Writing all the required pre-defined classes in java, those are being categorized in different categories and hence are termed as packages. For ex-

java.lang packages contains all the basic operations which are required for writing a simple java application and hence is the default package for any java class. Like that ,

java.io packages contains all the required classes and functionality for doing input and output operations using file and console.

As the java vendors has categorized the different classes in different packages, the same we can do while developing our own java application, which makes us to say that packages can be used to search and locate the classes and interfaces. Best practice is to always keep the classes in a packages which must be followed by three dots naming convention.

For ex-  com.def.core ---> A package name , followed with three dots.

Note: Package must always be the first statement of any java class and can be only one package statement in each java file.

package is the keyword used before declaring any package name in the java source file.
For ex-    package  com.def.core.* ;

In the above statement .* indicate we are supposed to use all the classes defined in the com.def.core package.








Following are task done by the Proxy Object when we calls the business logic on the proxy object.

1.  First proxy checks whether the Business logic( methods ) to be invoked is available with the target class object or proxy interface specified by the developer in the Spring Configuration while configuring the proxy for the target class. 

2. If it found either with the target class or the proxy interface, collects the data configured in the List of InterceptorName wile configuring the ProxyFactoryBean in the XML document.

Note : List of InterceptorName contains list of Advice class object. 

3. Proxy checks for the list of Advice class object in the List of InterceptorName, to invoke the methods defined in the Advice class by getting its Object form the list.  


Note: Normally 3 method are invoked sequentially i.e,
a) before()             --> First called defined in one of the Advice class.
b) afterReturning() --> Called after before() defined in another class.
c) afterThrowing    --> Called only when exception thrown by the . 


4. All the methods are called sequentially by their respective Target Objects , present in the Spring Container. 

Note : Respective methods will be called on the basis of Advice class implementing following Interfaces. 

a) MethodBeforeAdvice  ( has before() )
b) AfterReturningAdvice  ( has afterReturning() )
c) ThrowAdvice              ( has afterThrowing() )


5. If the Business Logic executes and returns control successfully then proxy checks for any Interceptor(Advice) which has to be applied after executing successfully. 

6. If not executed successfully that means exception will be thrown and then afterThrowing() will be executed. 


7. Proxy checks for the Advice class implementing ThrowAdvice interface to class the overridden afterThrowing() method. 

Important points 

1. before () method is present in the MethodBeforeAdvice interface , so need to override in our Advice class and always will be called before executing any business logic. 

2. afterReturning() method is present in the AfterReturningAdvice interface, so need to override in our Advice class and always will be executed if business logic executes successfully. 

3. afterThrowing() method is present in the ThrowAdvice interface, so need to override in our Advice class and always will be executed only if business logic fails to executes and throws exception.  
This is the very first technique to implement Spring AOP using Spring API. Later they introduces Annotation and XML schema concept which itself uses Spring API but with Annotation/ XML schema approach. 

Using ProxyFatoryBean we are approaching or implementing AOP in our service layers to our business operations to wrap business operations with the middle level services.
In this approach we are creating the proxies either of the target class or the interfaces. 


Creating the proxy of the interface based Services classes are called as Interface based Proxying where as, Creating the proxy of the object of the Services classes are called as Class based Proxying. 

So basically we are writing our services class in with ways :-
1. Class model
2. Interface model.

Class Model : In this model we are simply creating a service class having different business logic with its implementation. And we are creating proxy of this service class using ProxyFactoryService.

For ex:

Class AccountService{         // Business Service
     
    public void addAccount(){ // Business Logic
        .....
    }
    public void desposit(){   // Business Logic
        .....
    }
}


Interface Model : In this approach we are creating a service class which implements an interface having defines different business logic and the class implementing this interface have the implementation of the business logic defined in its Interface.

For ex:

Interface 
interface AccountService{   
     
    public void deposit();  //Business Logic
  
 }

Business Service class
Class AccountServiceImpl implements AccountService{ // Business Service

    public void desposit(){   //Business Logic
        .....
    }

Steps to be followed for implementing AOP using Spring API

1. Consider the business services written above.
2. Configure these Business services in the Spring Configuration document. For ex -

<bean id="asTarget" class="......AccountService" /> (class based)
<bean id="asTarget1" class="......AccountServiceImpl" /> (Interface based)

Note : From above both the bean configuration we have to use any of the approach either class model or interface model but recommendable is to use Interface model. 

3. Consider the required middle level services and write its services class. For ex-
I m considering three middle level service classes.
a) TxService.class ( Transaction Service class ) 
b) SecurityService.class (Security Service class )
c) LogService.class ( Logging Service class )

4. Configure the above bean in the Spring Configuration file. 

<bean id="tx" class="......TxService" /> 
<bean id="sc" class="......SecurityService" />
<bean id="lg" class="......logService" /> 

Note : The above middle level services are applied at the joinPoints of the Business operations. So for applying joinPoints we need to write some Advice class, as follows

5. Basically we have three joinPoints and grammatically all these 3 can be used with the predefined methods.
a) Method Before JoinPoint implemented using before(...) method.
b) Method Returning joinPoint can be implemented using afterReturning(...) method.
c) Method Throeing joinPoints can be implemented using afterThrowing(...) method. 

6. Write the required advcies as per the joinPoints. 

MBAdvice class 
Class MBAdvice implements methodBeforeAdvice{ //Advice class

    public void before(){  

      // Verify User logic
      // log the Information
      // Begin Transaction  
    }
}


MRAdvice class 

Class MRAdvice implements AfterReturningAdvice{ // Advice class

    public void before(){  

      // Transaction Commit
      // Log the Information
    }
}

MTAdvice class 
Class MTAdvice implements ThrowAdvice{ // Advice class

    public void before(){  

      // Rollback Transaction
      // Log the Information 
    }
}

7. Configure the above advcies class in the Spring Configuration file.
<bean id="mba" class="......MBAdvice" /> 
<bean id="mra" class="......MRAdvice" />
<bean id="mta" class="......MTAdvice" /> 

8. Configure the ProxyFactoryBean for creating the proxy of the business service class. 

<bean id="baseProxy" class="org.springframework.aop.framework.ProxyFactoryBean"abstract="true"> 
 <property name="InterceptorsNames">
   <list>
      <value> mba </value>
      <value> mra </value>
      <value> mta </value>
   </list>
 </property>  
</bean>
 

Note:
1. Using abstract="true " means instance will not be created for the ProxyFactoryBean class.
2. Configuring Advice class bean instance using the property tag and storing in the list as because all the information which is applied to the business operations are present in Advice classes instance. So need to configure with the Proxy class.

9. Configure Proxy object for our business Services.

Configuration of Class based proxy 
<bean id="asProxy" parent="baseProxy" > 
 <property name="targetClass" value="...........AccountService" />  
  <property name="target" ref="asTarget" />  
</bean>

Configuration of Interface based proxy 
<bean id="asProxy1" parent="baseProxy" > 
 <property name="proxyInterface" value="...........AccountServiceImpl" />  
  <property name="target" ref="asTarget1" />  
</bean>

Note :
1. In both the approaching have to follow any of these and recommendation is to use Interface based proxy.
2. In the immediate configuration parent="baseProxy" means the proxy created for the configured classes will be the sub-class of ProxyFactoryBean.

Important Point :
1. By the above configuration Container create the proxy for the target object of the Business service classes configured in the document.
2. That proxy object will be wrapped with the different middle level services which is done by the Spring Container while creating proxy with the help of the Advice class configured in the ProxyFactoryBean bean configuration in the list.   

There are some important terms in Spring AOP which we need to know about. All total there are 8 terms which are as follows :
1. Aspect
2. Advice
3. JoinPoint
4. PointCut
5. Advisor
6. Target
7. Proxy
8. Weaving

Aspect : It is one of the main concept in AOP which is programmatically represented as Middle Level Services(Sercurity, transaction, log etc) . Advices are applied to the Aspect.

For ex- Aspects are the middle level services like SecurityService, LogService, TransactionService etc are applied to the three JoinPoints of the Business Logic through Advices. 

Advice :  It is the required implementation of the Middle Level Services which tells about the method execution of different Aspects at the joinPoint. 
Advice is a Java class which contains the information about the execution of method of Middle level services at the different joinPoints. So basically it tells the container what to execute?  


JoinPoint : It is point or concept which tells where we want to apply Advice. It tells container when to execute?
Basically there are three joinPoints are provided by the Spring AOP i.e

1. Method Before 
2. Method Returning 
3. Method Throwing  

a) Method Before : It tell the container to execute the codes before the core logic to be execute. It is provided by the Spring in the form of a method called Before() present in the MethodBeforeAdvice interface.

b) Method Returning : It tells the container to execute the codes after successfully returning the control from the core logic. It is iimplemented using methodAfterReturning() method present in the methodAfterReturningAdvice interface.

c) Method Throwing : It tells the container to execute the codes when container throws execution while executing the core logic. It is implemented using methodAfterThrowing() method provided in the ThrowsAdvice interface. 

PointCut : It is the collection of JoinPoints. Previously JoinPoints are common for all core logic but now with the pointCut we have the option to tell Container to apply which JoinPoints to which core logics. 

Advisor : For telling the Container to apply these JoinPoints to the selected core logic we need joinPoint and Advice to configure in a Advisor. So Advisor is a collection of both JoinPoints and PointCuts. 

Target : It is the main object of our Business Service before applying the Advices or Advisors.It will directly call and execute the codes of core logic. 

Proxy : It is an object of our business service after applying Advices or Advisors. Calling the core logic with the Proxy calls the dynamic code generated by the Container which contains advices with the core logic. 

Weaving : It is the process of applying the Advice/Advisor to the target objects at given PointCuts to get the proxy objects. 




AOP is termed as Aspect Oriented Programming. Spring AOP is a concept which overcomes the limitations with the simple OOPs concepts. It is a new kind of programming technique which gives the facility to add the functionality dynamically. Here Spring Container is responsible to generating and executing the code dynamically, only we need to configure in the XML or through Annotation. Technically Middle Level Services termed as Aspect and practically we are Aspects to the main business logics at the joinPoints.

For ex- Let suppose if we have a business logic i.e deposit() and we are supposed to apply some middle level services to this business logic at the JoinPoints. 

Now the Question arises is where to apply Advices ?
Basically Advices are applied to three points , they are termed as JoinPoints. So there are Three joinPoints where we can apply Advice( Middle level services).

Comparison of Technical terms of AOP with the practical view

Aspect Middle Level Services
Advice Implementation of Middle Level Services
JoinPoints Where to Apply Advices?

Some of the Spring AOP Terminology we need to know
1. Aspect
2. Advice
3. JoinPoints
4. PointCut
5. Advisor
6. Target
7.  Proxy
8. Weaving.

Business Services -> It is the common services we come across in distributed applications.
Like CustomerServices , AccountServices etc.
It contains the set of Business Logic.

Business Services = set of Business Logics.

Business Logic -> It the main logic which is to be performed and it contains set of business operations.
For Example deposit(), withdraw(), getBalance() are the business logics under some business Services.
Advices are applied to the Business logic.

Business Logic = Set of business Operations

Some of the Advice/ Middle Level Services that we are attaching with the Aspects ( Business Logic ) are
Security Service , Transaction Service , log Service etc.

Spring provides three way to implement the Spring AOP :
1. Using Spring API based AOP ( 1.0 )
2. Using Spring Annotation based AOP ( 2.0 ) - > Recommended to use
3. Using XML schema based AOP ( 2.0 ) 
@Autowired can also be used with Setter method though Spring Container detects the bean instance using byType process only and inject the bean property using Setter Injection. Using @Autowired with the Setter means wiring at the property head. There will not be any need to configure XML with the <constructor-arg> tag. It is same as byType autowiring prcoess with a difference of mapping style. 

Example showing the Annotation on Setter :
There are two bean class A and Hello and Hello is having dependency of A.

A.java
public class A {  } 

Hello.java
public class Hello {
  
 A a;                                 // Bean Property
public Hello(){ //Default Constructor System.out.println("Hello instance "); } public Hello(A a){ System.out.println("Arg Cons"); //Constructor Injection this.a = a; } @Autowired //Annotation on properties public void setA(A a) {  System.out.println("Setter Injection"); //Setter Injection this.a = a; }
   public static void main(String[] args) {
  
       ApplicationContext ctx= new ClassPathXmlApplicationContext("SpringCore.xml");
       Hello hello =(Hello)ctx.getBean("hello");
 }

}

SpringCore.xml
<bean id="a" class="com.def.A"> 
</bean>

<bean id="hello" class="com.def.Hello" >
</bean>

Output
Def A cons
Hello Instance
Setter Injection

In the above example
1. Instance of bean A will be created by calling default constructor.
2. Instance of bean Hello will be created by calling default constructor.
3. Bean property will be injected using Setter Injection.

Note : In the above example has output of argumented constructor hence uses Constructor Injection for injecting bean property.

Note: In the above example it is using @Autowired with the Setter that means Setter Injection, so the instance of the Hello is created first and then inject the Bean property.

@Autowired can also be used with Constructor by which Spring Container detects the bean instance using byType process but inject the bean property using Constructor Injection. Using @Autowired with the Constructor, there is not need to configuring XML with the <constructor-arg> tag. It is same as constructor autowiring prcoess with a difference of mapping style. 

Example showing the Annotation on Constructor :
There are two bean class A and Hello and Hello is having dependency of A.

A.java
public class A {  } 

Hello.java
public class Hello {
  
 A a;                             // Bean Property
public Hello(){ //Default Constructor System.out.println("Hello instance "); } @Autowired //Annotation on properties public Hello(A a){ System.out.println("Arg Cons");//Constructor Injection this.a = a; } public void setA(A a) { //Setter Injection this.a = a; } public static void main(String[] args) { ApplicationContext ctx= new ClassPathXmlApplicationContext("SpringCore.xml"); Hello hello =(Hello)ctx.getBean("hello"); } }

SpringCore.xml
<bean id="a" class="com.def.A"> 
</bean>

<bean id="hello" class="com.def.Hello" >
</bean>

Output
Def A cons
Arg Cons

In the above example ,
1. Instance of bean A will be created by calling default constructor.
2. Instance of bean Hello will be created by calling argumenetd constructor.
3. Bean property will be injected using Constructor Injection.

Note : In the above example has output of argumented constructor hence uses Constructor Injection for injecting bean property.

@Autowired is used on Properties, which means Spring Container detects the bean instance using the byType process but injection is not with Setter method.This is how byType auto-wiring process is different from Annotation based auto-wiring. Hence annotation tells no need to write any setter method or constructor for injecting the bean property.

Example showing the Annotation on Properties :
There are two bean class A and Hello and Hello is having dependency of A.

A.java
public class A {  } 

Hello.java
public class Hello {
  
 @Autowired           //Annotation on properties
 A a;
public Hello(){ //Default Constructor System.out.println("Hello instance "); } public Hello(A a){ //Constructor Injection System.out.println("Arg Cons"); this.a = a; } public void setA(A a) { //Setter Injection this.a = a; } public static void main(String[] args) { ApplicationContext ctx= new ClassPathXmlApplicationContext("SpringCore.xml"); Hello hello =(Hello)ctx.getBean("hello"); } }

SpringCore.xml
<bean id="a" class="com.def.A"> 
</bean>

<bean id="hello" class="com.def.Hello" >
</bean>

Output
Def A cons
Hello instance

In the above example
1. Instance of bean A will be created using default constructor.
2. Instance of bean Hello will be created using default constructor.
3. Bean property will be injected but without setter and constructor injection.

Note : In the above example output doesn't involve any constructor or setter injection hence proved that it follows different approach for injecting the bean property rather using Constructor or Setter Injection.

Note : Annotation on properties rather follows different approach to inject the bean property.


Auto-wiring in spring can also be achieved through Annotation. Annotation is the simplest way to achieve wiring in Spring as it doesn't involve any extra code in XML file. It can be parsed by the Spring container at the time of reading the bean class. An the strange fact is that annotation auto-wiring doesn't use any of the constructor or setter injection for injection the bean property else it uses its own way to inject the bean property. But bean will be detected using the byType process. Annotation can be used with the bean properties, setter method and constructor.

@Autowired is the annotation can be used with the bean properties, constructor and Setter method.

1. Annotation on properties
2. Annotation on constructor
3. Annotation on Setter
4. Annotation on properties where ( required = false )

Note : By default annotation in Spring is disabled, we have to enable it manually by modifying the XML file.

For Enabling Annotation in Spring 

Why Annotation is by default disable in Spring ?
It is because in Spring all the functionality is not supported by annotation rather it is possible with using XML file.

Cyclic dependency is the one of the major limitation of the Injection process in Spring. Constructor Injection has chance of having Cyclic Dependency, while the same is not possible in case of Setter Injection.

When both the beans depends on each other for injecting the bean property then it forms the Cyclic Dependency. In Cyclic Dependency containers goes on infinite loop for getting the bean instance, so to avoid this type of situation ( Cyclic Chaining ) it finally create the instance of second bean by calling default constructor. So as a result only one bean will be injected and another remain uninjected.

To better understand the above statement lets take an example :
There are two bean A and B and both depends on each other.

Case 1: With Constructor Injection

A.java
public class A {

   B b;               // Bean property
 
   public A(B b) {
      this.b = b;    // Constructor Injection
   }
}

B.java
public class B {

   A a;               // Bean property
 
   public B(A a) {
      this.a = a;    // Constructor Injection
   }
 public static void main(String[] args) {
     ApplicationContext ctx=new ClassPathXmlApplicationContext("Spring-Core.xml");
     Hello hello = (Hello)ctx.getBean("hello");
  
 } 
}

Spring-Core.java

<?xml version="1.0" encoding="UTF-8"?>
<beans 
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="a" class="com.def.A" autowire="constructor">
</bean>

<bean id="b" class="com.def.B" autowire="constructor">
</bean>

</beans>

Explanation: 
1. Read the first bean tag in XML file and found one dependencies of bean B. 
2. Try to get the instance of bean B but not running in the Spring Container.
3. Try to create the instance by reading the bean definition of B.
4. But again found the dependency of A on B. 
5. Now Container try to get the instance of bean A ( Which already have dependency ) . 
6. Like this it will form infinite loop and at last one bean will be injected by avoiding the Cyclic chaining.   

Note: This happens all because in case of Constructor Injection
1. Container first inject the dependencies.
2. Then create the instance of the current bean. 

So in the above condition it both the bean cannot be injected and hence non of the instance will be created and hence form the Cyclic Dependency but to avoid Cyclic dependencies , container create the instance of second bean by calling the default constructor. 

Case 2: With Setter Injection

A.java
public class A {

   B b;               // Bean property
 
   public setA(B b) {
      this.b = b;    // Constructor Injection
   }
}

B.java
public class B {

   A a;               // Bean property
 
   public setB(A a) {
      this.a = a;    // Constructor Injection
   }
 public static void main(String[] args) {
     ApplicationContext ctx=new ClassPathXmlApplicationContext("Spring-Core.xml");
     Hello hello = (Hello)ctx.getBean("hello");
  
 } 
}

Spring-Core.java

<?xml version="1.0" encoding="UTF-8"?>
<beans 
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="a" class="com.def.A" autowire="byType">
</bean>

<bean id="b" class="com.def.B" autowire="byType">
</bean>

</beans>

Explanation: 

1. Read the first bean tag in XML file and found one dependencies of bean B. 

2. Container create the instance of A and try to inject the dependencies. 

2. So try to get the instance of bean B to Inject the bean property but not running in the Spring Container.
3. Then try to create the instance by reading the bean definition of B.
4. Again found the dependency of A on B and again before injecting the dependencies on B, first create the instance of B.  
5. Now Container try to inject the A's bean property because its instance is available . 
6. And finally Instance B is also available and inject the bean property of B.  

Note: This happens all because in case of Setter Injection
1. Container first create the instance of the current bean 
2. Then Inject the dependencies if any.  

So like this no chance of having any Cyclic Dependency in Setter Injection because of creating the current bean instance first, so anyhow a bean instance will be available to inject the bean properties.


In Spring wiring means configuration the bean dependency if any. It may be done explicitly by hard-coding the code in XMl file or implicitly by mentioning the required type of autowiring process in the XML file. Autowiring process is further divided into two category :
1. XML based Auto-Wiring process.
2. Annotation based Auto-Wiring process.

Explicitly wiring the dependencies in the XML is otherwise also called as XML based Explicitly Wiring.

XML based Auto-Wiring process 
There are four type by which we can achieve XML based autowiring, i.e
1. byName autowiring 
2. byType autowiring 
3. constructor autowiring 
4. Autodetect autowiring 

All the above auto-wiring process can be done by writing the respective values to the autowire attribute in the XML file. Like this,

<bean id=" " class=" " autowire="xxx" />
xxx can be :

a. byName         b. byType        c. constructor         d. autodetect

Annotation based Auto-Wiring
There are two annotations facility available in the Spring for bean dependency detection and injection. Injecting the bean through annotation based wiring is knows as Field Injection.
We are writing annotation with the field attributes that why it is named as Field Injection.

The two types of Annotation based Auto-Wiring are :
1. @Autowired annotation.
2. @Resource annotation.



It is a type of auto-wiring in which the injection procedure may be constructor or setter injection , depends on the argumented constructor is there in the bean. Container gives priority for the constructor injection and search for any argumented defined in the bean for injecting the bean property , if yes then injected and if not then setter injection is used for injecting the bean property. 

Autodetect wiring process is not in the Spring 3.0 as it doesn't have any additional features. Autowire attributes the value " autodetect " to use the facility for its injecting procedure. 

Container steps for Autodetect wiring prcoess.
1. Container checks for the instance running.
2. If not then search its bean definition and create the bean instance.
3. If yes then search for its matching constructor , if found then inject the bean property using Constructor Injection.
4. If not found then search for setter method, if found then inject the bean property using setter Injection with byType matching procedure.
5. If no bean found then bean property will remain uninjected.
6. If neither found matching constructor or matching setter method then also bean property will remain uninjected.

Example of constructor type autowire :
Let suppose there are two beans A and Hello and Hello is having on dependency i.e A .

Hello.java
public class Hello {
 
 A a;
  
   public Hello(A a) {
      this.a = a;
   }
 
   public static void main(String[] args) {
     ApplicationContext ctx=new ClassPathXmlApplicationContext("Spring-Core.xml");
     Hello hello = (Hello)ctx.getBean("hello");
  
 } 
}

Spring-Core.java

<?xml version="1.0" encoding="UTF-8"?>
<beans 
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="a" class="com.def.A">
   <constructor-arg value="10"></constructor-arg> // Value of x
</bean>

<bean id="hello" class="com.def.Hello" autowire="autodetect">
</bean>

</beans>


By Constructor :

A.java
public class A {

   int x;            // Bean property
 
   public A(int x) {
      this.x = x;    // Constructor Injection
   }
}


By Setter : 

A.java
public class A {

   int x;               // Bean A Property
 
   public setA(int x) {
     this.x = x;        //Setter Injection
   }
}

Note:
In autodetect process :
* Container first checks for Constructor and if found any matching constructor then inject bean property using Constructor Injection.
* If not found any matching constructor then search for setter method and if found inject the bean property using the setter injection.


Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic