0

Can I use the Dependency Injection in this case ? How can I configure the bean Article in the applicationContext.xml?

public class Test{

public void methodX(){
   .....
    while{

     Article a=new Article();
     ...
    }//while 
}//methodX
}//Test

Edit:

This is the solution which I have used:

 package factory;

 import bean.ArticoliOrdine;

 public abstract class ArticlesFactory {
   public abstract Article createArticle();
 }

 @Transational
 public class CartDAOImpl implements CartDAO {
    @Autowired
     private Cart cart;

    @PersistenceContext
    private EntityManager em;

    @Autowired
    ArticlesFactory articlesFactory;

    public void buy(){  
          ....
     while{
        Article article=articlesFactory.createArticle();
              .....
        em.persist(article);
     }//while
    }//buy
 }//CartDAOImpl 

In applicationContext

    <bean id="articolesFactory" class="factory.ArticlesFactory">
       <lookup-method name="createArticles" bean="articles"/>
    </bean>

   <bean class="bean.Articles" id="articles" scope="prototype"/>
Alex
  • 2,075
  • 5
  • 27
  • 39

1 Answers1

0

There are at least two ways to achieve that:

  1. Make Article @Configurable: @Configurable is a spring feature that allows you to configure (hence the name) instances that are not created under container control - as your Article instance named a.

To make a class @Configurable you simply annotate it. After that, you can inject dependencies into that class as if it was created by the container. Example:

@Configurable
public class Article {
    // This gets injected every time you instantiate a new Article by 'new Article()'
    @Autowired private SomeOtherClass dependency;
}

The downside: This uses aspects to do its magic, so you will have to either use compile time weaving or load time weaving (preferred). If you want to give it a try: http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#aop-aj-ltw

  1. Create a factory and inject it into class Test:

    @Component
    public class ArticleFactory {
        // Any stuff you want to configure article with
        @Autowired private SomeOtherClass dependency;
    
        public Article get() {
            Article a = new Article();
            a.setStuff(dependency);
            return a;
        }
    }
    

and later in class Test:

@Component
public class Test{

    @Autowired private ArticleFactory factory;

    public void methodX(){
       .....
        while{

           Article a = factory.get();
           ...
        }//while 
    }//methodX
}//Test
Dirk Lachowski
  • 3,121
  • 4
  • 40
  • 66
  • As you are not showing your code it's hard to tell. Add your solution as an answer. – Dirk Lachowski Dec 03 '14 at 13:39
  • The solution is described in this [link](http://docs.spring.io/spring/docs/3.0.0.RC3/reference/html/ch03s04.html#beans-factory-method-injection) – Alex Dec 03 '14 at 13:44
  • I know lookup method injection, but without knowing your code using it (i suppose your code is doing more than the `Test` class you posted) it's hard to decide. If i had to make a guess i would say that that's not the way you want to go, especially because your `Article` class is not managed. Using method lookup you are depending on CGLIB and then it's no big step to a `@Configurable`. If you want the simplest solution (in terms of libs depending on and avoiding trouble) you should use my second suggestion. – Dirk Lachowski Dec 03 '14 at 17:05
  • But in your second suggestion , Article is not managed by Spring Container.Later I will insert the solution – Alex Dec 03 '14 at 18:30
  • That's correct, that is what you have asked for in your question: How to configure an object not created by the container. – Dirk Lachowski Dec 03 '14 at 22:21
  • Sorry but my bad English didn't understand the question to you.I wanted to know how to initialize/create Article in Spring container.Sorry again..for that your solution was strange.. – Alex Dec 03 '14 at 23:44
  • Is it a good solution if I use a `Provider
    ` or a `ObjectFactory
    ` instead of using lookup method injection?
    – Alex Dec 04 '14 at 14:32