Search in sources :

Example 1 with RuntimeBeanReference

use of cn.taketoday.beans.factory.config.RuntimeBeanReference in project today-framework by TAKETODAY.

the class MapperScanTest method testScanWithExplicitSqlSessionTemplate.

@Test
void testScanWithExplicitSqlSessionTemplate() {
    BeanDefinition definition = new BeanDefinition();
    definition.setBeanClass(SqlSessionTemplate.class);
    ConstructorArgumentValues constructorArgs = new ConstructorArgumentValues();
    constructorArgs.addGenericArgumentValue(new RuntimeBeanReference("sqlSessionFactory"));
    definition.setConstructorArgumentValues(constructorArgs);
    applicationContext.registerBeanDefinition("sqlSessionTemplate", definition);
    applicationContext.register(AppConfigWithSqlSessionTemplate.class);
    startContext();
    // all interfaces with methods should be loaded
    applicationContext.getBean("mapperInterface");
    applicationContext.getBean("mapperSubinterface");
    applicationContext.getBean("mapperChildInterface");
    applicationContext.getBean("annotatedMapper");
}
Also used : BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition) RuntimeBeanReference(cn.taketoday.beans.factory.config.RuntimeBeanReference) ConstructorArgumentValues(cn.taketoday.beans.factory.config.ConstructorArgumentValues) Test(org.junit.jupiter.api.Test)

Example 2 with RuntimeBeanReference

use of cn.taketoday.beans.factory.config.RuntimeBeanReference in project today-framework by TAKETODAY.

the class MybatisAutoConfigurationTests method testAutoScanWithDefault.

@Test
void testAutoScanWithDefault() {
    this.context.register(EmbeddedDataSourceConfiguration.class, MybatisBootMapperScanAutoConfiguration.class, MybatisAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    this.context.refresh();
    SqlSessionFactory sqlSessionFactory = this.context.getBean(SqlSessionFactory.class);
    assertThat(this.context.getBeanNamesForType(SqlSessionFactory.class)).hasSize(1);
    assertThat(this.context.getBeanNamesForType(SqlSessionTemplate.class)).hasSize(1);
    assertThat(this.context.getBeanNamesForType(CityMapper.class)).hasSize(1);
    assertThat(this.context.getBean(SqlSessionTemplate.class).getExecutorType()).isEqualTo(ExecutorType.SIMPLE);
    assertThat(this.context.getBean(SqlSessionFactory.class).getConfiguration().isMapUnderscoreToCamelCase()).isFalse();
    this.context.getBean(CityMapper.class);
    assertThat(sqlSessionFactory.getConfiguration().getMapperRegistry().getMappers()).hasSize(1);
    assertThat(((RuntimeBeanReference) this.context.getBeanDefinition("cityMapper").getPropertyValues().getPropertyValue("sqlSessionTemplate")).getBeanName()).isEqualTo("sqlSessionTemplate");
    assertThat(context.getBeanDefinition(CollectionUtils.firstElement(context.getBeanNamesForType(MapperScannerConfigurer.class))).getRole()).isEqualTo(BeanDefinition.ROLE_INFRASTRUCTURE);
}
Also used : SqlSessionTemplate(cn.taketoday.orm.mybatis.SqlSessionTemplate) DefaultSqlSessionFactory(org.apache.ibatis.session.defaults.DefaultSqlSessionFactory) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) RuntimeBeanReference(cn.taketoday.beans.factory.config.RuntimeBeanReference) MapperScannerConfigurer(cn.taketoday.orm.mybatis.mapper.MapperScannerConfigurer) Test(org.junit.jupiter.api.Test)

Example 3 with RuntimeBeanReference

use of cn.taketoday.beans.factory.config.RuntimeBeanReference in project today-framework by TAKETODAY.

the class ScheduledTasksBeanDefinitionParser method doParse.

