Java中的公共字段 [英] Public field in Java

查看:158
本文介绍了Java中的公共字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个Managed bean:

  import java.util.ArrayList; 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.sql.DataSource;
import javax.annotation.Resource;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
//或导入javax.faces.bean.ManagedBean;

import org.glassfish.osgicdi.OSGiService;

@Named(ApplicationController)
@SessionScoped

public class Tab1Page实现Serializable {
public String actionString;

public Tab1Page(){
}
public static final long serialVersionUID = 254415216070877770L;
//常量
public final static String hashKey =tab1PageTab;
public String actApplicationControllerionString =;

@PostConstruct
public void post(){
Format formatter;
日期日期=新日期();

// Time formate 01:12:53 AM
formatter = new SimpleDateFormat(hh:mm:ss a);
tabName = formatter.format(date);
System.out.println(Tab1Page\t+ tabName +\ t @ PostConstruct ...);
}

@PreDestroy
public void destroy(){
Format formatter;
日期日期=新日期();

// Time formate 01:12:53 AM
formatter = new SimpleDateFormat(hh:mm:ss a);
tabName = formatter.format(date);
System.out.println(Tab1Page\t+ tabName +\t @ PreDestroy ...);
}
字符串tabName;

public String getTabName(){
return this.getClass()。getName()。substring(this.getClass()。getName()。lastIndexOf(。))+ \ t+ tabName;
}

public void setTabName(String tabName){
this.tabName = tabName;
}

public String getActionString(){
String actionString = null;
return actionString;
}

public void setActionString(String actionString){
this.actionString = actionString;
}
}

当我尝试在Glassfish上部署它时, error:

  [#| 2012-04-16T21:42:51.689 + 0300 | INFO | glassfish3.1.2 | org.glassfish .osgiweb | _ThreadID = 63; _ThreadName = Thread-2; |根据上下文路径移除软件包416 /HM_57-1.0-SNAPSHOT |#] 

[#| 2012-04-16T21:42:51.690+ 0300 |警告| glassfish3.1.2 | org.glassfish.osgijavaeebase | _ThreadID = 63; _ThreadName =线程2; |无法部署捆绑包com.DX_57.History-Module-57 [416]
org.glassfish.osgijavaeebase .DeploymentException:由于以下原因com.DX_57.History-Module-57 [416]部署失败:部署bundle com.DX_57.History-Module-57 [416]时失败:java.lang.RuntimeException:部署bundle失败[com.DX_57.History-Module-57 [416]],根本原因:在org.glassfish.osgijavaeebase.AbstractOSGiDeployer.deploy上加载应用程序
时出现异常(AbstractOSGiDeployer.java:125)$ or $ b org .glassfish.osgijavaeebase.OSGiContainer.deploy(OSG org.glassfish.osgijavaeebase.JavaEEExtender.deploy(JavaEEExtender.java:107)
at org.glassfish.osgijavaeebase.JavaEEExtender.access $ 200(JavaEEExtender.java:61)
at org.glassfish.osgijavaeebase.JavaEEExtender $ HybridBundleTrackerCustomizer $ 1.call(JavaEEExtender.java:151)
at org.glassfish.osgijavaeebase.JavaEEExtender $ HybridBundleTrackerCustomizer $ 1.call(JavaEEExtender.java:148)
在java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:334)
在java.util.concurrent.FutureTask.run(FutureTask.java:166)
在java.util。 (ThreadPoolExecutor.java:603)$ b $在java.lang.Thread.run(线程。 java:722)
导致:java.lang.RuntimeException:未能部署bundle [com.DX_57.History-Module-57 [416]],根本原因:在org中加载应用程序
时出现异常.glassfish.osgij avaeebase.OSGiDeploymentRequest.deploy(OSGiDeploymentRequest.java:196)
at org.glassfish.osgijavaeebase.OSGiDeploymentRequest.execute(OSGiDeploymentRequest.java:118)
at org.glassfish.osgijavaeebase.AbstractOSGiDeployer.deploy(AbstractOSGiDeployer。 java:121)
... 10 more
由org.glassfish.deployment.common.DeploymentException引发:WELD-000075普通范围的托管bean实现类有一个公共字段:public @ Named @SessionScoped class com.DX_57.HM_57.Tab1Page
at org.glassfish.weld.WeldDeployer.event(WeldDeployer.java:181)
at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:128 )
at org.glassfish.internal.data.ApplicationInfo.load(ApplicationInfo.java:277)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:460)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240)
at org.glassfish.osgijavaeebase.OSGiDeploymentReque st.deploy(OSGiDeploymentRequest.java:183)
... 12 more
原因:org.jboss.weld.exceptions.DefinitionException:WELD-000075正常范围的托管bean实现类有一个公共字段: public @ Named @SessionScoped class com.DX_57.HM_57.Tab1Page
at org.jboss.weld.bean.ManagedBean.checkBeanImplementation(ManagedBean.java:438)
at org.jboss.weld.bean.AbstractClassBean .initialize(AbstractClassBean.java:191)
at org.jboss.weld.bean.ManagedBean.initialize(ManagedBean.java:322)
at org.jboss.weld.bootstrap.AbstractBeanDeployer.deploy(AbstractBeanDeployer .java:115)
at org.jboss.weld.bootstrap.BeanDeployment.deployBeans(BeanDeployment.java:204)
at org.jboss.weld.bootstrap.WeldBootstrap.deployBeans(WeldBootstrap.java:344 )
at org.glassfish.weld.WeldDeployer.event(WeldDeployer.java:179)
... 17 more
|#]

