# javax.annotation

***

**1. Autowired Dependency Injection:**

```java
import javax.annotation.Autowired;

public class UserService {

    @Autowired
    private UserRepository userRepository;

    public void saveUser(User user) {
        userRepository.save(user);
    }
}
```

**2. Resource Injection (JNDI Lookup):**

```java
import javax.annotation.Resource;

public class MessageService {

    @Resource(name="jms/TopicConnectionFactory")
    private ConnectionFactory connectionFactory;

    public void sendMessage(String message) {
        // ...
    }
}
```

**3. Qualifier-Based Dependency Injection:**

```java
import javax.annotation.Resource;
import javax.annotation.Qualifier;

@Qualifier
public @interface MyQualifier {}

public class BeanFactory {

    @Resource(name="myBean")
    @MyQualifier
    private MyBean myBean;
}
```

**4. Transaction Management (Spring):**

```java
import javax.annotation.Transactional;

@Transactional
public class TransactionalService {

    public void doSomething() {
        // Method will be executed within a transaction
    }
}
```

**5. Persistence Context (EJB):**

```java
import javax.annotation.PersistenceContext;

public class PersistenceBean {

    @PersistenceContext(unitName="myPersistenceUnit")
    private EntityManager em;

    public void doSomething() {
        // Use the EntityManager to perform database operations
    }
}
```

**6. Event-Driven Programming (EventBus):**

```java
import javax.annotation.Subscribe;
import javax.enterprise.event.Event;

public class EventSubscriber {

    @Subscribe
    public void handleEvent(Event data) {
        // Handle the event
    }
}
```

**7. Asynchronous Execution (EJB):**

```java
import javax.annotation.Asynchronous;

@Asynchronous
public void doSomethingAsynchronous() {
    // This method will be executed asynchronously
}
```

**8. Interception (JAX-RS):**

```java
import javax.annotation.Priority;
import javax.ws.rs.Priorities;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;

@Priority(Priorities.AUTHENTICATION)
public class AuthFilter implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext requestContext) {
        // Perform authentication
    }
}
```

**9. CDI Injection (Simplified):**

```java
import javax.inject.Inject;

public class SimpleBean {

    @Inject
    private AnotherBean anotherBean;
}
```

**10. JSR-330 Annotations Support:**

```java
import javax.inject.Inject;
import javax.annotation.PostConstruct;

public class Jsr330Bean {

    @Inject
    private AnotherBean anotherBean;

    @PostConstruct
    private void init() {
        // Post-construction initialization
    }
}
```

**11. Specifying Injection Target in XML (Spring):**

```xml
<bean id="myBean" class="com.example.MyBean">
    <property name="dependency" ref="dependencyBean" />
</bean>
```

**12. Qualifiers for CDI (Simplified):**

```java
import javax.inject.Named;

@Named("myQualifier")
public class MyBean {
}
```

**13. Scopes for CDI (Simplified):**

```java
import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class MyBean {
}
```

**14. Dependency Injection with Constructors (Spring):**

```java
import org.springframework.beans.factory.annotation.Autowired;

public class MyBean {

    @Autowired
    public MyBean(AnotherBean anotherBean) {
    }
}
```

**15. Injection of Interfaces and Abstract Classes (Spring):**

```java
import org.springframework.beans.factory.annotation.Autowired;

public class MyBean {

    @Autowired
    private MyInterface myInterface;
}
```

**16. CDI Interceptors (Simplified):**

```java
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

@Interceptor
public class MyInterceptor {

    @AroundInvoke
    public Object intercept(InvocationContext context) throws Exception {
        // Intercept the method call
        return context.proceed();
    }
}
```

**17. Injection on Constructor Arguments (EJB):**

```java
import javax.annotation.PostConstruct;
import javax.ejb.EJB;

public class MyBean {

    @EJB
    private AnotherBean anotherBean;

    @PostConstruct
    private void init() {
    }
}
```

**18. Injection on Setter Methods (EJB):**

