Instantiating multiple beans of the same class with Spring annotations

It’s not possible. You get a duplicate exception.

It’s also far from optimal with configuration data like this in your implementation classes.

If you want to use annotations, you can configure your class with Java config:

@Configuration
public class PersonConfig {

    @Bean
    public Person personOne() {
        return new Person("Joe", "Smith");
    }

    @Bean
    public Person personTwo() {
        return new Person("Mary", "Williams");
    }
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)