suppose under situation, i have spring component that access database and get some data from database
@Component
public class SomeComponent {
@Autowiried
private Datasource datasource;
...
public SomeModel getModel() {
....
return result;
}
}
and i have a Factory structure that generate instance with some parameter. and this structure is not Spring Component
public class MyFactory {
public static MyObject newObject(...) {
... // in this scope(A), many classes are used to generate MyObject;
return MyObject;
}
}
this structure wants to access database, because it have to check data from database to generate proper Object
very simple solution that i think is this.
in some service layer
@Component
public class SomeService {
@Autowired
private SomeComponent someComponent;
public MyObject getMyObject() {
return MyFactory.newObject(someComponent);
}
}
my question is is this anti pattern in Spring usage?
in this solution, Non spring context Class get Spring Components through method parameter, and use them
if for some reason it is anti pattern, please guide to me solve this situation