Featured
-
How to split a String
Below I have written a code to split a string.
by shahbaz.ahmmod -
Send Push Notification with Custom Data to iPhone device from Java Server Side
This blog will help you to send Push Notifications
by babita.dhami -
Tomcat and Eclipse Integration Error : “Unknown version of Tomcat was specified”
If you are adding Server in Eclipse for Tomcat, an
by chandan.ahluwalia -
Spring MVC and MongoDB
Repository Class Using MongoDB : Spring has th
by sumit.vyas -
How to create DLL file from java?
Hello Guy's This Blog will guide how to create D
by bhagwan.khichar
Tags
Difference between Singleton and Prototype bean
Singleton Bean Scope
The singleton bean is termed as a stateless bean i.e. it doesn't carry any state with it. Only one instance of the object is created which is defined by this type of bean. One instance is created per container.All the o...
How to load properties file and fetch values by using annotation in Spring
Properties file:
It is basically used to store data that is constant and can be used anywhere in the program.
Example: You can store the database credentials and connection parameters in the file and fetch it when required.
How to use:
...
Access Denied in spring security 4 even after successful authentication
In spring security after successfull authentication I was getting the Access denied error to go to pages which had the authentication.
What I did was.
@Override
protected void configure(HttpSecurity http) throws Exception {
ht...
How to add multiple properties file in spring configuration
Here we have configured the spring mvc configuration. In @ComponentScan we can give the path where we have our components(like controller, model, dao etc).
For adding the single properties file we use:
@PropertySource("classpath:application.p...
How to disable csrf spring security 4
In spring security bydefault the csrf protect in on. As a result it asks for token during login and other requests. Although its not a good practise to disable the protection but we can do it. As we can see in the code below,
http.csrf().disable...
How to authenticate MD5 password encyption in spring security 4
Whenever we deal with password in an application, we go for some encryption. In spring the password it authenticated within the configuration.
Here is the sample code where we have already saved the password in encrypted form and now during lo...
Spring JDBC Template for calling Stored Procedures
In Spring we can call the stored Procedures using the SimpleJdbcCall class. we have use it with IN and OUT parameters. Firstly we need to create the procedure in database.
DELIMITER //
DROP PROCEDURE IF EXISTS `TEST`.`getRecord` $$
CREATE ...
How to call a method after bean initialization is complete
Hello Friends, If you Want to call the method after your bean is initialize in spring you can use the following options.
Use the afterProprtiesSet method.
class MyClass implements InitializingBean {
public void afterPropertiesSet(...
Initializing Log4J with Spring
In below Code you have to define the following log4j properties into your log.properties file:-
log4j.properties
# Global logging configuration
log4j.rootLogger = DEBUG, fileout
# Define the file appender
log4j.appender...
Read System environment variable in Spring
Hello Friends, The below code will show you how you can read System Environments Variables and System Properties by Using Spring applicationContext.
We have to create SystemVariables Class and create the property systemVariables
SystemVaria...
Spring MVC @RequestParam vs @PathVariable
@PathVariable:
Sometimes we need to pass parameters along with the url to get the data. In Spring MVC framework, we can customize the URL in order to get data. For this purpose @PathVariable annotation is used in Spring framework. @PathVariabl...
Spring Security:RESTful Authentication via Spring
AuthenticationTokenProcessingFilter
First we need to make security context where we define our Authentication Entry point and a filter for processing the token. Configuration xml will look something like this:
<security:http realm="Authe...
Reading and injecting properties into spring from property file
We can read and inject the property from property file in spring. The below code will show you how you can read the property from property file and inject that properties to the List Object by using spring-config.xml file.
Customer.java
pac...
Injecting Property Object in Spring
Here the below code will show you how to inject the Properties in Spring using the spring.xml file. By using that we will inject the Properties Class object in spring.
Customer.java
package com.evon;
import java.util.Properties;
public...
Injecting Set in Spring
Here the below code will show you how to inject the Set in Spring using the spring.xml file. By using that we will inject the Set object in spring.
Person.java
package com.evon;
public class Person {
String name;
String addr...
Injecting Map in Spring
Here the below code will show you how to inject the Map in Spring using the spring.xml file. By using that we will inject the Map object in spring. -
Person.java
package com.evon;
public class Person {
String name;
String add...
Injecting List Bean in Spring
Here the below code will show you how to inject the List in Spring using the spring.xml file. By using that we will inject the list object in spring.
Person.java
package com.evon;
public class Person {
String name;
String ad...
Sample application using JQuery, Spring MVC @RequestBody and JSON together
See the below steps to create sample application using JQuery, Spring MVC @RequestBody and JSON :
1- Define dependency for Spring, JSON in pom.xml as below:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/...
Spring MVC: Difference between context:annotation-config and context:component-scan
Difference between <context:annotation-config/> vs <context:component-scan/>:
<context:annotation-config/> : <context:annotation-config/> is used to activate all the annotations that are present in java beans and thos...
Spring MVC @PathVariable Uses
Sometimes we need to pass parameters along with the url to get the data. In Spring MVC framework, we can customize the URL in order to get data. For this purpose @PathVariable annotation is used in Spring framework.
For example you want to wr...
Spring @Transactional Annotation
@Transactional Annotation in Spring:- A transaction is unit of work that have ACID (atomicity, consistency, isolation and durability) properties.
Atomicity:- this means that the changes will completely happens or not. for example If money is ...
Spring MVC 3 and handling static content - am I missing something?
We can handle static content for example: js,css,images in Spring MVC by using mvc:resources element. mvc:resources element is used to point to the location of static content with a specific public URL pattern.
For example define the bel...
Spring Security: Obtaining current username (i.e. SecurityContext) information in a bean?
Instead of calling static method inside the controller like below:
@RequestMapping(value="/welcome", method = RequestMethod.GET)
public String getUserDetails(ModelMap model) {
Authentication authentication = SecurityContextHolder.getCont...
Sprint Security: Getting Active user's UserDetails
We can easily get active user's UserDetails in Spring Security. Spring Security provides "org.springframework.security.core.Authentication" which provides the required API for retrieving the logged in user details. The methods of this class are g...
Spring MVC @PathVariable getting truncated
We generally face a problem when we hit server with path variable that contains special character like dot(.) it gets truncated.
For Example: If you have the mapping
@RequestMapping(value = "/findMe/{emailId}", method = RequestMethod.GET)
...
What's the difference between @Component, @Repository, @Controller & @Service annotations in Spring?
When we define the annotations @Component, @Service, @Controller, @Repository with the classes, Spring automatically scan and identifies those classes and register the Bean definition with the ApplicationContext.
Difference between @Component...
What is @ModelAttribute in Spring MVC?
@ModelAttribute implies to a property of the Model object in Spring MVC. This annotation workes only if your calss is Controller class i.e. declares with @Controller annotation.
It is also used to define the command object that we declared by ...
Spring 3 MVC accessing HttpRequest from controller
Spring 3 MVC accessing HttpRequest from controller
To access HttpRequest from controller you just need to define HttpServletRequest and HttpServletResponse as parameters in a function signature of your Controller. by doing this you allowing Sp...
Spring Security - UserDetailsService vs. AuthenticationProvider
We can use Custome UserDetailsService and custom AuthenticationProvider for user authentication in Spring Security. Here I'm taking example of custom UserDetailsService.
UserDetailsService is a Core interface which loads user-specific data. I...
Spring security 3: database authentication with hibernate
For authenticating the user via spring security we need to make our own custom authentication-provider. We can make our custom UserDetailService easily, here is the sample custom code
MyUserDetailsService.java
package com.users.serv...
ResourceLoaderAware in Spring framework
ResourceLoaderAware : When we load resources using bean, ResourceLoaderAware will help us to load resources using getter/setter methods.
For more details see my last blog Click
Below example will help you to understand better:
Create Cust...
Using context:component-scan to pick up @Configuration Classes
In Spring 3.0 using the tag context:component-scan in xml file, the entry of the class(or bean) defined under the scan tag is automatically created. The example to show use of context:component-scan tag is as below:
Example:-
JavaConfig.jav...
Instantiating the Spring container using AnnotationConfigApplicationContext
In Spring 3 we can also create a bean using java file instead of XML.It is alternate way to create the bean, you can also use xml based configuration.
HelloWorld.java
package com.babita;
public class HelloWorld {
public void p...
Creating Custom Scope in Spring
Using Custom Scopes, we can define new scope as well as modify the existing scope.
Custom Scope is useful for following scenarios-
creating a bean scope where beans can be shared between servlet contexts
creating a bean scope where bean...
Auto detect Components in Spring.
When we define the annotations @Component, @Service, @Repository, @Controller with classes, Spring automatically scan and identifies those classes and register the Bean definition with the ApplicationContext.
The @Component annotations are:
...
@Resource annotaion in Spring
@Resource annotation allows you to specify a name of the injected bean. @Resource annotation takes a 'name' attribute which will be treated as the bean name we want to inject in the class. In other words we can say, it follows by-name autowiring ...
How to use Autowire With Qualifiers?
In Spring we use @Qualifier to indicate which bean we want to qualify to autowired on a field.
when we create multiple beans of the same type( for example: Department) and want to autowire only one of them with a property in some other class (...
How to use Filters to Customize Scanning?
In Spring context XML file, Filter has a sub-element 'context:include-filter' having two attributes as 'type' and 'expression' attributes that indicate to Spring container that 'type' is of regular expression whose value is been provided by expre...
How to exclude a bean from auto-wiring?
When configuring beans in configuration file, set the "autowire-candidate" attribute of the bean element in .xml file to false, by doing this the container will exclude that specific bean from auto-wiring.
By using the below example we can eas...
Quartz +Spring Integration
It seems today every application needs background jobs, here we are going to learn how to integrate Quartz Scheduler with Spring.
Version we are using
Spring 3.x
Quartz 1.8.6
Maven 3
we have to follow few steps for configure qua...
Spring 3 MVC configuration.
Introduction to Spring 3 MVC Framework
The Spring web MVC framework provides model-view-controller architecture and ready components that can be used to develop flexible and loosely coupled web applications. The MVC pattern allow us in separa...
Spring @RequestHeader Annotation
Spring @RequestHeader Annotation
Spring MVC provides annotation @RequestHeader that can be used to map controller parameter to request header value. Here is the simple usage of spring @RequestHeader annotation.
import org.springframework.s...
Spring MVC Exception handling using @ControllerAdvice Annotation
Spring MVC provides a great way to handle exceptions and errors.
@ExceptionHandler annotation is core to this feature. For each Spring controller we can simply define a method that automatically gets called if a given exception occurs.
For e...
Hibernate environment in Spring
Spring comes with a family of data access frameworks that integrate with a variety of data access technologies. Whether youre persisting your data via direct JDBC, iBATIS, or an object relational mapping (ORM) framework such as Hibernate, Spring...