简体   繁体   English

@Autowired 在多模块应用程序中不起作用

[英]@Autowired not working in multi-module application

I need to @Autowire database services or repositores form "database" module in "game" module.我需要@Autowire 数据库服务或存储库形成“游戏”模块中的“数据库”模块。 Already added those annotations in main "Application" class:已经在主“应用程序”class 中添加了这些注释:

@Configuration
@EnableMethodSecurity
@SpringBootApplication
@EnableAutoConfiguration
@EntityScan(basePackages="com.rydzwr.tictactoe")
@ComponentScan(basePackages="com.rydzwr.tictactoe")
@EnableJpaRepositories(basePackages="com.rydzwr.tictactoe")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Adding them in constructor is much more difficult, becouse I'm need to create list of instances of those classes in StrategySelector:将它们添加到构造函数中要困难得多,因为我需要在 StrategySelector 中创建这些类的实例列表:

@Service
public class GameStrategySelector {
    private final List<BuildGameStrategy> strategyList = asList(
           new LocalPlayerGameStrategy(),
            new MultiPlayerGameStrategy()
    );

    public BuildGameStrategy chooseStrategy(GameDto gameDto) {
        return strategyList
                .stream()
                .filter(strategy -> strategy.applies(gameDto))
                .findFirst()
                .orElse(new ErrorGameTypeStrategy());
    }
}

Or, maybe someone has better idea, for StrategySelector logic?或者,对于 StrategySelector 逻辑,也许有人有更好的主意?

Here is class with given problem:这是给定问题的 class:

@Component
public class LocalPlayerGameStrategy implements BuildGameStrategy {
    @Autowired
    private GameService gameService;
    @Autowired
    private UserService userService;
    @Autowired
    private PlayerService playerService;

    @Override
    @Transactional
    public void buildGame(GameDto gameDto) {
        Game game = new GameBuilder(gameDto.getGameSize(), gameDto.getGameDifficulty()).build();
        gameService.save(game);

        User caller = userService.findByName(SecurityContextHolder.getContext().getAuthentication().getName());
        assert caller != null;

        for (PlayerDto playerDto : gameDto.getPlayers()) {
            Player player = new PlayerBuilder().setGame(game).setUser(caller).setPlayerDetails(playerDto).build();
            playerService.save(player);
        }

        game.setState(GameState.IN_PROGRESS);

        gameService.save(game);
    }


    @Override
    public boolean applies(GameDto gameDto) {
        return gameDto.getPlayers().stream().allMatch(p -> p.getPlayerType().equals(PlayerType.LOCAL.name()));
    }
}

I tried to autowire repositores and services with implemented logic as well.我也尝试使用已实现的逻辑自动装配存储库和服务。 Every time all of them:每次他们所有人:

@Autowired
    private GameService gameService;
    @Autowired
    private UserService userService;
    @Autowired
    private PlayerService playerService;

ARE NULL是 NULL

I tried everything I found on google我尝试了在谷歌上找到的一切

You can't create new instances of LocalPlayerGameStrategy and MultiPlayerGameStrategy inside GameStrategySelector they won't be spring beans so they can't @Autowire other beans.您不能在GameStrategySelector中创建LocalPlayerGameStrategyMultiPlayerGameStrategynew实例,它们不会是 spring bean,因此它们不能 @Autowire 其他 bean。

Modify your GameStrategySelector to inject all BuildGameStrategy instead of creating them manually修改您的GameStrategySelector以注入所有BuildGameStrategy而不是手动创建它们

@Service
public class GameStrategySelector {
    private final List<BuildGameStrategy> strategyList;
    
    public GameStrategySelector(List<BuildGameStrategy> allStrategies){
        strategyList = allStrategies;
    }

    public BuildGameStrategy chooseStrategy(GameDto gameDto) {
        return strategyList
                .stream()
                .filter(strategy -> strategy.applies(gameDto))
                .findFirst()
                .orElse(new ErrorGameTypeStrategy());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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