1

I have a Class that accepts the following constructor

 public Student(int id, String name, Map<String, List<String>> mapInject) { super(); this.id = id; this.name = name; this.mapInject = mapInject; } 

And from spring Java Config, I am injecting the constructor args like below..

@Configuration public class JavaConfig { @Bean public Employee getEmployeeBean() { Map<String,List<String>> mapInject = new HashMap<String,List<String>>(); //Add map element return new Employee(3123,"John",mapInject); } } 

Am i doing constructor injection here? Is this the right way to do so?

1 Answer 1

2

I wouldn't use Spring to handle this bean creation, unless you want ALL employees to have the same id and name which I doubt.

The power behind Spring is its Dependency Injection (DI) where you define beans for providers such as database managers, services, etc. and inject those into your components. Defining a @Bean like you have there serves no purpose as now you can only inject employees with an id of 3123 and name John.

It's important to understand that just because you are using Spring it doesn't mean EVERYTHING needs to be handled as a bean - you will always need standard POJOs for housing and passing around state (such as your Employee class) which doesn't need to have anything to do with Spring.

Down the line you might have an EmployeeService for example which houses business logic to fetch employees from a database or something, this could then be configured as a bean so it can be injected across the application.

EDIT

@Configuration public class JavaConfig { @Bean @Autowired //assuming a sessionfactory been is configured elsewhere public EmployeeService employeeService(final SessionFactory sessionfactory) { return new EmployeeService(sessionFactory); } } 

You could then inject this anywhere (maybe in a controller for example):

@RestController public class EmployeeController { private final EmployeeService employeeService; @Autowired public EmployeeController(final EmployeeService employeeService) { this.employeeService = employeeService; } } 

Where the EmployeeController doesn't need to know or care that the userService has a DB connection and doesn't need to worry about configuring it as Spring will handle all of that.

Sign up to request clarification or add additional context in comments.

8 Comments

Excellent explanation. One clarification, can you elaborate on "this could then be configured as a bean so it can be injected across the application." Also, is my above code right way to do Constructor injection ?
That explains well! Can you think of any scenario where we may need to inject a List Or a Map? I couldn't think of such scenario where it might be useful. I believe, more often we Inject objects and not a collection (or) primitive types. Is it?
Correct, usually you wouldn't want to inject state (like a list or map) as that doesn't really make sense - ideally if there's any pre-defined state that a class should start with that should be defined in the constructor. If you're happy with the answer please accept/upvote it
I already did :D but my reputation is less than 15 and it's not taking my vote :(
Here the 'EmployeeService' defined in the controller, is an interface ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.