Results 1 to 2 of 2

Thread: @Autowired Explanation

  1. #1

    Default @Autowired Explanation

    New to Spring, and trying to understand how the following example works:

    ControllerA.java

    package com.web;

    @Controller
    public class ControllerA {
    @Autowired
    private PeopleManager pMgr;

    public ModelAndView get(Abilities abilities )
    {
    List<People> people = pMgr.loadPeople( ... );
    ....
    }
    }

    PeopleManager.java

    package com.service;

    public interface WikiManager
    {
    List<People> loadPeople( ... );
    }

    PeopleManagerImpl.java

    package com.service;

    @Component
    public class PeopleManagerImpl implements PeopleManager {

    @Override
    public List<People> loadPeople( ... ) {
    ....
    }
    }

    ApplicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schem...-beans-2.5.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schem...ng-aop-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config />

    <context:component-scan base-package="com" />

    <aop:aspectj-autoproxy />
    </beans>

    I understand conceptually what should happen through the use of autowiring, but I just don't see how this example works? How does Spring know that PeopleManager is really PeopleManagerImpl? PeopleManger doesn't even have an annotated tag to indicate it should be auto instantiated by the component scan tag.

    Any help would be appreciated to this newbie.

    Thanks

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

    Default

    Hello

    1) use code tags, is more readable for us
    2) Read the Spring documentation
    3)

    but I just don't see how this example works? How does Spring know that PeopleManager is really PeopleManagerImpl?
    I suggest you read carefully about Proxy, Spring work closely with this pattern

    4)

    PeopleManger doesn't even have an annotated tag to indicate it should be auto instantiated by the component scan tag.
    An interface cant be instantiated, but the implementation does, and since it implements the interface you got the relation, again read about Proxy

    HTH
    - 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
  •