1

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

김성진
  • 13
  • 2

1 Answers1

0

I suggest you adopt the KISS principle here:

public class MyFactory {
  public static MyObject newObject(SomeComponent someComponent /* and other parameter if needed */) {
    ... // in this scope(A), many classes are used to generate MyObject;
   return MyObject;  
  } 
}

Provide your factory construction method with the required parameters, including the autowired variable which gives you the required info from the DB, and call the Factory method directly whenever you need your new object.

The class you suggested is an overcomplication since it is a de-facto wrapper on your existing factory, with hard coding of a single DB related object, which is provided to your factory. This is a rigid implementation - what if you want to use a different instance of this object with your factory?

References:

Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
  • I apologize for my poor explanation. In my solution i need to implement `SomeService` because "MyFactory" alone can not get "SomeComponent" in spring context. so, someService is usage class of MyFactory. If I have a misunderstanding, let me know Thanks – 김성진 Apr 18 '19 at 04:55
  • @김성진 : Let me get this straight - you have no option of invoking `MyFactory.newObject` directly since you do not have access to an instance of `SomeComponent` ? – Rann Lifshitz Apr 18 '19 at 06:25
  • That`s right. `MyFactory.class` is pure Java class. not Spring bean. so, that cannot use `SomeComponent` injected through `@autowired`. – 김성진 Apr 18 '19 at 07:11
  • And `SomeService` is instead `@Autowired` and call `MyFactory.newObject` with injected parameter my intrinsic problem is not `SomeService` but the situation that `Spring Bean` is transfered `Not Spring Bean Class`(MyFactory) through parameter and used – 김성진 Apr 18 '19 at 07:18
  • @김성진 : Then based on your added descriptions - you do not have an alternative to your problem other than the implementation you used. I was under the assumption that you have the option of invoking the Factory new object creation directly with an instance that you have access to of `SomeComponent` – Rann Lifshitz Apr 18 '19 at 08:34