问题
Im really new to java, jsf, jsp, and I need to learn how it works quickly. So the website Im using to practise has some terms etc that I dont know what they mean and Im hoping somebody can explain what they mean and how/what they are used for:)
Requestscoped
Applicationscoped
Sessionscoped
EntityManager
and could someone walk me through what these lines do?
@RequestScoped
public class Dao {
@DataRepository
@Inject
private EntityManager entityManager;
回答1:
First of all, in Java (5 and higher), "things" starting with a @
(e.g. @Deprecated
) are called annotations.
Annotations provide data about a program that is not part of the program itself. They have no direct effect on the operation of the code they annotate.
Your JavaBeans needs to be configured to a scope if you want to use it in JSF (Definitions can be found here).
@RequestScoped
: Objects with this scope are visible from the start of the request until the end of the request. Request scope starts at the beginning of a request and ends when the response has been sent to the client. If the request is forwarded, the objects are visible in the forwarded page, because that page is still part of the same request/response cycle. Objects with request scope can use other objects with none, request, session, or application scope. If you have to think it in terms of servlet, the managed bean is stored in theHttpServletRequest
until the end of the request (when the response is sent to the client). After that, the bean no longer exists in the request.@SessionScoped
: An object with session scope is visible for any request/response cycle that belongs to a session. Objects with this scope have their state persisted between requests and last until the object or the session is invalidated. Objects with session scope can use other objects with none, session, or application scope. Basically, these objects are stored in aHttpSession
(refer to Servlets again). Each session has a session ID (known asJSESSIONID
) that the bean is associated with.ApplicationScoped
: An object with application scope is visible in all request/response cycles for all clients using the application, for as long as the application is active. In terms of Servlet, this could be, managed bean stored in aServletConfig
.@NoneScoped
: Objects with this scope are not visible in any JSF page. When used in the configuration file, they indicate managed beans that are used by other managed beans in the application. Objects with none scope can use other objects with none scope.
For EntityManager, this is associated with persistence context. It is used to create and remove persistent entity instances, to find entities by their primary key identity, and to query over all entities. For more, refer to the JPA (Java Persistence API) Specification, or Hibernate.
@Inject, means that the instance is injectable. They follow the infamous crase word of Depency Injection or Inversion of Control (IOC). What this basically mean, is that when the resource (in your case EntityManager entityManager
is needed, the JEE container instantiates the resource for you (without you needed to instantiate it directly through e.g. a constructor, etc.).
I have no clue what @DataRepository
means. Never seen it before.
I hope this helps you.
回答2:
These terms are usually associated with a dependency injection framework like guice and not with java in particular.
http://code.google.com/p/google-guice/wiki/Scopes describes the various scopes that are built into guice.
By default, Guice returns a new instance each time it supplies a value. This behaviour is configurable via scopes. Scopes allow you to reuse instances: for the lifetime of an application (
@Singleton
), a session (@SessionScoped
), or a request (@RequestScoped
). Guice includes a servlet extension that defines scopes for web apps. Custom scopes can be written for other types of applications.
来源:https://stackoverflow.com/questions/5526145/java-terms-and-random-words-clarification