Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7688793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:00:23+00:00 2026-05-31T20:00:23+00:00

I have the following 2 objects: User and DomainUser User.java: package com.domain; import java.io.Serializable;

  • 0

I have the following 2 objects: User and 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;
    }



}

I am trying to autowire the User object in to DomainUser by using @Autowired annotation. But when i run the test as below, the User object is not populated.

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

}

If I autowire using the ‘byType’ in the autowiring attribute in the applicationContext.xml it works fine :

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

Can some one help me understand why doesnt @Autowired annotation produce the same result?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:00:24+00:00Added an answer on May 31, 2026 at 8:00 pm

    You need an AutowiredAnnotationBeanPostProcessor to handle the injection of @Autowired properties. You could place <context:annotation-config /> (you need to define the context-namespace in your xml) or just define the post processor as a bean in your xml:

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

    See for example here for details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following @OneToOne relation: @Entity @Table(name=USER) public class User implements Serializable{ private
I have the following objects: public class User { int Id { get; set;
I have the following user resource: class UserResource(ModelResource): class Meta: queryset = User.objects.all() resource_name
Let's say I have following POJO's using Hibernate. public class User { private String
I have following two entities public class User { public int UserId { get;
Given I have two File objects I can think of the following implementation: public
I have following model: class UserProfile(models.Model): User profile model, cintains a Foreign Key, which
I have the following model: class Task(Model): solvers = ManyToManyField(User, related_name='slvrs') Now I want
Suppose I have the following two objects: first_name_relation = User.where(:first_name => 'Tobias') # ActiveRecord::Relation
I have the following models: class UserProfile(models.Model): user = models.OneToOneField(User) score = models.PositiveIntegerField() class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.