Answer the question
In order to leave comments, you need to log in
How does the @Inject annotation work?
Hello. I'll try to explain my question with an example.
There is an entity class displayed in the database, and a controller class for it:
public class Controller {
@Inject
private EntityManager em;
<...>
}
<...>
public void add() {
Controller с = new Controller();
с.save(new Entity("habrahabr"));
}
<...>
field em
is equal to null
. (Hibernate is connected to the project as a provider.) Answer the question
In order to leave comments, you need to log in
At what point in time and under what conditions does this annotation “work”?In order for the annotation to work, you need a configured data source deployed by the JBoss server before your application starts (which is achieved by specifying dependencies in your deployed module), as well as connecting your business logic as a module in the EAR, for which in application.xml specify
<module>
<java>ваша-логика.jar</java>
</module>
1. The
2. Any dependency injection framework requires beans to be managed through it; accordingly, it is impossible to create objects through new in the general case. If you need an instance of the Controller, create it the same way, via
Why do you have Inject ? and not @PersistenceContext(unitName = "Blablabla") and so on? Maybe I misunderstood something in your question, but I always use @PersistenceContext.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question