Introduction
This article is a continuation from several previous article. One of the article is an article with the title of ‘How to Create a Simple Java Spring Application using NetBeans IDE’ in this link. But in this article, the focus is just sharing about how to inject value to a property of a Spring Bean. So, there is a Java Spring Application running using the help of NetBeans IDE. In the output of the main program or main class of the Java Spring Application, there is just a simple string of sentence. Take a look at the Java Spring Application in this link which is an article previously mentioned.
How to Inject Value of a Spring Bean Property in Java Spring Application
There are several steps which is very important in order to achieve the purpose. The first part is about the property definition in the Spring Bean. Since the process is actually injecting a value to that property, the next part is about the value definition of the property itself. It is important to define it because when running the application after, the printed output will be retrieved from the property definition.
Defining the property of a Spring Bean
This is actually the first part for achieving the purpose. The following are the steps for defining the property of the Spring Bean. As an example, the Spring Bean is a Java class with the name of ‘Employee.java’. The following is the original content of the file :
public class Employee { public void identifyID(){ System.out.println("I am an employee !"); } }
In order to define and add the property of a Spring Bean, below are the steps :
-
Add a new attribute or property for the Employee class. The attribute or the property is a variable with the name of ‘id’. Furthermore, in order to make it simple, just set the variable to have a String type. Moreover, since we want to prove that the injected value is exist in the property of the ‘Employee’ Bean, the code for printing is also modified. Print the property so the injected value will appear if the value injection is a success. So, all the changes from the previous source code exist as follows :
public class Employee { String id; public void identifyID(){ System.out.println("I am an "+ this.id +" !"); } }
-
Save the file and make sure to test it by compiling it first. It is a step to make sure that there is no error appear after changing the source code as in the above modification.
-
Next, just create a setter method to set or inject the value of the property. It is very important step so that the value injection to the property is possible. Do it manually by adding the setter method or by using the help from NetBeans IDE. In order to use the help from NetBeans IDE, just right click in the source code. The following floating menu will appear :
How to Inject Value of a Spring Bean Property in a Java Spring Application using NetBeans IDE Select the ‘Insert Code…’ menu, in order to insert setter method code for ‘id’ attribute or property. The following image will appear :
How to Inject Value of a Spring Bean Property in a Java Spring Application using NetBeans IDE Just choose the Setter menu, there will be a new window dialog appear as follows :
How to Inject Value of a Spring Bean Property in a Java Spring Application using NetBeans IDE Don’t forget to check the checkbox in the above window dialog. Especially the ‘id’ checkbox which is representing the action for generating the setter method. If there are too many of the properties available, just click the ‘Employee’ checkbox which is representing all available properties in the Spring Bean itself. The following is the image after clicking the checkbox to select the property for generating the setter method :
How to Inject Value of a Spring Bean Property in a Java Spring Application using NetBeans IDE Finally, click the Generate Button exist in the window dialog above. There will be a new additional script which is actually the setter method. The following is the generated setter method :
public void setId(String id) { this.id = id; }
In summary, after generating the setter method for the ‘id’ property, the Employee Java class or the Employee Spring Bean overall will be in the following appearance :
public class Employee { String id; public void setId(String id) { this.id = id; } public void identifyID(){ System.out.println("I am an "+ this.id +" !"); } }
Defining the value for the property of the Spring Bean
After defining the property of the Spring Bean in the previous part, continue on to define the value. Actually, the value definition is intended for the property’s value. Just perform the following steps :
-
Open the Spring Configuration file. For an example in this article, it has a name of ‘spring-config.xml’. So, the name can be different from this project with another. The most important thing is that the Spring Configuration file must be an XML format file. The following is the original content of the ‘spring-config.xml’ before adding new configuration lines :
<?xml version="1.0" encoding="UTF-8"?> <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-3.0.xsd"> <bean id="employeeBean" class="com.test.spring.Employee"> </bean> </beans>
-
Add the following configuration line to define the value for the property :
<property name="id" value="Employee of the Company" />
In the end, the spring-config.xml will be in the following display :
<?xml version="1.0" encoding="UTF-8"?> <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-3.0.xsd"> <bean id="employeeBean" class="com.test.spring.Employee"> <property name="id" value="Employee of the Company" /> </bean> </beans>
Running the Java Spring Application
The last part, in order to prove that the injection value is a success, just run the Java Spring Application. The following is the actual output display of the execution :
Finally, as in the above output appear, the injected value with the string of ‘Employee of the Company’ exist as part of the printed text. It is a prove that the process for injecting the value to the property of the Spring Bean is a success.