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 will… Continue Reading
spring thread thread poolHere’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 implements… Continue Reading
jsf spring validator wireSince 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 same… Continue Reading
dependency injection spring spring3In 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> Correct… Continue Reading
springDeveloping 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 changed… Continue Reading
quartz springDeveloping 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
springThis 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
integration jersey springProblem 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 processing… Continue Reading
springNote 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 works… Continue Reading
integration jax-rs resteasy springIn 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 to… Continue Reading
aop aspectj springIn 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 – Run… Continue Reading
annotation aop aspectj springIn 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
spring wiringIn 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
spring wiringIn 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, developer… Continue Reading
spring wiringUses Spring to dependency inject a bean via constructor. 1. IOutputGenerator An interface and implementation class of it. package com.mkyong.output; public interface IOutputGenerator { public void generateOutput(); } package com.mkyong.output.impl; import com.mkyong.output.IOutputGenerator; public class JsonOutputGenerator implements IOutputGenerator { public void generateOutput(){ System.out.println("This is Json Output Generator"); }… Continue Reading
dependency injection spring