문제

I know this kind of question is coming back regurarly but I wonder if there is a way to instantiate manually a bean (with new Xxxx()) and have my injection done. Maybe using BeanManager but I don't find how to.

If it is not possible, I wonder why this has not be done. Conceptually I don't find a good reason for making this not possible.

Thanks for your explanation.

P.S : i'm working with JBoss 7.1.1 and Weld CDI container.

JM.

도움이 되었습니까?

해결책

Because a fundamental concept behind CDI is the notion of scopes and their associated context. That's the Contexts in Contexts and Dependency Injection. Basically, a bean with a scope has a well defined life cycle, but when you instantiate a bean yourself there is no longer telling what its life cycle is. Read section 2.4 Scopes of the CDI spec.

To give you slightly more insight - CDI does not only @Inject your dependencies, but also manages their life cycle. That extends beyond simple instanciation and dependency injection, into termination (calling @PreDestroy) not only for the bean instance itself, but also for all the dependent instances. When you instanciate a bean yourself there is no telling when to do that last part.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top