Spring @Value default value

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

Import Spring XML files into @Configuration

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

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

Read more

Spring 3 REST hello world example

In Spring 3, old RequestMapping class is enhanced to support RESTful features, which makes Spring developers easier to develop REST services in Spring MVC. In this tutorial, we show you how to use Spring 3 MVC annotations to develop a RESTful style web application. 1. Project Directory Review the project folder structure. 2. Project Dependency …

Read more

Spring 3 MVC ContentNegotiatingViewResolver 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 and JSR303 @Valid 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 JSON example

In this tutorial, we show you how to output JSON data in Spring MVC framework. Technologies used : Spring 3.2.2.RELEASE Jackson 1.9.10 JDK 1.6 Eclipse 3.6 Maven 3 P.S In Spring 3, to output JSON data, just puts Jackson library in the project classpath. 1. Project Dependencies Get Jackson and Spring dependencies. pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" …

Read more

Spring 3 MVC and XML example

In Spring 3, one of the feature of “mvc:annotation-driven“, is support for convert object to/from XML file, if JAXB is in project classpath. In this tutorial, we show you how to convert a return object into XML format and return it back to user via Spring @MVC framework. Technologies used : Spring 3.0.5.RELEASE JDK 1.6 …

Read more

Spring 3 MVC and RSS feed example

In Spring 3, comes with a abstract class “AbstractRssFeedView” to generate RSS feed view, using java.net’s ROME package. In this tutorial, we show you how to generate a RSS feed view from Spring MVC framework. Technologies used : Spring 3.0.5.RELEASE ROME 1.0.0 JDK 1.6 Eclipse 3.6 Maven 3 At the end of the tutorial, when …

Read more

Spring Object/XML mapping example

The Spring’s Object/XML Mapping, is converting Object to XML or vice verse. This process is also known as XML Marshalling – Convert Object to XML. XML UnMarshalling – Convert XML to Object. In this tutorial, we show you how to use Spring’s oxm to do the conversion, Object XML. Note No nonsense, for why and …

Read more

ClassNotFoundException: org.apache.xml.serialize.XMLSerializer

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.exolab.castor.xml.XMLException

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

Spring 3 JavaConfig @Import example

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 example

Since Spring 3, JavaConfig features are included in core Spring module, it allow developer to move bean definition and Spring configuration out of XML file into Java class. But, you are still allow to use the classic XML way to define beans and configuration, the JavaConfig is just another alternative solution. See the different between …

Read more

CGLIB is required to process @Configuration classes

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

Spring EL Lists, Maps example

In this article, we show you how to use Spring EL to get value from Map and List. Actually, the way of SpEL works with Map and List is exactly same with Java. See example : //get map whete key = ‘MapA’ @Value("#{testBean.map[‘MapA’]}") private String mapA; //get first value from list, list is 0-based. @Value("#{testBean.list[0]}") …

Read more

Spring EL Operators example

Spring EL supports most of the standard mathematical, logical or relational operators. For example, Relational operators – equal (==, eq), not equal (!=, ne), less than (<, lt), less than or equal (<= , le), greater than (>, gt), and greater than or equal (>=, ge). Logical operators – and, or, and not (!). Mathematical …

Read more

Spring EL bean reference example

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 regular expression 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 ternary operator (if-then-else) 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 method invocation 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

Test Spring el with ExpressionParser

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

Spring EL hello world example

The Spring EL is similar with OGNL and JSF EL, and evaluated or executed during the bean creation time. In addition, all Spring expressions are available via XML or annotation. In this tutorial, we show you how to use Spring Expression Language(SpEL), to inject String, integer and bean into property, both in XML and annotation. …

Read more

Spring 3 hello world example

This tutorial shows you how to create a simple hello world example in Spring 3.0. Technologies used in this article : Spring 3.0.5.RELEASE Maven 3.0.3 Eclipse 3.6 JDK 1.6.0.13 P.S Spring 3.0, at least JDK 1.5 is required to work. Spring 3.0 dependencies In Spring 2.5.x, almost the entire Spring modules are grouped into a …

Read more

JSF 2 + Spring 3 integration example

In this tutorial, we will show you how to integrate JSF 2.0 with Spring 3 using : JSF XML faces-config.xml Spring annotations JSR-330 standard injection Tools and technologies used : JSF 2.1.13 Spring 3.1.2.RELEASE Maven 3 Eclipse 4.2 Tomcat 6 or 7 1. Directory Structure A standard Maven project for demonstration. 2. Project Dependencies Declares …

Read more