java - How to autowire services outside of the constructor? -
is possible add @autowired
class, , add constructor class not use these autowired classes?
example:
public class myjob { @autowired private myservice1 serv1; @autowired private myservice2 serv2; public myjob(string filename) { this.filename = filename; } }
here want take myservice1
, myservice2
granted, , initialize class @bean
using constructor:
@bean public getmyjob1() { return new myjob("test1"); } @bean public getmyjob2() { return new myjob("test2"); }
is possible?
i think best way make factory myjob's instances:
public class myjobfactory { private myservice1 serv1; private myservice2 serv2; @autowired public myjobfactory(myservice1 serv1, myservice2 serv2) { this.serv1 = serv1; this.serv2 = serv2; } public myjob myjob(string filename) { return new myjob(filename, serv1, serv2); } }
Comments
Post a Comment