cbmeeks
cbmeeks

Reputation: 11420

How do I Autowire some beans to be automatically injected with Spring MVC 3?

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!

Upvotes: 0

Views: 1884

Answers (1)

shazinltc
shazinltc

Reputation: 3666

Couple of things about the controller you have put up.

@Controller
public class EmailController {

@Autowired
Login login;    // The container has to create a bean of type Login to autowire into EmailController

@Autowired
Employee employee;    //same as above

// getters/setters....
}

If the container has to create a singleton bean on application startup, you have to mark the Login and Employee class with annotation @Component. Even annotations like @Repository, @Service does this. You can have look at this answer for difference between these annotations.

So once you mark your classes with any of these annotations, singleton beans of respective type will be created on application startup. You will see something like this in your logs

Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6c811e18

And then a list of beans it has created. You can inject these beans to other beans. And these beans aren't stored in the session. But they are managed by the container itself.

You can do away with the xml bean definitions if your are using annotations like @Controller, @Component etc. Also you can avoid majority of your xml configuration files by using @Configuration. You can check here and here for examples.

Upvotes: 2

Related Questions