import javax.enterprise.inject.Produces; class FacesContextProducer { @Produces @RequestScoped FacesContext getFacesContext() { return FacesContext.getCurrentInstance(); } }
@Transactional @Interceptor public class TransactionInterceptor { @Resource UserTransaction transaction; @AroundInvoke public Object manageTransaction(InvocationContext ctx) throws Exception { ... } }
@SessionScoped public class Login implements Serializable { @Inject Credentials credentials; @PersistenceContext EntityManager userDatabase; ... }
在Java EE @PostConstruct和@PreDestroy的生命週期回調也支持全部ManagedBean。全部資源注入完成後@PostConstruct方法會被調用。 java
固然,咱們建議組件環境注入是被用來定義CDI資源,而且在應用程序代碼中以類型安全的注入。public class LoginServlet extends HttpServlet { @Inject Credentials credentials; @Inject Login login; //只是示例,實際開發要避免重寫Service方法, //一爲了保留HttpServlet默認實現的緩存協商的機制(HTTP 304); //二就是禁用你沒有在servlet中重寫的方法,例如post、head等,這樣就從必定程度上提升了安全性 @Override public void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { credentials.setUsername(request.getParameter("username")): credentials.setPassword(request.getParameter("password")): login.login(); if ( login.isLoggedIn() ) { response.sendRedirect("/home.jsp"); } else { response.sendRedirect("/loginError.jsp"); } } }
servlet的實例共享全部傳入的線程. 緩存
@Transactional @MessageDriven public class ProcessOrder implements MessageListener { @Inject Inventory inventory; @PersistenceContext EntityManager em; public void onMessage(Message message) { ... } }
請注意,消息驅動bean不能注入@SessionScoped或@ConversationScoped的Bean。只有@RequestScoped和@ApplicationScopedBean可用。 安全
import javax.jms.ConnectionFactory; import javax.jms.Queue; public class OrderResources { @Resource(name="jms/ConnectionFactory") private ConnectionFactory connectionFactory; @Resource(name="jms/OrderQueue") private Queue orderQueue; @Produces @Order public Connection createOrderConnection() throws JMSException { return connectionFactory.createConnection(); } public void closeOrderConnection(@Disposes @Order Connection connection)throws JMSException { connection.close(); } @Produces @Order public Session createOrderSession(@Order Connection connection)throws JMSException { return connection.createSession(true, Session.AUTO_ACKNOWLEDGE); } public void closeOrderSession(@Disposes @Order Session session)throws JMSException { session.close(); } @Produces @Order public MessageProducer createOrderMessageProducer(@Order Session session)throws JMSException { return session.createProducer(orderQueue); } public void closeOrderMessageProducer(@Disposes @Order MessageProducer producer)throws JMSException { producer.close(); } }
使用示例: session
@Inject Order order; @Inject @Order MessageProducer producer; @Inject @Order Session orderSession; public void sendMessage() { MapMessage msg = orderSession.createMapMessage(); msg.setLong("orderId", order.getId()); ... producer.send(msg); }
注入的JMS對象的生命週期徹底由容器控制。 框架
beans.xml文件必須位於:META-INF/beans.xml(jar包),或者,WEB-INF/beans.xml或WEB-INF/classes/META-INF/beans.xml(war包)。 jsp
Over. ide