Dependency Check (@Required) is not working and giving exception even when i am giving the value to property, Spring Boot

Viewed 24

DependencyCheck.Java

package com.yuv.spring.dependencyCheck;
import java.util.List;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class DependencyCheck {

    @Value("123")
    private int id;
    private String name;
    @Value("#{itemList}")
    private List<String> itemList;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    @Required
    public void setId(int id) {
        this.id = id;
    }

    public List<String> getItemList() {
        return itemList;
    }

    public void setItemList(List<String> itemList) {
        this.itemList = itemList;
    }

    @Override
    public String toString() {
        return "DependencyCheck [id=" + id + ", name=" + name + ", itemList=" + itemList + "]";
    }

}

Test.Java

package com.yuv.spring.dependencyCheck;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "com/yuv/spring/dependencyCheck/config.xml");
    DependencyCheck obj = (DependencyCheck) context.getBean("idRequired");
    System.out.println(obj);

}
}

Config.XML

<?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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<util:list id="itemList" list-class="java.util.ArrayList">
    <value>1</value>
    <value>2</value>
</util:list>

<context:annotation-config />
<context:component-scan
    base-package="com.yuv.spring" />


<bean name="idRequired"
    class="com.yuv.spring.dependencyCheck.DependencyCheck" p:name="yuvraj">
    <property name="id" value="123" />
</bean>

<bean
    class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" />

In This code When I am removing @Required from setID() Method in DependencyCheck.Java I am getting the right result value "123" is injected in id successfully but when i add @Required it gives Exception

Property 'id' is required for bean 'dependencyCheck'

when the value is being injected then why is it throwing error, I am Learning Spring Boot.

Without @Required

DependencyCheck [id=123, name=yuvraj, itemList=[1, 2]]

above i am getting correct answer

0 Answers
Related