August 14, 2022

Spring PropertyPlaceholderConfigurer example

Often times, most Spring developers just put the entire deployment details (database details, log file path) in XML bean configuration file. But, in a corporate environment, deployment detail is usually only can ‘touch’ by your system or database administrator, they just refuse to access your bean configuration file directly, and they will request a separate file for deployment configuration, for example, a simple properties, with deployment detail only. To fix it, you can use PropertyPlaceholderConfigurer class to externalize the deployment details into a properties file, and access from bean configuration file via a special format – ${variable}.

Spring PropertyPlaceholderConfigurer example Read More

Spring inject Date into bean property – CustomDateEditor

Spring example to show you how to inject a “Date” into bean property. In Spring, you can inject a Date via two methods. Factory bean: Declare a dateFormat bean, in “customer” bean, reference “dateFormat” bean as a factory bean. CustomDateEditor: The factory method will call SimpleDateFormat.parse() to convert String into Date object automatically.

Spring inject Date into bean property – CustomDateEditor Read More

Spring bean scopes example

In Spring, bean scope is used to decide which type of bean instance should be return from Spring container back to the caller. 5 types of bean scopes supported : (1) singleton – Return a single bean instance per Spring IoC container, (2) prototype – Return a new bean instance each time when requested, (3) request – Return a single bean instance per HTTP request. (4) session – Return a single bean instance per HTTP session. (5) globalSession – Return a single bean instance per global HTTP session. In most cases, you may only deal with the Spring’s core scope – singleton and prototype, and the default scope is singleton. P.S the scopes 3, 4, 5 means only valid in the context of a web-aware Spring ApplicationContext

Spring bean scopes example 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. Normal way: Inject value within a ‘value’ tag and enclosed with ‘property’ tag. Shortcut: Inject value with “value” attribute. “p” schema: Inject value by using “p” schema as an attributes. Which methods to use is totally base on personal preference, it will not affect the value inject into the bean properties.

How to inject value into bean properties in Spring 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. If you are referring to a bean in different XML file, you can reference it with a ‘ref‘ tag, ‘bean‘ attribute. If you are referring to a bean in same XML file, you can reference it with ‘ref‘ tag, ‘local‘ attribute. Actually, the ‘ref’ tag can access to a bean either in same or different XML files, however, for the project readability, you should use the ‘local’ attribute if you reference to a bean which declared in the same XML file.

Spring bean reference example Read More

How to load multiple Spring bean configuration file

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. The above ways are lack of organizing and error prone, the better way should be organized all your Spring bean configuration files into a single XML file. For example, create a Spring-All-Module.xml file, and import the entire Spring bean files.

How to load multiple Spring bean configuration file Read More