Announcement Announcement Module
Collapse
No announcement yet.
How to load a bean that is autowired? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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")

    Comment


    • #3
      I have this:

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

      Comment


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

        Comment


        • #5
          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.

          Comment

          Working...
          X