zoukankan      html  css  js  c++  java
  • spring 手动添加 bean 到容器,例子 :多数据源配置

    package com.thunisoft.spsjsb.config.db.decrypt;
    
    import com.alibaba.druid.pool.DruidDataSource;
    import com.thunisoft.spsjsb.config.AppEnv;
    import org.springframework.beans.factory.support.BeanDefinitionBuilder;
    import org.springframework.beans.factory.support.DefaultListableBeanFactory;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import javax.sql.DataSource;
    
    @Configuration
    public class MultiDsConf {
    
        @Bean
        public Integer doRun() {
            //获取BeanFactory
            DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) AppEnv.getCtx().getAutowireCapableBeanFactory();
    
            //创建bean信息.
            BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(DruidDataSource.class);
            beanDefinitionBuilder.addPropertyValue("driverClassName","com.mysql.jdbc.Driver");
            beanDefinitionBuilder.addPropertyValue("url","jdbc:mysql:///sp_tjpt_dev?useUnicode=true&characterEncoding=utf8&useSSL=false&generateSimpleParameterMetadata=true&useCursorFetch=true&defaultFetchSize=100");
            beanDefinitionBuilder.addPropertyValue("username","root");
            beanDefinitionBuilder.addPropertyValue("password","123456");
            defaultListableBeanFactory.registerBeanDefinition("sjsb", beanDefinitionBuilder.getBeanDefinition());
    
            BeanDefinitionBuilder beanDefinitionBuilder2 = BeanDefinitionBuilder.genericBeanDefinition(DruidDataSource.class);
            beanDefinitionBuilder2.addPropertyValue("driverClassName","net.sourceforge.jtds.jdbc.Driver");
            beanDefinitionBuilder2.addPropertyValue("url","jdbc:jtds:sybase:///YWST");
            beanDefinitionBuilder2.addPropertyValue("username","sa");
            beanDefinitionBuilder2.addPropertyValue("password","123456");
            defaultListableBeanFactory.registerBeanDefinition("np", beanDefinitionBuilder2.getBeanDefinition());
    
            return 1;
        }
    }
    

      

  • 相关阅读:
    访问双工服务
    为 Silverlight 客户端生成双工服务
    RMAN创建辅助实例(副本数据库)
    js获取对话框返回值
    在ascx中使用js找不到对象问题解决
    css滑动门技术[摘自网络]
    非禁用validateRequest=false使用Page_Error()错误处理[摘自网络]
    PreviousPage跨页面传值
    jQuery Ajax 方法调用 Asp.Net WebService 的详细例子[转]
    动态的创建客户端控件[收藏网络]
  • 原文地址:https://www.cnblogs.com/hfultrastrong/p/9983409.html
Copyright © 2011-2022 走看看