我不明白这个错误的含义。我修改了所有的方法是公开的,但我仍然得到这个错误。如何改正代码?



祝愿

解决方案

问题是字段( public String actionString; )而不是方法。 $ b

From docs


如果托管bean具有公共字段,则它必须具有@Dependent范围。
如果一个带有public字段的托管bean声明除
@Dependent之外的任何作用域,那么容器会自动检测到该问题并将
视为定义错误。



I have this Managed bean:

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.sql.DataSource;
import javax.annotation.Resource;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
// or import javax.faces.bean.ManagedBean;   

import org.glassfish.osgicdi.OSGiService;

@Named("ApplicationController")
@SessionScoped

public class Tab1Page implements Serializable {
    public String actionString;

    public Tab1Page() {
    }
    public static final long serialVersionUID = 254415216070877770L;
// Constants
    public final static String hashKey = "tab1PageTab";
    public String actApplicationControllerionString = "";

    @PostConstruct
    public void post() {
        Format formatter;
        Date date = new Date();

        // Time formate 01:12:53 AM
        formatter = new SimpleDateFormat("hh:mm:ss a");
        tabName = formatter.format(date);
        System.out.println("Tab1Page\t" + tabName + "\t@PostConstruct...");
    }

    @PreDestroy
    public void destroy() {
        Format formatter;
        Date date = new Date();

        // Time formate 01:12:53 AM
        formatter = new SimpleDateFormat("hh:mm:ss a");
        tabName = formatter.format(date);
        System.out.println("Tab1Page\t" + tabName + "\t@PreDestroy...");
    }
    String tabName;

    public String getTabName() {
        return this.getClass().getName().substring(this.getClass().getName().lastIndexOf(".")) + "\t" + tabName;
    }

    public void setTabName(String tabName) {
        this.tabName = tabName;
    }

    public String getActionString() {
        String actionString = null;
        return actionString;
    }

    public void setActionString(String actionString) {
        this.actionString = actionString;
    }
}

When I try to deploy it on Glassfish I get this error:

