Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
I've just started reading through Core JavaServer Faces, 3rd Ed. and they say this (emphasis mine): It is a historical accident that there are two separate mechanisms, CDI beans and JSF managed beans, for beans that can be used in JSF pages. We suggest that you use CDI beans unless your application must work on a plain servlet runner such as Tomcat. Why? They don't provide any justi...
I am going through some blogs on SpringSource and in one of the blog author is using @Inject and I suppose he can also use @Autowired Here is the piece of code: @Inject private CustomerOrderService customerOrderService; I am not sure about the difference between @Inject and @Autowired and would appreciate if someone can explain the difference and which one to use under what situation?
there is a way to replace constructor-arg with Annotation? I have this constructor: public GenericDAOImpl(Class<T> type) { this.type = type; } and i need to inject that in my Facade: @Inject private GenericDAO<Auto, Long> autoDao; The problem is that i don't know how to pass the value of parameter in costructor. Thank you in advance [More Info] I try to explain my probl...
i was wondering if it possible to use the @Resource annotation on a constructor? My use case is that i want to wire a final field called Bar public class Foo implements FooBar { private final Bar bar; @javax.annotation.Resource(name="myname") public Foo(Bar bar) { this.bar = bar; } } I get a message that the @Resource is not allowed on this location, is there any o...
This question is related to a previous one on writing a session timeout handler. The answer in that thread involved accessing various session-scoped managed beans from the servlet. The recommendation (as seen here) is to do this in the filter: HttpSession session = request.getSession(false); User user = (session != null) ? (User) session.getAttribute("user") : null; Presumably this fetches...
I'm using the primefaces autocomplete element in a search form of my project. The user can choose how many and which form-elements (search parameters) he wants to include so i need to pass an ID to the completeMethod to each of them. I've tried adding onfocus="" to pass the object to the bean but that only would be activated when the element first is loaded. My question: How can I pass an attr...
We're looking at using the JSR-330 javax.inject.* annotations instead of the com.google.inject.* equivalents. It looks like the JSR-330 standard does not include several features that I've come to love in Guice. Specifically, I see no support for the @Assisted keyword. Also, what about @AssistedInject? Is @Inject able to be placed on multiple constructors? I'm interested in being vendor ne...
I'm using boolean switchers to resolve a choosed behaviour of application, for example SAVEACCEPTED enables SAVE button of form. <h:commandButton action="#{bean.save}" disabled="#{!bean.saveaccepted}"> JSF need private boolean and its getters and setters, but if I want to resolve some internal logic in application server, it must be defined static. For example IF (USERFOUND) SAVEACC...
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 {...
  /*
   * Copyright (C) 2009 The JSR-330 Expert Group
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package javax.inject;
 
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 import static java.lang.annotation.ElementType.METHOD;
 import static java.lang.annotation.ElementType.CONSTRUCTOR;
 import static java.lang.annotation.ElementType.FIELD;

Identifies injectable constructors, methods, and fields. May apply to static as well as instance members. An injectable member may have any access modifier (private, package-private, protected, public). Constructors are injected first, followed by fields, and then methods. Fields and methods in superclasses are injected before those in subclasses. Ordering of injection among fields and among methods in the same class is not specified.

Injectable constructors are annotated with @Inject and accept zero or more dependencies as arguments. @Inject can apply to at most one constructor per class.

@Inject ConstructorModifiersopt SimpleTypeName(FormalParameterListopt) Throwsopt ConstructorBody

@Inject is optional for public, no-argument constructors when no other constructors are present. This enables injectors to invoke default constructors.

@Injectopt Annotationsopt public SimpleTypeName() Throwsopt ConstructorBody

Injectable fields:

  • are annotated with @Inject.
  • are not final.
  • may have any otherwise valid name.

@Inject FieldModifiersopt Type VariableDeclarators;

Injectable methods:

  • are annotated with @Inject.
  • are not abstract.
  • do not declare type parameters of their own.
  • may return a result
  • may have any otherwise valid name.
  • accept zero or more dependencies as arguments.

@Inject MethodModifiersopt ResultType Identifier(FormalParameterListopt) Throwsopt MethodBody

The injector ignores the result of an injected method, but non-void return types are allowed to support use of the method in other contexts (builder-style method chaining, for example).

Examples:

   public class Car {
     // Injectable constructor
     @Inject public Car(Engine engine) { ... }

     // Injectable field
     @Inject private Provider<Seat> seatProvider;

     // Injectable package-private method
     @Inject void install(Windshield windshield, Trunk trunk) { ... }
   }

A method annotated with @Inject that overrides another method annotated with @Inject will only be injected once per injection request per instance. A method with no @Inject annotation that overrides a method annotated with @Inject will not be injected.

Injection of members annotated with @Inject is required. While an injectable member may use any accessibility modifier (including private), platform or injector limitations (like security restrictions or lack of reflection support) might preclude injection of non-public members.

Qualifiers

A qualifier may annotate an injectable field or parameter and, combined with the type, identify the implementation to inject. Qualifiers are optional, and when used with @Inject in injector-independent classes, no more than one qualifier should annotate a single field or parameter. The qualifiers are bold in the following example:

   public class Car {
     @Inject private @Leather Provider<Seat> seatProvider;

     @Inject void install(@Tinted Windshield windshield,
         @Big Trunk trunk) { ... }
   }

If one injectable method overrides another, the overriding method's parameters do not automatically inherit qualifiers from the overridden method's parameters.

Injectable Values

For a given type T and optional qualifier, an injector must be able to inject a user-specified class that:

  1. is assignment compatible with T and
  2. has an injectable constructor.

For example, the user might use external configuration to pick an implementation of T. Beyond that, which values are injected depend upon the injector implementation and its configuration.

Circular Dependencies

Detecting and resolving circular dependencies is left as an exercise for the injector implementation. Circular dependencies between two constructors is an obvious problem, but you can also have a circular dependency between injectable fields or methods:

   class A {
     @Inject B b;
   }
   class B {
     @Inject A a;
   }

When constructing an instance of A, a naive injector implementation might go into an infinite loop constructing an instance of B to set on A, a second instance of A to set on B, a second instance of B to set on the second instance of A, and so on.

A conservative injector might detect the circular dependency at build time and generate an error, at which point the programmer could break the circular dependency by injecting Provider<A> or Provider<B> instead of A or B respectively. Calling Provider.get() get() on the provider directly from the constructor or method it was injected into defeats the provider's ability to break up circular dependencies. In the case of method or field injection, scoping one of the dependencies (using singleton scope, for example) may also enable a valid circular relationship.

See also:
javax.inject.Qualifier @Qualifier
javax.inject.Provider
public @interface Inject {}
New to GrepCode? Check out our FAQ X