@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
    // lazy scheduled tasks are a contradiction in terms -> force to false
    builder.setLazyInit(false);
    ManagedList<RuntimeBeanReference> cronTaskList = new ManagedList<>();
    ManagedList<RuntimeBeanReference> fixedDelayTaskList = new ManagedList<>();
    ManagedList<RuntimeBeanReference> fixedRateTaskList = new ManagedList<>();
    ManagedList<RuntimeBeanReference> triggerTaskList = new ManagedList<>();
    NodeList childNodes = element.getChildNodes();
    int length = childNodes.getLength();
    for (int i = 0; i < length; i++) {
        Node child = childNodes.item(i);
        if (!isScheduledElement(child, parserContext)) {
            continue;
        }
        Element taskElement = (Element) child;
        String ref = taskElement.getAttribute("ref");
        String method = taskElement.getAttribute("method");
        // Check that 'ref' and 'method' are specified
        if (!StringUtils.hasText(ref) || !StringUtils.hasText(method)) {
            parserContext.getReaderContext().error("Both 'ref' and 'method' are required", taskElement);
            // Continue with the possible next task element
            continue;
        }
        String cronAttribute = taskElement.getAttribute("cron");
        String triggerAttribute = taskElement.getAttribute("trigger");
        String fixedRateAttribute = taskElement.getAttribute("fixed-rate");
        String fixedDelayAttribute = taskElement.getAttribute("fixed-delay");
        String initialDelayAttribute = taskElement.getAttribute("initial-delay");
        boolean hasCronAttribute = StringUtils.hasText(cronAttribute);
        boolean hasTriggerAttribute = StringUtils.hasText(triggerAttribute);
        boolean hasFixedRateAttribute = StringUtils.hasText(fixedRateAttribute);
        boolean hasFixedDelayAttribute = StringUtils.hasText(fixedDelayAttribute);
        boolean hasInitialDelayAttribute = StringUtils.hasText(initialDelayAttribute);
        if (!(hasCronAttribute || hasFixedDelayAttribute || hasFixedRateAttribute || hasTriggerAttribute)) {
            parserContext.getReaderContext().error("one of the 'cron', 'fixed-delay', 'fixed-rate', or 'trigger' attributes is required", taskElement);
            // with the possible next task element
            continue;
        }
        if (hasInitialDelayAttribute && (hasCronAttribute || hasTriggerAttribute)) {
            parserContext.getReaderContext().error("the 'initial-delay' attribute may not be used with cron and trigger tasks", taskElement);
            // with the possible next task element
            continue;
        }
        String runnableName = runnableReference(ref, method, taskElement, parserContext).getBeanName();
        if (hasFixedDelayAttribute) {
            fixedDelayTaskList.add(intervalTaskReference(runnableName, initialDelayAttribute, fixedDelayAttribute, taskElement, parserContext));
        }
        if (hasFixedRateAttribute) {
            fixedRateTaskList.add(intervalTaskReference(runnableName, initialDelayAttribute, fixedRateAttribute, taskElement, parserContext));
        }
        if (hasCronAttribute) {
            cronTaskList.add(cronTaskReference(runnableName, cronAttribute, taskElement, parserContext));
        }
        if (hasTriggerAttribute) {
            String triggerName = new RuntimeBeanReference(triggerAttribute).getBeanName();
            triggerTaskList.add(triggerTaskReference(runnableName, triggerName, taskElement, parserContext));
        }
    }
    String schedulerRef = element.getAttribute("scheduler");
    if (StringUtils.hasText(schedulerRef)) {
        builder.addPropertyReference("taskScheduler", schedulerRef);
    }
    builder.addPropertyValue("cronTasksList", cronTaskList);
    builder.addPropertyValue("triggerTasksList", triggerTaskList);
    builder.addPropertyValue("fixedRateTasksList", fixedRateTaskList);
    builder.addPropertyValue("fixedDelayTasksList", fixedDelayTaskList);
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ManagedList(cn.taketoday.beans.factory.support.ManagedList) RuntimeBeanReference(cn.taketoday.beans.factory.config.RuntimeBeanReference)

Example 4 with RuntimeBeanReference

use of cn.taketoday.beans.factory.config.RuntimeBeanReference in project today-framework by TAKETODAY.

the class ScheduledTasksBeanDefinitionParser method beanReference.

private RuntimeBeanReference beanReference(Element taskElement, ParserContext parserContext, BeanDefinitionBuilder builder) {
    // Extract the source of the current task
    builder.getRawBeanDefinition().setSource(parserContext.extractSource(taskElement));
    String generatedName = parserContext.getReaderContext().generateBeanName(builder.getRawBeanDefinition());
    parserContext.registerBeanComponent(new BeanComponentDefinition(builder.getBeanDefinition(), generatedName));
    return new RuntimeBeanReference(generatedName);
}
Also used : BeanComponentDefinition(cn.taketoday.beans.factory.parsing.BeanComponentDefinition) RuntimeBeanReference(cn.taketoday.beans.factory.config.RuntimeBeanReference)

