Autoboxing in spring bean

I am using spring 4.0.5 and Java 1.7.0-51. I am creating a spring bean of type Integer and setting the value through its constructor as shown in Applicationcontext.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:context="http://www.springframework.org/schema/context"
    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-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    ">

    <context:component-scan base-package="com.spring.beans"></context:component-scan>
    <bean id="user.min" class="java.lang.Integer"> <constructor-arg value="30" /></bean>
    <bean id="machine.min" class="java.lang.Integer"> <constructor-arg value="30" /></bean>
</beans>

      

I am injecting these beans into my class where I have already set some defaults.

    @Component
    public class Token {

        @Autowired(required = false)
        @Qualifier("user.min")
        private Integer userMin = 480;
        @Autowired(required = false)
        @Qualifier("machine.min")
        private int machineMin = 480;

    public Integer getUserMin() {
        return userMin;
    }
    public void setUserMin(Integer userMin) {
        this.userMin = userMin;
    }
    public int getMachineMin() {
        return machineMin;
    }
    public void setMachineMin(int machineMin) {
        this.machineMin = machineMin;
    }

      

When I print these values, I get the following values.

public static void main(String[] args){
    ApplicationContext context  = new ClassPathXmlApplicationContext("Applicationcontext.xml");
    Token t = context.getBean(Token.class);

    System.out.println("User:"+t.getUserMin());
    System.out.println("Machine:"+t.getMachineMin());
}

Output:
User:30
Machine:480

      

enter image description here

The value for userMin (class Integer) is entered from the bean, but the value for machineMin (primitive int) is not entered.

The primitive type is 'int'

not autoboxed before Integer

. Is this a bug in spring or is the way I did the setup wrong? I am working on Windows 7 (Eclipse Juno). Someone please help.

+3


source to share


1 answer


The problem is that Spring, with annotation configuration, annotates by type (first and then by name). It looks for a bean of any type of field or method (or constructor) parameter. In your case, this int

is ApplicationContext

not a bean type either int

. Since your injection target is not required, Spring does not throw exceptions.



I would not say that this is a bug, there are workarounds, just not with @Autowired

.

+1


source







All Articles