Results 1 to 4 of 4

Thread: How to inject Spring bean into Java code?

  1. #1
    Join Date
    Jan 2011
    Posts
    7

    Default How to inject Spring bean into Java code?

    Spring newbie here.

    I've read comments that it's not a good idea to use ApplicationContext.getBean() to retrieve beans, because that violates Inversion of Control. Instead, you should just declaratively "inject" your bean where you need it.

    So how do I do it?

    Let's say I have my Spring MVC Controller class, and I want to inject my bean here:

    Code:
    public class HelloController implements Controller {
    
        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
        {
             // use my DAO bean here
        }
    }
    And the bean that I want to inject is a DAO bean, defined in my applicationContext.xml as follows (I'm using Hibernate to connect to the DB):

    Code:
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="mappingResources">
                <list>
                    <value>Movie.hbm.xml</value>
                </list>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                    <prop key="show_sql">true</prop>
                </props>
            </property>
        </bean>
    
        <bean id="MainDao" class="MyApp.dao.MainDao">
            <property name="sessionFactory">
                <ref bean="sessionFactory" />
            </property>
        </bean>
    So my MainDao bean is defined and created by Spring, and now I need to inject it into my Controller class above, that handles the page. How do I do it? Is it enough to just create a member MainDao variable in my Controller, and assume it will be populated? Thanks.

  2. #2
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    I've read comments that it's not a good idea to use ApplicationContext.getBean() to retrieve beans, because that violates Inversion of Control. Instead, you should just declaratively "inject" your bean where you need it.
    Yes, is not recommendable that the application has knowledge about Spring itself, but some times would required

    So my MainDao bean is defined and created by Spring, and now I need to inject it into my Controller class above, that handles the page.
    An important rule you must consider is the MVC pattern, is not wise that the Controller call directly a DAO object, bad practice, should be

    Controller -> BO (Business Object or Service) -> DAO

    How do I do it? Is it enough to just create a member MainDao variable in my Controller, and assume it will be populated? Thanks.
    your MainDao must implement some interface MainDaoService (due Proxy pattern used by Spring), and such interface must be declared in your BO (controller for your case, recall it is wrong)

    Code:
    private MainDaoService mainDaoService
    
    //setter for your Variable
    Then Spring offer you two forms to inject, by XML or by annotations, it is the magic of Spring

    I suggest you read more documentation for more details about this
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  3. #3
    Join Date
    Jan 2011
    Posts
    7

    Default

    thanks Dr Pompeii,

    But suppose I have a Service like you said, can you give a quick example of how I would inject with annotations?

  4. #4
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    But suppose I have a Service like you said, can you give a quick example of how I would inject with annotations?
    In Spring documentation has good information about this, read about @Autowired and @Qualifier, like plus you must read about @Repository, @Controller and @Service to get all the piece working together
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •