user2589193
user2589193

Reputation: 67

Spring boot app context and child context make as separate contexts

I want to use different child api modules in my application. My requirement is

  1. I want to use spring boot.
  2. Parent and child context should be separate from each other.
  3. I should able to use same name service in different contexts.
  4. I want to pass some objects from parent context to child context like DataSource.

What is the best way to achieve this goal.

Thanks.

OK. I tried with you suggested and I got System.out.println("Parent: " + count); print but I am not getting print System.out.println("Child: " + count);

What is the issue with this code?

EDITED

@Configuration
public class SpringBootChildApplication {


}


@Configuration
@EnableConfigurationProperties 
public class ChildDataConfig {

    @Autowired
    DataSource dataSource;

    @Bean
    JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}

@Service
public class ChildDataService {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @PostConstruct
    void executeSql() {
        String sql = "select 5 as cnt from dual";
        Object[] listParams = {};
        Integer count = jdbcTemplate.queryForObject(sql, listParams, new RowMapper<Integer>() {
            @Override
            public Integer mapRow(ResultSet rs, int number) throws SQLException {
                return rs.getInt("cnt");
            }
        });

        System.out.println("Child: " + count);
    }
}

@SpringBootApplication
public class SpringBootParentApplication {

    public static void main(String[] args) {
        // SpringApplication.run(SpringBootParentApplication.class, args);
        new SpringApplicationBuilder().bannerMode(Banner.Mode.OFF).sources(SpringBootParentApplication.class)
                .child(SpringBootChildApplication.class).run(args);
    }
}

@Service
class ParentDataService {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    ChildDataService childDataService;

    @PostConstruct
    void executeSql() {
        String sql = "select 5 as cnt from dual";
        Object[] listParams = {};
        Integer count = jdbcTemplate.queryForObject(sql, listParams, new RowMapper<Integer>() {
            @Override
            public Integer mapRow(ResultSet rs, int number) throws SQLException {
                return rs.getInt("cnt");
            }
        });

        System.out.println("Parent: " + count);

        childDataService.executeSql();
    }
}


@Configuration
@EnableConfigurationProperties 
public class ParentDataConfig {

    @ConfigurationProperties(prefix = "datasource.custom")
    @Bean
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}

Upvotes: 0

Views: 3496

Answers (1)

2.You can set child and parent context in your SpringBootApplication : new SpringApplicationBuilder() .bannerMode(Banner.Mode.OFF) .sources(Parent.class) .child(Application.class) .run(args); see https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-spring-application.html#boot-features-fluent-builder-api

3.Beans with same could work (didnt try), the parent will see only the beans created in the parent context. The child will see beans in its context, and if not found will look in the parent context. So if a bean with the same name exists in parent and child context, the parent will see the bean in the parent and the child in the child context.

4.Transafering beans in the child context is not necessary, other beans in the child context can see it, if its in the parent context.

I coulkd imagine there will be problems with everything depending on Postprocessors. E.g. @EnableJpaRepositories because they may lead to missing or double beans..

whether you see System.out.println("Parent: " + count) or System.out.println("Child: " + count) depends on where you call the service. If the call is issued by a bean in the parent context (thats what you seem to have) it will use the bean in the parent context. Called by a bean from the child context, the bean in the child context is used.

Upvotes: 2

Related Questions