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 Auto-Wiring Beans

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 + JdbcTemplate + JdbcDaoSupport examples

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 + JDBC example

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 dependency checking with @Required Annotation

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 properties dependency checking

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 Spring inner bean examples

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 Constructor injection type ambiguities 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 inject value into bean properties in Spring

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 How to load multiple Spring bean configuration file

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 Spring AOP Interceptor transaction is not working