---
title: "在Tomcat7上配置JSF的CDI支持"
author: "minfirefox"
date: "2011-08-29T19:35:17Z"
tags: []
categories: ["编程语言"]
slug: "tomcat7-jsf-cdi"
draft: false
lastmod: "2011-08-30T00:14:34Z"
id: 1160760
url: "https://minfirefox.iteye.com/blog/1160760"
---

**在Tomcat7上添加JSF2.0的Contexts and Dependency Injection(CDI)支持**

**1.** JavaBean中把@ManagedBean改为@Named，把Session改为javax.enterprise.context包下的，JavaBean 必须实现Serializable接口。如下：

 

```
import java.io.Serializable;
import javax.inject.Named; 
import javax.enterprise.context.SessionScoped; 
@Named
@SessionScoped
```

**2.** 在[http://seamframework.org/](http://seamframework.org/ "http://seamframework.org/") 下载CDI的weld实现。  
添加\\artifacts\\weld\\**weld-servlet.jar** 这个文件到Build Path中。

**3. web.xml** 中添加以下代码：

 

```
<listener> 
	<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class> 
</listener> 
```

**4.** 在WEB-INF目录下增加空文件**beans.xml** ：

 

```
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>
```

重启Tomcat7，配置完成！

参考文章：  
[http://www.theserverside.com/tutorial/Working-with-CDI-and-JSF-20-on-Tomcat-7-Configuring-Weld](http://www.theserverside.com/tutorial/Working-with-CDI-and-JSF-20-on-Tomcat-7-Configuring-Weld)