基本春季问题:使用@Autowired

时间:2012-03-24 09:52:01

标签: spring

我有以下两个对象:User和DomainUser

User.java:

package com.domain;

import java.io.Serializable;

@SuppressWarnings("serial")
public class User implements Serializable {

    private long id = 0;
    private String userName;
    private String password;

    public User() {

    }

    public User(long id) {
        this.id = id;
    }

    public String toString() {

        StringBuffer sb = new StringBuffer(256);

        sb.append("[id : ").append(id).append(", ");
        sb.append("userName : ").append(userName).append(", ");
        sb.append("password : ").append(password).append("]");

        return sb.toString();
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

DomainUser.java

package com.domain;

import org.springframework.beans.factory.annotation.Autowired;


public class DomainUser {


    @Autowired
    private User user;
    private String domainName;

    public String toString() {
        StringBuffer sb = new StringBuffer(255);

        sb.append(user.toString()).append(", domainName : ").append(domainName);

        return sb.toString();
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getDomainName() {
        return domainName;
    }

    public void setDomainName(String domainName) {
        this.domainName = domainName;
    }



}

我正在尝试使用@Autowired注释将User对象自动装入DomainUser。但是,当我按如下所示运行测试时,不会填充User对象。

的applicationContext.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="user" class="com.domain.User">
        <constructor-arg  value="#{1234}"/> 
        <property name="userName" value="somename" />
        <property name="password" value="sompassword" />
    </bean>

    <bean id="domainUser" class="com.domain.DomainUser">
        <property name="domainName" value="mysite" />
    </bean>

</beans>

DomainUserTest.java

package com.domain.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.domain.DomainUser;

public class DomainUserTest {

    public static void main(String[] args) {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");

        DomainUser domainUser = (DomainUser) context.getBean("domainUser");

        System.out.println(domainUser.toString());
    }

}

如果我在applicationContext.xml的autowiring属性中使用'byType'进行自动装配,则可以正常工作:

    <bean id="domainUser" class="com.domain.DomainUser" autowire="byType">
        <property name="domainName" value="mysite" />
    </bean>

有人可以帮助我理解为什么@Autowired注释不会产生相同的结果吗?

1 个答案:

答案 0 :(得分:4)

您需要 AutowiredAnnotationBeanPostProcessor 来处理@Autowired属性的注入。您可以放置​​<context:annotation-config />(您需要在xml中定义 context -namespace)或者只是将后处理器定义为xml中的bean:

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

有关详细信息,请参阅示例here