0% found this document useful (0 votes)
6 views2 pages

@required: This Annotation Makes A Filed Setter Injection Mandatory

The document shows an Employee class with a private field "val" and a setter method for it annotated with @Required. This requires the setter to be injected by Spring. A beans.xml configures context:annotation-config and defines an employee bean with the val property set. A test class gets the employee bean from the context and prints the val field, demonstrating the required setter injection.

Uploaded by

Prabhakar Prabhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

@required: This Annotation Makes A Filed Setter Injection Mandatory

The document shows an Employee class with a private field "val" and a setter method for it annotated with @Required. This requires the setter to be injected by Spring. A beans.xml configures context:annotation-config and defines an employee bean with the val property set. A test class gets the employee bean from the context and prints the val field, demonstrating the required setter injection.

Uploaded by

Prabhakar Prabhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

@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());

You might also like