OK, so we are learning Spring MVC 3. And we are a little new to IoC, DI, etc. We want to clean up a lot of our old legacy mistakes. :-)
We are really liking @Autowired
for our user services, etc.
However, we now have an issue that we would like to solve with Autowiring.
Let's say we have a Login
bean:
public class Login {
private String username;
private String email;
// getters/setters....
}
This bean should be used session wide. We want every controller to be able to access this single object.
Which I'm assuming we need in our application-config.xml
<bean id="login" class="com.example.models.Login" scope="session" />
Also, let's say we have another class as:
public class Employee {
private String firstName;
private String lastName;
private Login login;
public Employee(Login paLogin) {
this.login = paLogin;
}
}
And to put that in session:
<bean id="employee" class="com.example.models.Employee" scope="session" />
OK, later on in our application, we have an email notification service. That service needs to access the username
and email
from the Login bean AND information from the Employee bean. Granted I could access the login bean from session memory but this is just an example.
@Controller
public class EmailController {
@Autowired
Login login; // this should come from session memory automatically right??
@Autowired
Employee employee; // OK, this should also come from session memory. Which contains a reference of the login too. Correct?
// getters/setters....
public void sendEmails() {
// ....
String email = login.getEmail();
String firstName = employee.getFirstName();
// ....
}
}
I hope this makes sense. What we are really wanting to accomplish is reducing XML configs, reducing constant parameter passing, minimal annotations, etc.
Any help that could point me in the right direction would be appreciated.
Thanks!