3

I want to inject an object like Foo in some class. I have two bean for Foo. One of them is RequestScope and other is Prototype. I want to use @Conditional for this. Is there any annotation like ConditionalOnHttpRequestExist that separate these injection?

mohammad_1m2
  • 1,571
  • 5
  • 19
  • 38

2 Answers2

2

You can simulate the condition via providing any flag in properties file, like below sample:

    @EnableSwagger2
    @Configuration
    @ConditionalOnProperty(value = "myapi.enable.swagger", havingValue = "true")
    public class SwaggerConfig {

        @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build();
        }

    }

Here the Swagger will be configured only if the value of property "myapi.enable.swagger" is set to true otherwise not.

ConditionalOnProperty

Kumar Ashutosh
  • 1,121
  • 10
  • 33
  • Some work in my system is job-based and there isn't any request for this. I want to inject the prototype bean in job work and request scope for use request. I can't distinguished those situation by some properties. Because the property exist for those. – mohammad_1m2 Feb 27 '20 at 15:20
  • You might wanna look at this post : https://medium.com/@lofidewanto/creating-spring-bean-dynamically-in-the-runtime-d9e32c41d286 – Kumar Ashutosh Feb 27 '20 at 15:30
0

I wasn't able to achieve it with the conditional request scoped bean. The condition citeria was executed only once according to my experiments.

Analogical solution with AOP

@AutowiredCustom
public SpeedLimitService speedLimitService;

Aspect:

import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import performance.context.CountryHolder;

import java.lang.reflect.Field;

/**
 */
@Aspect
@Component
public aspect AutowiredCustomFieldAspect implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    pointcut annotatedField(): get(@performance.annotation.AutowiredCustom * *);

    before(Object object): annotatedField() && target(object) {
        try {
            String fieldName = thisJoinPoint.getSignature().getName();
            Field field = object.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);

            String className = field.getType().getSimpleName() + CountryHolder.getCountry().name();

            Object bean = applicationContext.getAutowireCapableBeanFactory().getBean(className);

            field.set(object, bean);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

}

Beans:

@Service("SpeedLimitServiceCH")
public class SpeedLimitServiceCH implements SpeedLimitService {

    @Override
    public int getHighwaySpeedLimit() {
        return 120;
    }
}

@Service("SpeedLimitServiceDE")
public class SpeedLimitServiceDE implements SpeedLimitService {

    @Override
    public int getHighwaySpeedLimit() {
        return 200;
    }
}
Viktor Reinok
  • 113
  • 13
  • refs: https://medium.com/@viktorreinok/dependency-injection-pattern-for-cleaner-business-logic-in-your-java-spring-application-f4ace0a3cba7 – Viktor Reinok Apr 29 '23 at 11:12