我有用户pojo,这个pojo不是从T.延伸的。就像这样
@Entity
@Table(name = "USERS")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "USERS.findAll", query = "SELECT s FROM USERS s")})
public class USERS implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected USERSPK usersPK;
@Lob
@Column(name = "Name")
private Stirng name;
@Size(max = 20)
@Column(name = "Surname")
private String surname;
我想从这个表中选择smth.Service和DAO类如下:
public interface CommonService {
public List<Object> hepsiniGetir2(Class persistenceClass, String property, Object searchCrit);
}
这是接口的实现;
@Service("commonService")
public class CommonServiceImpl implements CommonService, Serializable {
@Transactional
public List<Object> hepsiniGetir2(Class persistenceClass, String property, Object searchCrit) {
return commonDao.findAllByCrit2(persistenceClass, property, searchCrit);
}
}
这里的dao界面:
public interface CommonDAO extends GenericDAO<TemelNesne, Long> {
public List<Object> findAllByCrit2(Class persistenceClass, String property, Object searchCrit);
}
这里实现dao类:
@Repository
public class CommonDAOImpl extends GenericDAOImpl<TemelNesne, Long> implements CommonDAO {
public List findAllByCrit2(Class persistenceClass, String property, Object searchCrit) {
Criteria c = sessionFactory.getCurrentSession().
createCriteria(persistenceClass).add(Restrictions.eq(property, searchCrit));
List<Object> list = c.list();
if (list.isEmpty()) {
return null;
} else {
return list;
}
}
}
在视图类中,我将此方法称为;
@ManagedBean(name="userView", eager=true)
@ViewScoped
public class UserView extends BaseView implements Serializable {
@ManagedProperty("#{commonService}")
private CommonService commonService;
private List<USERS> list;
@PostConstruct
public void init() {
list = (List) commonService.hepsiniGetir2(USERS.class, "name", "Deniz");
}
}
最后,我有这个例外:
Tem 04,2014 5:38:10 PM com.sun.faces.application.view.FaceletViewHandlingStrategy handleRenderException SEVERE:错误渲染视图[/userList.xhtml] com.sun.faces.mgbean.ManagedBeanCreationException:发生错误 在托管bean userView上执行资源注入引起: org.hibernate.type.SerializationException:无法反序列化 引起:java.io.StreamCorruptedException:无效的流标题: 3C3F786D
答案 0 :(得分:1)
您的问题是一个概念问题。 JSF bean不能自动装配Spring bean,也不能反过来。这是因为它们在不同的容器中管理。你应该将Spring与JSF集成以允许注入,这基本上归结为Spring控制一切。关于这样做,网上有很多教程。一个很好的教程是mkyong的:JSF 2 + Spring 3 Integration Example。基本上,这些是步骤(取自他的教程):
在faces-config.xml文件中,添加表达式语言(EL)解析器:
<application>
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
</application>
而是使用JSF注释,对JSF托管bean使用Spring注释。适用于您的类的示例:
@Component("userView")
@Scope("view")
public class UserView extends BaseView implements Serializable {
@Autowired
private CommonService commonService;
private List<USERS> list;
@PostConstruct
public void init() {
list = (List) commonService.hepsiniGetir2(Sertifikalar.class, "name", "Deniz");
}
}
请注意,在此示例中,我使用@Scope("view")
但Spring doesn't have a view scope by default, the team is still working on it。您必须手动实现此范围。幸运的是,您可以使用Cagatay's implementation来解决此问题。
除了这些问题之外,还有另一个概念性问题:支持eager=true
的唯一bean是@ApplicationScoped
,因为它将作为@Singleton
Spring bean运行,其他托管bean将忽略此属性。