Tag : spring

TestNG + Spring

TestNG + Spring Integration Example

In this tutorial, we will show you how to test Spring’s components with TestNG. Tools used : TestNG 6.8.7 Spring 3.2.2.RELEASE Maven 3 Eclipse IDE 1. Project Dependencies To integrate Spring with TestNG, you need spring-test.jar, add the following : pom.xml <properties> <spring.version>3.2.2.RELEASE</spring.version> <testng.version>6.8.7</testng.version> </properties>   <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId>Continue Reading

Java thread images

Spring and Java Thread example

Here are 3 examples to show you how to do “threading” in Spring. See the code for self-explanatory. 1. Spring + Java Threads example Create a simple Java thread by extending Thread, and managed by Spring’s container via @Component. The bean scope must be “prototype“, so that each request willContinue Reading

jsf2-logo

Spring @Autowired into JSF custom validator

Here’s the scenario, create a custom JSF validator, injects a bean via Spring’s @Autowired. UsernameValidator.java – Custom JSF validator package com.mkyong.user;   import javax.faces.application.FacesMessage; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.validator.FacesValidator; import javax.faces.validator.Validator; import javax.faces.validator.ValidatorException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import com.mkyong.user.bo.UserService;   @Component @Scope("request") @FacesValidator("UsernameValidator") public class UsernameValidator implementsContinue Reading

TestNG + Spring

Spring 3 and JSR-330 @Inject and @Named example

Since Spring 3.0, Spring supports for the standard JSR 330: Dependency Injection for Java. In Spring 3 application, you can uses standard @Inject instead of Spring’s @Autowired to inject a bean. @Named instead of Spring’s @Component to declare a bean. Those JSR-330 standard annotations are scanned and retrieved the sameContinue Reading

TestNG + Spring

How to inject null value in Spring

In Spring, you can uses this special <null /> tag to pass a “null” value into constructor argument or property. 1. Constructor Argument The wrong way to inject a null into constructor argument, a really common mistake, and nice try :) <bean id="defaultMongoTypeMapper1" class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper"> <constructor-arg name="typeKey" value="null" /> </bean> CorrectContinue Reading

TestNG + Spring

IncompatibleClassChangeError : JobDetailBean has interface org.quartz.JobDetail as super class

Developing Quartz 2.1.5 + Spring 3.1.2.RELEASE, hits following error messages : Caused by: java.lang.IncompatibleClassChangeError: class org.springframework.scheduling.quartz.JobDetailBean has interface org.quartz.JobDetail as super class at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) at java.lang.ClassLoader.defineClass(ClassLoader.java:615) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2901) at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1170) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556) at org.springframework.util.ClassUtils.forName(ClassUtils.java:258) … 19 more Solution Quartz 2 APIs are changedContinue Reading

TestNG + Spring

java.lang.ClassNotFoundException: org.springframework.transaction.TransactionException

Developing Quartz with Spring 3, and hits following error message. Caused by: java.lang.NoClassDefFoundError: org/springframework/transaction/TransactionException at java.lang.Class.getDeclaredConstructors0(Native Method) at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389) at java.lang.Class.getConstructor0(Class.java:2699) at java.lang.Class.getDeclaredConstructor(Class.java:1985) ….. Caused by: java.lang.ClassNotFoundException: org.springframework.transaction.TransactionException at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1711) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556) … 29 more Solution Doesn’t matter with Quartz, above error message show that you need Spring transaction dependency.Continue Reading

default thumbnail

Jersey + Spring integration example

This tutorial show you how to integrate Jersey web application with Spring framework. Technologies used : Jersey 1.8 Spring 3.0.5.RELEASE Eclipse 3.6 Maven 3 1. Project Dependency Declares Jersey 1.8, Spring3 and “jersey-spring.jar” dependencies in Maven pom.xml file. Note In “jersey-spring.jar” version, it will download all the Spring 2.5.6 dependencies.Continue Reading

default thumbnail

Spring request scope error : No thread-bound request found

Problem Developing web application with Spring, make a bean with scope of “request“. @Component @Scope("request") public class PaymentService {   @Autowired UserBo userBo; //… But hit following error message? Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processingContinue Reading

default thumbnail

RESTEasy + Spring integration example

Note Not much comments on this official RESTEasy guide to integrate Spring, the documentation need to be improve, because it’s simply too “abstract” for others to understand (at least to me :)). Here we show you two general ways to inject Spring bean into JBoss RESTEasy, below solutions should worksContinue Reading

default thumbnail

Spring AOP + AspectJ in XML configuration example

In this tutorial, we show you how to convert last Spring AOP + AspectJ annotation into XML based configuration. For those don’t like annotation or using JDK 1.4, you can use AspectJ in XML based instead. Review last customerBo interface again, with few methods, later you will learn how toContinue Reading

default thumbnail

Spring AOP + AspectJ annotation example

In this tutorial, we show you how to integrate AspectJ annotation with Spring AOP framework. In simple, Spring AOP + AspectJ allow you to intercept method easily. Common AspectJ annotations : @Before – Run before the method execution @After – Run after the method returned a result @AfterReturning – RunContinue Reading

default thumbnail

Spring Autowiring @Qualifier example

In Spring, @Qualifier means, which bean is qualify to autowired on a field. See following scenario : Autowiring Example See below example, it will autowired a “person” bean into customer’s person property. package com.mkyong.common;   import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier;   public class Customer {   @Autowired private Person person; //…Continue Reading

default thumbnail

Spring Autowiring by AutoDetect

In Spring, “Autowiring by AutoDetect“, means chooses “autowire by constructor” if default constructor (argument with any data type), otherwise uses “autowire by type“. See an example of Spring “auto wiring by autodetect”. Auto wiring the “kungfu” bean into “panda”, via constructor or type (base on the implementation of panda bean).Continue Reading

default thumbnail

Spring Autowiring by Constructor

In Spring, “Autowiring by Constructor” is actually autowiring by Type in constructor argument. It means, if data type of a bean is same as the data type of other bean constructor argument, auto wire it. See a full example of Spring auto wiring by constructor. 1. Beans Two beans, developerContinue Reading

Page 1 of 612345...Last »