【问题标题】:init @PostConstruct doesnt work初始化 @PostConstruct 不起作用
【发布时间】:2015-08-27 01:51:02
【问题描述】:

我使用 hibernate4spring4 创建了一个网络动态项目。 这是我的文件:


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>posts</display-name>
  <welcome-file-list>
  <welcome-file>users.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
     <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application-context.xml</param-value>
    </context-param>
</web-app>

这是我调用 init() 方法的 bean

package com.posts.beans;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.posts.models.Users;
import com.posts.services.UsersService;

@SuppressWarnings("serial")
@Component("Usersbean")
@Scope("session")
public class UsersBean implements Serializable{


    @Autowired
    private transient UsersService us;
    private List<Users> lu;



    public UsersBean() {
    }



    @PostConstruct
    public void init(){
        lu=us.getAllUsers();
        System.out.println("hello");//doesn't shw up=init() doesnt  work
    }

    public String getMyname(){
        return "mohamed";
    }
    public List<Users> getLu() {
        return lu;
    }
    public void setLu(List<Users> lu) {
        this.lu = lu;
    }
}

这是我的jsp文件;我试图只显示一个字段:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test list users</title>
</head>
<body>

<jsp:useBean id="users" class="com.posts.beans.UsersBean" />
<table border="1">
<tr><th>id</th><th>nom</th><th>prenom</th><th>login</th><th>password</th></tr>
</table>

<c:forEach var="user" items="${users.lu}">
<p><c:out value="${user.nom}" /></p>
</c:forEach>
</body>
</html>

所以控制台不会在方法 init 中显示消息“hello”,这意味着 id 不起作用。 也许是因为我没有在 web.xm 中声明 servlet 元素,但我不知道它是如何工作的.. 注意:我使用 JUnit 测试了服务,工作正常

【问题讨论】:

  • 为什么要这样做,您没有使用托管的 Spring 实例,而是让 tje JSP 自己创建一个。调用不起作用(也不知道@PostConstruct
  • ii m new at using jsp .. 解决方案是什么??
  • 请勿使用jsp:useBean。使用 Spring Controller 为您准备模型。
  • 感谢您的快速回复,但是修改 web.xml 是否还有其他操作,因为??
  • 没有。您必须使用spring托管实例,您可以使用RequestContextUtils获取ApplicationContext并检索bean。但这基本上是您应该避免的视图层中的编程,因此是带有模型的控制器。

标签: spring hibernate jsp spring-mvc web.xml


【解决方案1】:

我已经解决了这个问题,用 @Service("userbean") 注释 UserBean 类并添加:

<bean id="usersBean" class="com.posts.beans.UsersBean" init-method="init" />

在 application-context.xml 文件中.. 当我运行我的应用程序时,我看到了休眠生成的 SELECT 代码和消息 hello

你好

休眠:从public.users users0_中选择users0_.id作为id1_2_,users0_.login作为login2_2_,users0_.nom作为nom3_2_,users0_.password作为password4_2_,users0_.prenom作为prenom5_2_来自public.users users0_

" 在控制台中表示init() 方法已被执行。

【讨论】:

    【解决方案2】:

    您可以通过三种不同的方式使其工作:

    1. 添加到您的 application-context.xml bean 中,该 bean 负责制定 @PostConstruct 注释:

      &lt;bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /&gt;

    2. 添加内部已经有 CommonAnnotationBeanPostProcessor 的命名空间:

    &lt;context:annotation-config /&gt;

    &lt;context:component-scan base-package="package.with.beans" /&gt; &lt;!-- scans components and adds &lt;context:annotation-config /&gt; --&gt;

    1. 在你的类的标签bean中为参数“init-method”定义init方法

    【讨论】:

      猜你喜欢
      • 2018-08-23
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2017-06-26
      • 2017-06-29
      • 2017-12-19
      相关资源
      最近更新 更多