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

  • SEARCH
  • Home
  • 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 8965601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:50:30+00:00 2026-06-15T16:50:30+00:00

I have a bean Employee as: public class Employee { private int empId; public

  • 0

I have a bean Employee as:

public class Employee {
    private int empId;
    public int getEmpId() {
        return empId;
    }
    public void setEmpId(int empId) {
        this.empId = empId;
    }
}

An EmployeeList class which has looks like:

public class EmployeeList {
    @Autowired
    public Employee[] empList;
}

The spring config file:

<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"
       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">

    <context:annotation-config/>

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

    <bean id="empBean" class="Employee" scope="prototype">
    </bean>
    <bean id="empBeanList" class="EmployeeList">
    </bean>
 </beans>

The main method class:

public class App 
{

    public static void main( String[] args )
    {
        ApplicationContext empContext = new ClassPathXmlApplicationContext(
                "employee-module.xml");

        EmployeeList objList = (EmployeeList) empContext.getBean("empBeanList");

        Employee obj = (Employee) empContext.getBean("empBean");
        obj.setEmpId(1);
        System.out.println(obj.getEmpId());
        System.out.println("length " + objList.empList.length);

        Employee obj1 = (Employee) empContext.getBean("empBean");
        obj1.setEmpId(2);
        System.out.println(obj1.getEmpId());
        System.out.println("length " + objList.empList.length);

        Employee obj2 = (Employee) empContext.getBean("empBean");
        System.out.println("length " + objList.empList.length);
    }
}

The count of Employee instances I get is always 1. Why it is not incremented when I get the bean instance multiple times. The Employee bean has scope as prototype.

  • 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-06-15T16:50:33+00:00Added an answer on June 15, 2026 at 4:50 pm

    Because getting a new prototype instance doesn’t magically add it to a previously instantiated bean array.

    When the context starts up, a single employee bean is instantiated and injected in the empBeanList bean, and then the empList bean is created and doesn’t change anymore.

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

Sidebar

Related Questions

I have a table bean as : public class Employee implements Serializable { @Id
i have bean that contain method [void return] and want access to this bean
I have a bean which contains an int variable. I want to do something
I have this code for login validation using a Struts2 action class which calls
I have the following 2 entities: @Entity(name = Employee) @Table(name = EMPLOYEE) public class
I have a bean mongoService which looks like as <?xml version=1.0 encoding=UTF-8?> <beans xmlns=http://www.springframework.org/schema/beans
I have a bean which I map to the database using Hibernate. I'm using
I have a bean of type string[] which has two or more values. I
I have a bean where I have a field CustAmount which is double, I
I have a bean definition in Spring and it's proxy counterpart which is meant

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.