Nearby lessons
10 of 35Spring - Method Replacement
- Understand Spring - Method Replacement
- See working code examples
- Learn from common mistakes and Q&A
Learn Spring - Method Replacement step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.
Method Replacement
It is one type of method injection, where we will give an intimation to IOC Container
about to replace the existed method implementation with some other method
implementation.
To achieve Method Replacement, we have to declare an user defined bean class with a
method whose implementation we want to replace and we will provide new
implementation for that method by declaring a class and by implementing
"MethodReplacer" interface.
MethodReplacer interface contains reimplement(-----) method, here we must provide
our new implementation in reimplement() method.
Example:
Course.java
package com.durgasoft.beans;
public class Course {
private String cid;
private String cname;
private int ccost;
setXXX()
getXXX()
public void getCourseDetails(){
System.out.println("Course Details");
System.out.println("-----------------");
System.out.println("Course Id :"+cid);
System.out.println("Course Name :"+cname);
System.out.println("Course Cost :"+ccost);
}
}
NewImpl.java
package com.durgasoft.beans;
import java.lang.reflect.Method;
import org.springframework.beans.factory.support.MethodReplacer;
public class NewImpl implements MethodReplacer {
public Object reimplement(Object arg0, Method arg1, Object[] arg2) throws Throwable
{
System.out.println("Course Details");
System.out.println("-----------------");
System.out.println("Course Id :C-222");
System.out.println("Course Name :.NET");
System.out.println("Course Cost :20000");
return null;
}
}
spring_beans_config.xml
<beans>
<bean id="newImpl" class="com.durgasoft.beans.NewImpl"/>
<bean id="course" class="com.durgasoft.beans.Course">
<property name="cid" value="C-111"/>
<property name="cname" value="Java"/>
<property name="ccost" value="10000"/>
<replaced-method name="getCourseDetails" replacer="newImpl"/>
</bean>
</beans>
Test.java
package com.durgasoft.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.durgasoft.beans.Course;
public class Test {
public static void main(String[] args)throws Exception {
ApplicationContext context=new
ClassPathXmlApplicationContext("/com/durgasoft/cfgs/spring_beans_config.xml");
Course crs=(Course)context.getBean("course");
crs.getCourseDetails();
}
}
Bean Validations in spring
The process of checking data validity before using data in applications is called as Data
Validations.
In Web Applications, there are two types of data validations.
1.Client Side Data Validations
2.Server Side Data Validations
1.Client Side Data Validations: The process of checking whether data is valid or not at
client browser before submitting request to the Server is called as Client Side Data Data
Validations. To perform client side data validations we will use "Java Script" Functions.
2.Server Side Data Validations: The process of checking whether data is valid or not
at server after getting request from client and before using data in application logic is
called as Server side data validations. To perform Server side data validations we have
to use Java code explicitly.
Note: If we use web Frameworks like Struts, JSF , SPring WEB MVC module then all
these frameworks are able to provide their own validations services implicitly to
perform data validations.
In Spring Core Module, to perform validations over the data after storing data in Bean
objects Spring Framework has provided a predefined interface in the form of
"org.springframework.validation.Validator" .
To perform bean validations in Spring applications then we have to use the following
steps.
1.Prepare a properties file with all validation messages:
Declare validation messages in the form of key-value pairs, where keys must be
validation message code and value must be validation message.
EX: abc.properties
error.uname= User Name is required.
error.password=User Password is required.
2.Prepare Validator class:
The main intention of Validator class is to define all data validation logic w.r.t the bean
properties.
Steps:
a)Declare an user defined class.
b)Implement org.springframework.validation.Validator interface.
c)Implement the following Validator interface provided methods.
public boolean supports(Class type)
public void validate(Object obj, Errors errors)
Where supports(--) method will check whether the Bean object is supporting data
validations or not.
Where validate(---) is able to include data validation logic inorder to perform data
validations.
Where "Errors" is able to include all validation messages in the form of Map object
inorder to display. To add an error message to Errors object we have to use
"rejectValue(----)" method.
d)Declare Resource property and its setXXX() method inorder to repersent the name
and location of the properties file where all the validation messages are existed.
- Key ideas of Spring - Method Replacement explained simply
- Ready-to-use code examples
- Exam-style questions at the end