spring动态生成bean

本文详细介绍了如何使用Spring Batch框架,在配置文件中读取properties动态生成lineTokenizerbean,并将其应用于launch-context.xml中指定的作业。通过加载配置文件和注册bean定义,实现了灵活的数据处理流程配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

读取properties 然后动态生成lineTokenizer bean.
        GenericApplicationContext context = new GenericApplicationContext();
        
        PropertySourcesPlaceholderConfigurer pph = new PropertySourcesPlaceholderConfigurer();
        pph.setLocation(new ClassPathResource("/batch.properties"));
        context.addBeanFactoryPostProcessor(pph);

        
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
        reader.loadBeanDefinitions("launch-context.xml");

        
        List<String> names = new ArrayList<String>();
        names.add("id");
        names.add("name");
        names.add("age");
        names.add("score");
        names.add("x");
        names.add("y");
        GenericBeanDefinition def = new GenericBeanDefinition();
        MutablePropertyValues vals = new MutablePropertyValues();
        def.setBeanClassName("org.springframework.batch.item.file.transform.DelimitedLineTokenizer");
        vals.add("delimiter", new TypedStringValue("^"));
        vals.add("names", names);
        def. setPropertyValues(vals);
        context. registerBeanDefinition("lineTokenizer", def);
        context.refresh();  
        
        
        
        JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");
        ImsParisStateUtil dataMartState = (ImsParisStateUtil) context
                .getBean("imsParisState");

        Job job = (Job) context.getBean("imsParisJob");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值