Announcement Announcement Module
Collapse
No announcement yet.
Problem with @Required and factory-method Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with @Required and factory-method

    I have a problem that is better described with some meta-code.

    Consider the next trivial POJO

    Code:
    public class Product{
    
    private double price; @Required public void setPrice(double price){ this.price = price; }
    }
    Then, consider the next trivial (not useful) class with the factory method:

    Code:
    public class ProductCreator{
    
    public static Product createProduct(double price){
    
    Product product = new Product(); product.setPrice(20); return product;
    } }
    Then, imagine the next Spring configuration file:

    Code:
    <bean id="productCreator" class="theclasspackage.ProductCreator" factory-method="createProduct">
        	<constructor-arg value="30" />
        </bean>
    When I try that configuration, the Spring Container throws a BeanCreationException, because property "price" is required for bean "productCreator".

    Why does it happen?

    I am setting the value of the required price attribute in the code of the factory method.

    Thank you in advance for your comments.

    Regards.
Working...
X