2016-12-24 61 views

回答

0

你可以使用構造函數注入。
它沒有通過反射注入田地的缺點。 這裏是Spring

@Service 
public class FooService { 

    private final FooRepository repository; 

    @Autowired 
    public FooService(FooRepository repository) { 
     this.repository = repository 
    } 
} 
+0

不,我需要從哪裏得到JPA庫的情況下,我需要一個,當我創建另一個bean實例,要知道一個例子。 '@Bean FooService fooService(){return new FooService({jpa repository instance}); }' – Artegon

+1

在這種情況下,您可以爲fooService添加一個方法參數:'@Bean FooService fooService(FooRepository repository){}'。 Spring將自動注入存儲庫。 – dunni