This article shows how to provide a default value for the @Value annotation. In the below code, if the property.name doesn’t exist in the application properties or environment, defaultValue will be assigned to the propertyName field. SimpleComponent.java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class SimpleComponent { @Value("${property.name:defaultValue}") private String propertyName; // getters, setters, etc } […]

Read more Spring @Value default value

This is common to mix XML configuration into Spring @Configuration, because developers are used to the XML namespaces. In Spring, you can use @ImportResource to import Spring XML configuration files into @Configuration : AppConfig.java import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; @Configuration @ImportResource("classpath:/config/spring.xml") public class AppConfig { } Another example AppConfig.java import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; import org.springframework.context.annotation.Import; @Configuration […]

Read more Import Spring XML files into @Configuration

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 same way as Spring annotations, the […]

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

Spring 3, ContentNegotiatingViewResolver, is an interesting view resolver, which allow you to output a same resource (content or data) to different type of views like JSP, XML, RSS, JSON and etc. Put it simple, see following web requested URL, which will return in different views. https://mkyong.com/fruit/banana.rss , returned as RSS file. https://mkyong.com/fruit/banana.xml , returned as […]

Read more Spring 3 MVC ContentNegotiatingViewResolver example

In Spring 3, you can enable “mvc:annotation-driven” to support JSR303 bean validation via @Valid annotation, if any JSR 303 validator framework on the classpath. Note Hibernate Validator is the reference implementation for JSR 303 In this tutorial, we show you how to integrate Hibernate validator with Spring MVC, via @Valid annotation, to perform bean validation […]

Read more Spring 3 MVC and JSR303 @Valid example

Problem With Spring OXM + Castor binding, the Castor library is added, but still hit the following error message? Exception in thread "main" java.lang.RuntimeException: Could not instantiate serializer org.apache.xml.serialize.XMLSerializer: java.lang.ClassNotFoundException: org.apache.xml.serialize.XMLSerializer at org.exolab.castor.xml.XercesSerializer.<init>(XercesSerializer.java:50) //… Castor dependency in Maven. <dependency> <groupId>org.codehaus.castor</groupId> <artifactId>castor</artifactId> <version>1.2</version> </dependency> Solution If not mistake, Castor need Xerces to work, so, you need […]

Read more ClassNotFoundException: org.apache.xml.serialize.XMLSerializer

Problem In Spring OXM (object XML mapping), when converting an object to XML file, it hits following error message : Caused by: java.lang.ClassNotFoundException: org.exolab.castor.xml.XMLException at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:252) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320) … 29 more Is castor data binding framework included in Spring oxm? Solution The castor is […]

Read more ClassNotFoundException : org.exolab.castor.xml.XMLException

Normally, you will split a large Spring XML bean files into multiple small files, group by module or category, to make things more maintainable and modular. For example, <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <import resource="config/customer.xml"/> <import resource="config/scheduler.xml"/> </beans> In Spring3 JavaConfig, the equivalent functionality is @Import. package com.mkyong.config; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @Configuration @Import({ CustomerConfig.class, […]

Read more Spring 3 JavaConfig @Import example

Problem Using Spring3 @Configuration to create an application configuration file like below : import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean //… } However, when run it, it hits following error message : org.springframework.context.support.AbstractApplicationContext prepareRefresh //… Exception in thread "main" java.lang.IllegalStateException: CGLIB is required to process @Configuration classes. Either add CGLIB to the classpath or […]

Read more CGLIB is required to process @Configuration classes

In Spring EL, you can reference a bean, and nested properties using a ‘dot (.)‘ symbol. For example, “bean.property_name“. public class Customer { @Value("#{addressBean.country}") private String country; In above code snippet, it inject the value of “country” property from “addressBean” bean into current “customer” class, “country” property. Spring EL in Annotation See following example, show […]

Read more Spring EL bean reference example

Spring EL supports regular expression using a simple keyword “matches“, which is really awesome! For examples, @Value("#{‘100’ matches ‘\\d+’ }") private boolean isDigit; It test whether ‘100‘ is a valid digit via regular expression ‘\\d+‘. Spring EL in Annotation See following Spring EL regular expression examples, some mixed with ternary operator, which makes Spring EL […]

Read more Spring EL regular expression example

Spring EL supports ternary operator , perform “if then else” conditional checking. For example, condition ? true : false Spring EL in Annotation Spring EL ternary operator with @Value annotation. In this example, if “itemBean.qtyOnHand” is less than 100, then set “customerBean.warning” to true, else set it to false. package com.mkyong.core; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; […]

Read more Spring EL ternary operator (if-then-else) example

Spring expression language (SpEL) allow developer uses expression to execute method and inject the method returned value into property, or so called “SpEL method invocation“. Spring EL in Annotation See how to do Spring EL method invocation with @Value annotation. package com.mkyong.core; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("customerBean") public class Customer { @Value("#{‘mkyong’.toUpperCase()}") private String name; […]

Read more Spring EL method invocation example

Spring expression language (SpEL) supports many functionality, and you can test those expression features with this special “ExpressionParser” interface. Here’s two code snippets, show the basic usage of using Spring EL. SpEL to evaluate the literal string expression. ExpressionParser parser = new SpelExpressionParser(); Expression exp = parser.parseExpression("’put spel expression here’"); String msg = exp.getValue(String.class); SpEL […]

Read more Test Spring el with ExpressionParser