Example 5 with RuntimeBeanReference

use of cn.taketoday.beans.factory.config.RuntimeBeanReference in project today-framework by TAKETODAY.

the class PropertyResourceConfigurerTests method doTestPropertyPlaceholderConfigurer.

private void doTestPropertyPlaceholderConfigurer(boolean parentChildSeparation) {
    Map<String, String> singletonMap = Collections.singletonMap("myKey", "myValue");
    if (parentChildSeparation) {
        PropertyValues pvs1 = new PropertyValues();
        pvs1.add("age", "${age}");
        PropertyValues pvs2 = new PropertyValues();
        pvs2.add("name", "name${var}${var}${");
        pvs2.add("spouse", new RuntimeBeanReference("${ref}"));
        pvs2.add("someMap", singletonMap);
        RootBeanDefinition parent = new RootBeanDefinition(TestBean.class);
        parent.setPropertyValues(pvs1);
        ChildBeanDefinition bd = new ChildBeanDefinition("${parent}", pvs2);
        factory.registerBeanDefinition("parent1", parent);
        factory.registerBeanDefinition("tb1", bd);
    } else {
        PropertyValues pvs = new PropertyValues();
        pvs.add("age", "${age}");
        pvs.add("name", "name${var}${var}${");
        pvs.add("spouse", new RuntimeBeanReference("${ref}"));
        pvs.add("someMap", singletonMap);
        RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
        bd.setPropertyValues(pvs);
        factory.registerBeanDefinition("tb1", bd);
    }
    ConstructorArgumentValues cas = new ConstructorArgumentValues();
    cas.addIndexedArgumentValue(1, "${age}");
    cas.addGenericArgumentValue("${var}name${age}");
    PropertyValues pvs = new PropertyValues();
    pvs.add("stringArray", new String[] { "${os.name}", "${age}" });
    List<Object> friends = ManagedList.of("na${age}me", new RuntimeBeanReference("${ref}"));
    pvs.add("friends", friends);
    Set<Object> someSet = ManagedSet.of("na${age}me", new RuntimeBeanReference("${ref}"), new TypedStringValue("${age}", Integer.class));
    pvs.add("someSet", someSet);
    Map<Object, Object> someMap = ManagedMap.ofEntries(Map.entry(new TypedStringValue("key${age}"), new TypedStringValue("${age}")), Map.entry(new TypedStringValue("key${age}ref"), new RuntimeBeanReference("${ref}")), Map.entry("key1", new RuntimeBeanReference("${ref}")), Map.entry("key2", "${age}name"));
    PropertyValues innerPvs = new PropertyValues();
    innerPvs.add("country", "${os.name}");
    RootBeanDefinition innerBd = new RootBeanDefinition(TestBean.class);
    innerBd.setPropertyValues(innerPvs);
    someMap.put("key3", innerBd);
    PropertyValues innerPvs2 = new PropertyValues(innerPvs);
    someMap.put("${key4}", new BeanDefinitionHolder(new ChildBeanDefinition("tb1", innerPvs2), "child"));
    pvs.add("someMap", someMap);
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, cas, pvs);
    factory.registerBeanDefinition("tb2", bd);
    PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
    Properties props = new Properties();
    props.setProperty("age", "98");
    props.setProperty("var", "${m}var");
    props.setProperty("ref", "tb2");
    props.setProperty("m", "my");
    props.setProperty("key4", "mykey4");
    props.setProperty("parent", "parent1");
    ppc.setProperties(props);
    ppc.postProcessBeanFactory(factory);
    TestBean tb1 = (TestBean) factory.getBean("tb1");
    TestBean tb2 = (TestBean) factory.getBean("tb2");
    assertThat(tb1.getAge()).isEqualTo(98);
    assertThat(tb2.getAge()).isEqualTo(98);
    assertThat(tb1.getName()).isEqualTo("namemyvarmyvar${");
    assertThat(tb2.getName()).isEqualTo("myvarname98");
    assertThat(tb1.getSpouse()).isEqualTo(tb2);
    assertThat(tb1.getSomeMap().size()).isEqualTo(1);
    assertThat(tb1.getSomeMap().get("myKey")).isEqualTo("myValue");
    assertThat(tb2.getStringArray().length).isEqualTo(2);
    assertThat(tb2.getStringArray()[0]).isEqualTo(System.getProperty("os.name"));
    assertThat(tb2.getStringArray()[1]).isEqualTo("98");
    assertThat(tb2.getFriends().size()).isEqualTo(2);
    assertThat(tb2.getFriends().iterator().next()).isEqualTo("na98me");
    assertThat(tb2.getFriends().toArray()[1]).isEqualTo(tb2);
    assertThat(tb2.getSomeSet().size()).isEqualTo(3);
    assertThat(tb2.getSomeSet().contains("na98me")).isTrue();
    assertThat(tb2.getSomeSet().contains(tb2)).isTrue();
    assertThat(tb2.getSomeSet().contains(98)).isTrue();
    assertThat(tb2.getSomeMap().size()).isEqualTo(6);
    assertThat(tb2.getSomeMap().get("key98")).isEqualTo("98");
    assertThat(tb2.getSomeMap().get("key98ref")).isEqualTo(tb2);
    assertThat(tb2.getSomeMap().get("key1")).isEqualTo(tb2);
    assertThat(tb2.getSomeMap().get("key2")).isEqualTo("98name");
    TestBean inner1 = (TestBean) tb2.getSomeMap().get("key3");
    TestBean inner2 = (TestBean) tb2.getSomeMap().get("mykey4");
    assertThat(inner1.getAge()).isEqualTo(0);
    assertThat(inner1.getName()).isNull();
    assertThat(inner1.getCountry()).isEqualTo(System.getProperty("os.name"));
    assertThat(inner2.getAge()).isEqualTo(98);
    assertThat(inner2.getName()).isEqualTo("namemyvarmyvar${");
    assertThat(inner2.getCountry()).isEqualTo(System.getProperty("os.name"));
}
Also used : PropertyPlaceholderConfigurer(cn.taketoday.beans.factory.config.PropertyPlaceholderConfigurer) PropertyValues(cn.taketoday.beans.PropertyValues) ChildBeanDefinition(cn.taketoday.beans.factory.support.ChildBeanDefinition) Properties(java.util.Properties) ConstructorArgumentValues(cn.taketoday.beans.factory.config.ConstructorArgumentValues) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) BeanDefinitionHolder(cn.taketoday.beans.factory.config.BeanDefinitionHolder) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) TypedStringValue(cn.taketoday.beans.factory.config.TypedStringValue) RuntimeBeanReference(cn.taketoday.beans.factory.config.RuntimeBeanReference)