```java
import javax.annotation.PostConstruct;
import javax.ejb.EJB;

public class MyBean {

    @EJB
    public void setAnotherBean(AnotherBean anotherBean) {
    }

    @PostConstruct
    private void init() {
    }
}
```

**19. Interceptors on EJBs (Simplified):**

```java
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

@Interceptor
public class MyInterceptor {

    @AroundInvoke
    public Object intercept(InvocationContext context) throws Exception {
        // Intercept method calls on EJBs with @Interceptors
        return context.proceed();
    }
}
```

**20. Bean Validation (JSR-303 / Hibernate Validator):**

```java
import javax.validation.constraints.NotNull;

public class MyBean {

    @NotNull
    private String name;
}
```

**21. RESTful Web Services Annotations (JAX-RS):**

```java
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("/myResource")
public class MyResource {

    @GET
    @Produces("text/plain")
    public String get() {
        return "Hello, world!";
    }
}
```

**22. Event-Driven Programming (EJB):**

```java
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.enterprise.event.Event;
import javax.inject.Inject;

@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class EventBean {

    @Inject
    private Event<MyEvent> event;

    public void raiseEvent() {
        MyEvent myEvent = new MyEvent();
        event.fire(myEvent);
    }
}
```

**23. Web Services Invocation (JAX-WS):**

```java
import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class MyWebService {

    @WebMethod
    public String helloWorld() {
        return "Hello, world!";
    }
}
```

**24. Singleton Scope (JAX-RS):**

```java
import javax.annotation.Singleton;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Singleton
@Path("/mySingleton")
public class MySingletonResource {

    @GET
    @Produces("text/plain")
    public String get() {
        return "I am a singleton!";
    }
}
```

**25. Asynchronous Invocation (JAX-RS):**

```java
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;

public class AsyncInvocation {

    public static void main(String[] args) {
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target("http://localhost:8080/myResource");
        Response response = target.request().async().get();
    }
}
```

**26. Injection in Servlets (JSR-330 / Servlet 3.0):**

```java
import javax.inject.Inject;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/myServlet")
public class MyServlet extends HttpServlet {

    @Inject
    private MyService service;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        service.doSomething();
    }
}
```

**27. CDI Producers (Simplified):**

```java
import javax.annotation.Produces;
import javax.enterprise.inject.Produces;

public class MyProducer {

    @Produces
    public MyBean produceBean() {
        return new MyBean();
    }
}
```

**28. Interceptors on Methods (EJB):**

```java
import javax.annotation.PostConstruct;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

@Interceptor
public class MethodInterceptor {

    @AroundInvoke
    public Object intercept(InvocationContext context) throws Exception {
        // Intercept method calls on EJBs with @Interceptors
        return context.proceed();
    }

    @PostConstruct
    private void init() {
    }
}
```

**29. Injection of EJBs (Simplified):**

```java
import javax.annotation.EJB;

public class MyBean {

    @EJB
    private AnotherBean anotherBean;
}
```

**30. JAX-RS Resources with Singleton Scope:**

```java
import javax.annotation.Singleton;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Singleton
@Path("/mySingleton")
public class SingletonResource {

    @GET
    @Produces("text/plain")
    public String get() {
        return "I am a singleton resource!";
    }
}
```

**31. CDI Producers for Singleton Beans (Simplified):**

```java
import javax.annotation.Produces;
import javax.enterprise.inject.Produces;
import javax.enterprise.context.ApplicationScoped;

public class SingletonProducer {

    @Produces
    @ApplicationScoped
    public MyBean produceBean() {
        return new MyBean();
    }
}
```

**32. Injection on Fields (Spring):**

```java
import org.springframework.beans.factory.annotation.Autowired;

public class MyBean {

    @Autowired
    private AnotherBean anotherBean;
}
```

**33. Lifecycle Methods (EJB):**

```java
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class MyBean {

    @PostConstruct
    private void init() {
    }

    @PreDestroy
    private void destroy() {
    }
}
```

**34. CDI Qualifier with Binding Annotation (Simplified):**

