简体   繁体   中英

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 ? 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 . It can be only used on a field. See the definition of the ManagedProperty below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM