Results 1 to 5 of 5

Thread: How to load a bean that is autowired?

Hybrid View

  1. #1

    Default How to load a bean that is autowired?

    I'm trying to run my spring container in a standalone application (it is from a spring mvc application).

    So the context loads fine, but the problem is my services are autowired in my controllers.

    Code:
    @Autowired
    ProductService productService;
    How can I load this in my standalone application now?

    ProductService ps = (ProductService) context.getBean("productService");

    I can the error:

    Code:
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'productService' is defined.

    Is the only way for this to work is to manually define it then?

  2. #2
    Join Date
    Dec 2010
    Location
    Singapore
    Posts
    302

    Default

    Hi, check followings,

    1. If you have a bean named "productService" in the application context (xml)
    2. Or if you have defined your beans through annotations, check if you have set the name (or correct name) . i.e. @Service(value="productService") or @Component(value="productService")
    Amila Domingo

  3. #3

    Default

    I have this:

    Code:
    @Service
    public class UserServiceImpl implements UserService {
    
    }
    Is that not enough?

  4. #4
    Join Date
    Feb 2011
    Posts
    1

    Default

    Have you "annotation-config" and "component-scan" tags in your xml??

  5. #5
    Join Date
    Dec 2010
    Location
    Singapore
    Posts
    302

    Default

    What i meant was make sure that you set the bean name.

    Code:
    @Service(value="productService")
    public class UserServiceImpl implements UserService {
    
    }
    Note the highlighted part.
    Amila Domingo

Tags for this Thread

Posting Permissions

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