Aggregations

RuntimeBeanReference (cn.taketoday.beans.factory.config.RuntimeBeanReference)12 Test (org.junit.jupiter.api.Test)7 RootBeanDefinition (cn.taketoday.beans.factory.support.RootBeanDefinition)5 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)5 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)4 ConstructorArgumentValues (cn.taketoday.beans.factory.config.ConstructorArgumentValues)3 PropertyValues (cn.taketoday.beans.PropertyValues)1 BeanDefinition (cn.taketoday.beans.factory.config.BeanDefinition)1 BeanDefinitionHolder (cn.taketoday.beans.factory.config.BeanDefinitionHolder)1 PropertyPlaceholderConfigurer (cn.taketoday.beans.factory.config.PropertyPlaceholderConfigurer)1 TypedStringValue (cn.taketoday.beans.factory.config.TypedStringValue)1 BeanComponentDefinition (cn.taketoday.beans.factory.parsing.BeanComponentDefinition)1 BeanDefinitionDefaults (cn.taketoday.beans.factory.support.BeanDefinitionDefaults)1 ChildBeanDefinition (cn.taketoday.beans.factory.support.ChildBeanDefinition)1 GenericBeanDefinition (cn.taketoday.beans.factory.support.GenericBeanDefinition)1 ManagedList (cn.taketoday.beans.factory.support.ManagedList)1 IndexedTestBean (cn.taketoday.beans.testfixture.beans.IndexedTestBean)1 Nullable (cn.taketoday.lang.Nullable)1 SqlSessionTemplate (cn.taketoday.orm.mybatis.SqlSessionTemplate)1 MapperScannerConfigurer (cn.taketoday.orm.mybatis.mapper.MapperScannerConfigurer)1