@Required: This annotation makes a filed setter injection
mandatory.
package com.app.model;
import org.springframework.beans.factory.annotation.Required;
public class Employee {
private Integer val;
public Integer getVal() {
return val;
}
@Required
public void setVal(Integer val) {
this.val = val;
}
Beans.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
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></context:annotation-config>
<bean id="employee" class="com.app.model.Employee" >
<property name="val">
<value>250</value>
</property>
</bean>
</beans>
Client Program:
package com.app.test;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationCon
text;
import com.app.model.Employee;
public class Test {
public static void main(String[] args) {
ApplicationContext context=new
ClassPathXmlApplicationContext("beans.xml");
Employee
emp=context.getBean("employee",Employee.class);
System.out.println(emp.getVal());