日期:2014-05-16  浏览次数:20358 次

jsf+spring实现完全依托spring的注入方式

完全脱离managed-beans 管理bean.让spring管理bean的依赖。

其实JSF也是采用set方式提供对bean的依赖注入,但是并不完全和spring相同。因为Spring可以给我们提供更简单的注入方式,并且也提供了更多的功能,如安全验证和事物管理等等。

下面演示一下怎么实现让Spring完全的管理Bean

定义一个pojo Person.java

package pojo;

public class Person {
private String name;
private int age;
private IAction action;

public IAction getAction() {
?? return action;
}

public void setAction(IAction action) {
?? this.action = action;
}

public String getName() {
?? return name;
}

public void setName(String name) {
?? this.name = name;
}

public int getAge() {
?? return age;
}

public void setAge(int age) {
?? this.age = age;
}
public void isExecute(){
?? action.doSomething();
?? System.out.println("哈哈"+this.getName());
}

}

action接口 IAction.java

public interface IAction {
??? public void doSomething();
}

action接口实现类 ActionImpl

package pojo;

public class ActionImpl implements IAction {

@Override
public void doSomething() {
System.out.println("正在打架");
}

}
到此Java的业务逻辑部分完成,下面是配置文件。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.5"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee?? http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
?? <param-name>contextConfigLocation</param-name>
?? <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
?? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
?? <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<servlet>
?? <servlet-name>Faces Servlet</servlet-name>
?? <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
?? <load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
?? <servlet-name>Faces Servlet</servlet-name>
?? <url-pattern>*.faces</url-pattern>
</servlet-mapping>
<welcome-file-list>
?? <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

注意:两个监听类都需要引入spring.web包

faces.xml

<?xml version='1.0' encoding='UTF-8'?>

<faces-config 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/web-facesconfig_1_2.xsd"
version="1.2">

<application>
?? <variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>
</application>
</faces-config>

很简单就一句话

Spring的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
??? <bean id="action" class="pojo.ActionImpl"></bean>
??? <bean id="bean" class="pojo.Person" scope="request"> bean的存在范围
???? <property name="action" ref="action"></property>
??? </bean>

</beans>

最后页面 index.faces

<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ tagli