简体   繁体   English

为什么不能在函数内使用ManagedProperty?

[英]Why can't I use ManagedProperty inside a function ?

I want to make the DB object be created only when it's required, so I want to do something like this 我想只在需要时才创建数据库对象,所以我想做这样的事情

public class Data {

public List<Group> getGroups(){
    List<Group> MyList=new ArrayList<Group>();
    Connection conn=null;
@ManagedProperty(value = "#{myConnection}")
myConnection getCon;

}

But that doesn't work, Instead I have to do it like this 但这是行不通的,相反,我必须这样做

public class Data {
@ManagedProperty(value = "#{myConnection}")
myConnection getCon;
public List<Group> getGroups(){
    List<Group> MyList=new ArrayList<Group>();
    Connection conn=null;


}

Why can't I make a managedProperty inside the function ? 为什么我不能在函数内部创建ManagedProperty? I thought about it and it should be okay to be outside since it'll be created when I create the object but is there anyway to make it inside the function ? 我考虑了一下,应该可以放在外面,因为它是在创建对象时创建的,但是无论如何都可以在函数内部创建它?

You can't use ManagedProperty annotation inside the function . 您不能在函数内部使用ManagedProperty批注。 It can be only used on a field. 它只能在字段上使用。 See the definition of the ManagedProperty below 请参阅下面的ManagedProperty的定义

@Retention(value=RUNTIME)
@Target(value=FIELD)
public @interface ManagedProperty

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不能在 Java 的 switch 语句中使用“继续”? - Why can't I use 'continue' inside a switch statement in Java? 为什么我不能在函数内部创建数组对象? - why I can't create my array object inside function? 在支持组件中使用@ManagedProperty - Use of @ManagedProperty in backing component 为什么我可以在main中使用外部非静态类和方法,而不能使用类中定义的类和方法? - Why can I use external non static classes and methods inside main, and can't with classes and methods defined inside the class? 为什么我不能在函数内保存来自 Scanner 的用户输入? - Why can't I save user input from Scanner inside a function? 如何返回一个 lambda 函数并通过将其作为参数传递给 Java 8 中的方法来使用? 为什么我不能使用 :: 运算符? - How to return a lambda function and use by passing as an argument to a method in Java 8? Why I can't use :: operator? 为什么我不能使用此变量 - Why can't I use this variable 为什么我不能在 JCreator 中使用 JavaFX? - Why can't I use JavaFX in JCreator? 为什么我不能使用覆盖注释? - Why can't I use Override annotation? 为什么我不能在Android上使用JMF? - Why I can't use JMF on Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM