Friday, June 21, 2013
Spring : Types of Advice
- Before advice – Run before the method execution
- After returning advice – Run after the method returns a result
- After throwing advice – Run after the method throws an exception
- Around advice – Run around the method execution, combine all three advices above.
1. Before advice
implements MethodBeforeAdvice
public void before(Method method, Object[] args, Object target)
id=""
class="org.springframework.aop.framework.ProxyFactoryBean">
name="target" ref="customerService" />
name="interceptorNames">
>
>interceptor name
2. After returning advice
implements AfterReturningAdvice
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
3. After throwing advice
public class HijackThrowException implements ThrowsAdvice
public void afterThrowing(IllegalArgumentException e) throws Throwable
4. Around advice
public class HijackAroundMethod implements MethodInterceptor
Object result = methodInvocation.proceed();
Wednesday, June 12, 2013
Spring - AutoWiring
Auto wiring how you want to relate the dependencies in spring xml file
1) Default
2)By Name
3) By Type
4)Constructor
5)Auto-Detect:
autowire="autodetect" dependency-check="objects />
1) Default
2)By Name
3) By Type
4)Constructor
5)Auto-Detect:
autowire="autodetect" dependency-check="objects />
Spring - Ambiguities in Constructor Dependency Injection
IF you are using constructor injection sometimes you need to explicitly specify data type along with constructor arguments as spring converts values like 120, 245 from String to int.
Hence add type property to constuctor-arg tag
188
Hence add type property to constuctor-arg tag
Spring - Multiple Spring xml files
If you have modules in your code or if you want to break your large spring xml in smaller parts
or you can use @import in HelperClass
@Configuration
@Import({ CustomerConfig.class, StaffConfig.class })
Declare Beans
@bean and name attribute to declare beans
or you can use @import in HelperClass
@Configuration
@Import({ CustomerConfig.class, StaffConfig.class })
Declare Beans
@bean and name attribute to declare beans
Spring - A normal Spring appliation
You will have a interface like
public interface Vehicle{
public void noOfWheels();
}
You will have classes implementing the interfaces
public class Car implement Vehicle{
public void noOfWheels(){
}
}
public class Truck implement Vehicle{
public void noOfWheels(){
}
}
A Helper Class
public class transport {
1)Vehicle vehicle;
2)setter for vehcile
public wheel(){
vehicle.noOfWheels();
}
}
A Calling Class
public class SpringDemo{
ApplicationContext app = new ClassPathApplicationContext ("spring.xml");
Transport transport= (Transport )app.getBean("transport");
transport.wheel();
}
Spring configuration file
public interface Vehicle{
public void noOfWheels();
}
You will have classes implementing the interfaces
public class Car implement Vehicle{
public void noOfWheels(){
}
}
public class Truck implement Vehicle{
public void noOfWheels(){
}
}
A Helper Class
public class transport {
1)Vehicle vehicle;
2)setter for vehcile
public wheel(){
vehicle.noOfWheels();
}
}
A Calling Class
public class SpringDemo{
ApplicationContext app = new ClassPathApplicationContext ("spring.xml");
Transport transport= (Transport )app.getBean("transport");
transport.wheel();
}
Spring configuration file
Spring - Advantages and features of Spring
1)Ligtweight, less overhead
2)Spring IOC
3)Loose coupling
http://javaqafaq.blogspot.in/2013/06/spring-normal-spring-appliation.html
Here in this example tomorrow if apart from car or truck , you want to introduce new vehicle say Bus.
You just need to add the interface and make changes only to Spring.xml
4)Good integration with ORM tool like Hibernate, Ibatis
5)Good integration with Struts and Struts framework which are widely used in industry.
2)Spring IOC
3)Loose coupling
http://javaqafaq.blogspot.in/2013/06/spring-normal-spring-appliation.html
Here in this example tomorrow if apart from car or truck , you want to introduce new vehicle say Bus.
You just need to add the interface and make changes only to Spring.xml
4)Good integration with ORM tool like Hibernate, Ibatis
5)Good integration with Struts and Struts framework which are widely used in industry.
Spring - Types of Dependency Injection
Constructor Injection
The Helper class should have a Constructor
The Helper class should have a Constructor
Setter Injection
The helper class should have a setter which will be set in spring.xml
Spring - Dependency Injection / Inversion of Control
Dependency Injection or Inversion of control means giving the control or task of injecting dependencies i.e object creation to the container. Instead of creating objects in code let container handle it for you.
In spring, you can have a normal xml file where you declare all your objects and services and which services are required by which object inside the xml. The Spring container will wire them and create those objects for you.
In spring, you can have a normal xml file where you declare all your objects and services and which services are required by which object inside the xml. The Spring container will wire them and create those objects for you.
Monday, June 3, 2013
Sunday, June 2, 2013
What is IOC.
Inversion of Control or Dependency Injection means passing on the control to Spring container.
In IOC, the objects are not created in code but it is described in a configuration file, how the objects are to be created. the container takes the responsibilty
In IOC, the objects are not created in code but it is described in a configuration file, how the objects are to be created. the container takes the responsibilty
Subscribe to:
Comments (Atom)