Spring Auto-Wiring Beans with @Autowired annotation

In last Spring auto-wiring in XML example, it will autowired the matched property of any bean in current Spring container. In most cases, you may need autowired property in a particular bean only. In Spring, you can use @Autowired annotation to auto wire bean on the setter method, constructor or a field. Moreover, it can …

Read more

Spring Auto-Wiring Beans

In Spring framework, you can wire beans automatically with auto-wiring feature. To enable it, just define the “autowire” attribute in <bean>. <bean id="customer" class="com.mkyong.common.Customer" autowire="byName" /> In Spring, 5 Auto-wiring modes are supported. no – Default, no auto wiring, set it manually via “ref” attribute byName – Auto wiring by property name. If the name …

Read more

Spring + JdbcTemplate + JdbcDaoSupport examples

In Spring JDBC development, you can use JdbcTemplate and JdbcDaoSupport classes to simplify the overall database operation processes. In this tutorial, we will reuse the last Spring + JDBC example, to see the different between a before (No JdbcTemplate support) and after (With JdbcTemplate support) example. 1. Example Without JdbcTemplate Witout JdbcTemplate, you have to …

Read more

Spring + JDBC example

In this tutorial, we will extend last Maven + Spring hello world example by adding JDBC support, to use Spring + JDBC to insert a record into a customer table. 1. Customer table In this example, we are using MySQL database. CREATE TABLE `customer` ( `CUST_ID` int(10) unsigned NOT NULL AUTO_INCREMENT, `NAME` varchar(100) NOT NULL, …

Read more

Spring dependency checking with @Required Annotation

Spring’s dependency checking in bean configuration file is used to make sure all properties of a certain types (primitive, collection or object) have been set. In most scenarios, you just need to make sure a particular property has been set, but not all properties.. For this case, you need @Required annotation, see following example : …

Read more

Spring properties dependency checking

In Spring,you can use dependency checking feature to make sure the required properties have been set or injected. Dependency checking modes 4 dependency checking modes are supported: none – No dependency checking. simple – If any properties of primitive type (int, long,double…) and collection types (map, list..) have not been set, UnsatisfiedDependencyException will be thrown. …

Read more

Spring inner bean examples

In Spring framework, whenever a bean is used for only one particular property, it’s advise to declare it as an inner bean. And the inner bean is supported both in setter injection ‘property‘ and constructor injection ‘constructor-arg‘. See a detail example to demonstrate the use of Spring inner bean. package com.mkyong.common; public class Customer { …

Read more

Constructor injection type ambiguities in Spring

In Spring framework, when your class contains multiple constructors with same number of arguments, it will always cause the constructor injection argument type ambiguities issue. Problem Let’s see this customer bean example. It contains two constructor methods, both accept 3 arguments with different data type. package com.mkyong.common; public class Customer { private String name; private …

Read more

Spring Dependency Injection (DI)

In Spring frameowork, Dependency Injection (DI) design pattern is used to define the object dependencies between each other. It exits in two major types : Setter Injection Constructor Injection 1. Setter Injection This is the most popular and simple DI method, it will injects the dependency via a setter method. Example A helper class with …

Read more

Spring bean reference example

In Spring, beans can “access” to each other by specify the bean references in the same or different bean configuration file. 1. Bean in different XML files If you are referring to a bean in different XML file, you can reference it with a ‘ref‘ tag, ‘bean‘ attribute. <ref bean="someBean"/> In this example, the bean …

Read more

How to inject value into bean properties in Spring

In Spring, there are three ways to inject value into bean properties. Normal way Shortcut “p” schema See a simple Java class, which contains two properties – name and type. Later you will use Spring to inject value into the bean properties. package com.mkyong.common; public class FileNameGenerator { private String name; private String type; public …

Read more

How to install Spring IDE in Eclipse

Spring IDE is a very useful graphical user interface tool adding support for Spring Framework. In this tutorial, we show you two ways to install Spring IDE in Eclipse. Version used in this tutorial : Spring IDE 2.9 Eclipse 3.7 Spring IDE or SpringSource Tool Suite (STS)? Refer to this Spring IDE vs STS pdf …

Read more

Spring loosely coupled example

The concept of object-oriented is a good design to break your system into a group of reusable objects. However, when system grows larger, especially in Java project, the huge object dependencies will always tightly coupled causing objects very hard to manage or modify. In this scenario, you can use Spring framework to act as a …

Read more

How to load multiple Spring bean configuration file

Problem In a large project structure, the Spring’s bean configuration files are located in different folders for easy maintainability and modular. For example, Spring-Common.xml in common folder, Spring-Connection.xml in connection folder, Spring-ModuleA.xml in ModuleA folder…and etc. You may load multiple Spring bean configuration files in the code : ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-Common.xml", …

Read more

Maven + Spring hello world example

This quick guide example uses Maven to generate a simple Java project structure, and demonstrates how to retrieve Spring bean and prints a “hello world” string. Technologies used in this article : Spring 2.5.6 Maven 3.0.3 Eclipse 3.6 JDK 1.6.0.13 Spring 3 example For Spring 3, refer to this Maven + Spring 3 hello world …

Read more

Spring AOP Interceptor transaction is not working

Problem The Spring AOP transaction is not working in following interceptors? <bean id="testAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="interceptorNames"> <list> <idref bean="urlInterceptorInsert" /> <idref bean="urlInterceptorCommit" /> <idref bean="urlInterceptorRelease" /> <idref bean="matchGenericTxInterceptor" /> </list> </property> <property name="beanNames"> <list> <idref local="urlBo" /> </list> </property> </bean> The “matchGenericTxInterceptor” transaction interceptor, suppose to intercept urlInterceptorInsert, urlInterceptorCommit,urlInterceptorRelease, but it’s not work as expected? …

Read more