```java
import javax.annotation.Qualifier;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Qualifier
@Retention(RUNTIME)
@Target({FIELD, METHOD, TYPE})
public @interface MyQualifier {

    String value();
}
```

**35. CDI Producers for Interfaces (Simplified):**

```java
import javax.annotation.Produces;
import javax.enterprise.inject.Produces;

public class InterfaceProducer {

    @Produces
    public MyInterface produceInterface() {
        return new MyInterfaceImpl();
    }
}
```

**36. Injection on Constructor Parameters (EJB):**

```java
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.inject.Inject;

public class MyBean {

    @Inject
    private AnotherBean anotherBean;

    @EJB
    public MyBean(AnotherBean anotherBean) {
    }

    @PostConstruct
    private void init() {
    }
}
```

**37. CDI Interceptors (Simplified):**

```java
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

@Interceptor
public class MyInterceptor {

    @AroundInvoke
    public Object intercept(InvocationContext context) throws Exception {
        // Intercept method calls on CDI beans with @Interceptors
        return context.proceed();
    }
}
```

**38. JPA EntityManager Injection (Spring Data JPA):**

```java
import org.springframework.data.jpa.repository.JpaRepository;

public interface MyRepository extends JpaRepository<MyEntity, Long> {
}
```

**39. CDI Dependent Scope (Simplified):**

```java
import javax.enterprise.context.Dependent;

@Dependent
public class MyBean {
}
```

**40. CDI Optional Injection (Simplified):**

```java
import javax.annotation.Inject;
import javax.enterprise.inject.Default;

public class MyBean {

    @Inject
    private MyInterface myInterface;
}
```

**41. JPA Entity Listeners (Simplified):**

```java
import javax.persistence.PrePersist;
import javax.persistence.EntityListener;

@EntityListener
public class MyEntityListener {

    @PrePersist
    public void prePersist(Object entity) {
    }
}
```

**42. Java EE Transactions (Simplified):**

```java
import javax.transaction.Transactional;

public class MyBean {

    @Transactional
    public void doSomething() {
    }
}
```

**43. JPA Named Queries (Simplified):**

```java
import javax.persistence.NamedQuery;

@NamedQuery(name = "MyQuery", query = "SELECT e FROM MyEntity e WHERE e.name = :name")
public class MyEntity {
}
```

**44. Injection of JNDI Resources (Simplified):**

```java
import javax.annotation.Resource;
import javax.sql.DataSource;

public class MyBean {

    @Resource(name = "jdbc/test")
    private DataSource dataSource;
}
```

**45. JMX Managed Beans (Simplified):**

```java
import javax.management.MBean;

@MBean
public class MyManagedBean {
}
```

**46. JPA Criteria Queries (Simplified):**

```java
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

public class MyBean {

    public void doSomething() {
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        CriteriaQuery<MyEntity> cq = cb.createQuery(MyEntity.class);
        Root<MyEntity> root = cq.from(MyEntity.class);
        cq.select(root).where(cb.equal(root.get("name"), "test"));
        List<MyEntity> results = entityManager.createQuery(cq).getResultList();
    }
}
```

**47. CDI Event-Driven Programming (Simplified):**

```java
import javax.enterprise.event.Event;
import javax.inject.Inject;

public class MyBean {

    @Inject
    private Event<MyEvent> event;

    public void doSomething() {
        MyEvent myEvent = new MyEvent();
        event.fire(myEvent);
    }
}
```

**48. Injection on Class Fields (Spring):**

```java
import org.springframework.beans.factory.annotation.Value;

public class MyBean {

    @Value("${myProperty}")
    private String myProperty;
}
```

**49. Java EE Security (Simplified):**

```java
import javax.annotation.security.RolesAllowed;

public class MyBean {

    @RolesAllowed("admin")
    public void doSomething() {
    }
}
```

**50. JPA Cascades (Simplified):**

```java
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;

@Entity
public class MyEntity {

    @ManyToOne(cascade = CascadeType.ALL)
    private MyParentEntity parentEntity;
}
```
