May 14, 2021 Spring
@Required comment is applied to the setter method of the bean property, which indicates that the affected bean property must be placed in the XML profile when configured, or the container throws a BeanInitializationException exception. Shown below is an example of @Required using the message.
Let's put the Eclipse IDE to work, follow these steps to create a Spring application:
Steps | Describe |
---|---|
1 | Create a project called SpringExample, and create a package called com.tutorialspoint under the src folder of the project you created. |
2 | Use the Add External JARs option to add the required Spring library files, as explained in the Spring Hello World Example section. |
3 | Create Java Class Student and MainApp under the com.tutorialspoint package. |
4 | Create the Beans profile Beans file under the src .xml. |
5 | The final step is to create the contents of all Java files and bean profiles and run the application as explained below. |
Here's .java student file:
package com.tutorialspoint;
import org.springframework.beans.factory.annotation.Required;
public class Student {
private Integer age;
private String name;
@Required
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
return age;
}
@Required
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Here's what .java mainApp files are:
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
Student student = (Student) context.getBean("student");
System.out.println("Name : " + student.getName() );
System.out.println("Age : " + student.getAge() );
}
}
Here's what the .xml Beans: The contents of the file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<!-- Definition for student bean -->
<bean id="student" class="com.tutorialspoint.Student">
<property name="name" value="Zara" />
<!-- try without passing age and check the result -->
<!-- property name="age" value="11"-->
</bean>
</beans>
Once you've finished creating the source file and bean profile, let's run the application. If your application is all right, this causes a BeanInitializationException exception and outputs error messages and other log messages:
Property 'age' is required for bean 'student'
Next, after you remove the comment from the "age" property as follows, you can try to run the example above:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<!-- Definition for student bean -->
<bean id="student" class="com.tutorialspoint.Student">
<property name="name" value="Zara" />
<property name="age" value="11"/>
</bean>
</beans>
The example above now produces the following results:
Name : Zara
Age : 11