[#|2012-04-16T21:42:51.689+0300|INFO|glassfish3.1.2|org.glassfish.osgiweb|_ThreadID=63;_ThreadName=Thread-2;|Removed bundle 416 against context path /HM_57-1.0-SNAPSHOT |#]

[#|2012-04-16T21:42:51.690+0300|WARNING|glassfish3.1.2|org.glassfish.osgijavaeebase|_ThreadID=63;_ThreadName=Thread-2;|Failed to deploy bundle com.DX_57.History-Module-57 [416]
org.glassfish.osgijavaeebase.DeploymentException: Deployment of com.DX_57.History-Module-57 [416] failed because of following reason: Failed while deploying bundle com.DX_57.History-Module-57 [416] : java.lang.RuntimeException: Failed to deploy bundle [ com.DX_57.History-Module-57 [416] ], root cause: Exception while loading the app
    at org.glassfish.osgijavaeebase.AbstractOSGiDeployer.deploy(AbstractOSGiDeployer.java:125)
    at org.glassfish.osgijavaeebase.OSGiContainer.deploy(OSGiContainer.java:154)
    at org.glassfish.osgijavaeebase.JavaEEExtender.deploy(JavaEEExtender.java:107)
    at org.glassfish.osgijavaeebase.JavaEEExtender.access$200(JavaEEExtender.java:61)
    at org.glassfish.osgijavaeebase.JavaEEExtender$HybridBundleTrackerCustomizer$1.call(JavaEEExtender.java:151)
    at org.glassfish.osgijavaeebase.JavaEEExtender$HybridBundleTrackerCustomizer$1.call(JavaEEExtender.java:148)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.RuntimeException: Failed to deploy bundle [ com.DX_57.History-Module-57 [416] ], root cause: Exception while loading the app
    at org.glassfish.osgijavaeebase.OSGiDeploymentRequest.deploy(OSGiDeploymentRequest.java:196)
    at org.glassfish.osgijavaeebase.OSGiDeploymentRequest.execute(OSGiDeploymentRequest.java:118)
    at org.glassfish.osgijavaeebase.AbstractOSGiDeployer.deploy(AbstractOSGiDeployer.java:121)
    ... 10 more
Caused by: org.glassfish.deployment.common.DeploymentException: WELD-000075 Normal scoped managed bean implementation class has a public field:  public@Named @SessionScoped class com.DX_57.HM_57.Tab1Page
    at org.glassfish.weld.WeldDeployer.event(WeldDeployer.java:181)
    at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:128)
    at org.glassfish.internal.data.ApplicationInfo.load(ApplicationInfo.java:277)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:460)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240)
    at org.glassfish.osgijavaeebase.OSGiDeploymentRequest.deploy(OSGiDeploymentRequest.java:183)
    ... 12 more
Caused by: org.jboss.weld.exceptions.DefinitionException: WELD-000075 Normal scoped managed bean implementation class has a public field:  public@Named @SessionScoped class com.DX_57.HM_57.Tab1Page
    at org.jboss.weld.bean.ManagedBean.checkBeanImplementation(ManagedBean.java:438)
    at org.jboss.weld.bean.AbstractClassBean.initialize(AbstractClassBean.java:191)
    at org.jboss.weld.bean.ManagedBean.initialize(ManagedBean.java:322)
    at org.jboss.weld.bootstrap.AbstractBeanDeployer.deploy(AbstractBeanDeployer.java:115)
    at org.jboss.weld.bootstrap.BeanDeployment.deployBeans(BeanDeployment.java:204)
    at org.jboss.weld.bootstrap.WeldBootstrap.deployBeans(WeldBootstrap.java:344)
    at org.glassfish.weld.WeldDeployer.event(WeldDeployer.java:179)
    ... 17 more
|#]

I don't understand what this error means. I modified all methods to be public but still I get this error. How I can correct the code?

Best wishes

解决方案

The problem is the field (public String actionString;) not the methods.

From docs:

If a managed bean has a public field, it must have scope @Dependent. If a managed bean with a public field declares any scope other than @Dependent, the container automatically detects the problem and treats it as a definition error.

这篇关于Java中的公共字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