Results 1 to 3 of 3

Thread: how to inject @Autowired when perform new Object() ?

  1. #1
    Join Date
    Apr 2012
    Posts
    8

    Default how to inject @Autowired when perform new Object() ?

    I am Using spring mvc 3.1 and i have factory class with static method that perform "new Object()"

    and the Object contain @Autowired inside which return null.

    All the beans are in servlet-context.xml how can i perform new and sill use the @Autowired in

    the Objec ?

    Thanks.

  2. #2
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    You can't. If you don't let the framework initialize the objects, then there is no injection (the @Autowired annotation is useless because there is no framework to process it). So new -> no injection. What you could do is replicate the behavior of your factory class inside the object's constructor, then register the object as a prototype bean.

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Well actually you can... By using AspectJ and the @Configurable annotation.. All this is explained in the reference guide.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

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
  •