美文网首页
platform-base项目内容说明:

platform-base项目内容说明:

作者: 5163f7c9561e | 来源:发表于2017-07-03 17:55 被阅读53次
    image.png

    MyBatisConfig.java

    package com.delsk.base;
    
    import org.apache.ibatis.plugin.Interceptor;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.stereotype.Repository;
    
    import javax.sql.DataSource;
    import java.util.ArrayList;
    import java.util.List;
    
    
    /**
     * Created by zhoushengqiang on  17/6/30.
     */
    @Configuration
    @ConditionalOnClass(SqlSessionFactoryBean.class)
    @MapperScan(basePackages = "com.delsk", annotationClass = Repository.class,
            sqlSessionFactoryRef = "sqlSessionFactory")
    public class MyBatisConfig  {
    
            @Autowired(required = false)
            private List<Interceptor> interceptors = new ArrayList<>();
    
            @Bean(name = "sqlSessionFactory")
            public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
                SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
                ssfb.setDataSource(dataSource);
                ssfb.setMapperLocations((new PathMatchingResourcePatternResolver()).getResources("classpath*:mapper/*.xml"));
                if (!interceptors.isEmpty()) {
                    ssfb.setPlugins(interceptors.toArray(new Interceptor[interceptors.size()]));
                }
                SqlSessionFactory factory = ssfb.getObject();
                factory.getConfiguration().setMapUnderscoreToCamelCase(true);
                return factory;
            }
    
    }
    

    RedisConfig.java

    package com.delsk.base;
    
    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.cache.CacheManager;
    import org.springframework.cache.annotation.CachingConfigurerSupport;
    import org.springframework.cache.annotation.EnableCaching;
    import org.springframework.cache.interceptor.KeyGenerator;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.cache.RedisCacheManager;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    
    import java.lang.reflect.Method;
    
    /**
    * Created by zhoushengqiang on 17/6/30.
    */
    @Configuration
    @EnableCaching
    public class RedisConfig extends CachingConfigurerSupport {
    
       @Bean
       public KeyGenerator wiselyKeyGenerator() {
           return new KeyGenerator() {
               @Override
               public Object generate(Object target, Method method, Object... params) {
                   StringBuilder sb = new StringBuilder();
                   sb.append(target.getClass().getName());
                   sb.append(method.getName());
                   for (Object obj : params) {
                       sb.append(obj.toString());
                   }
                   return sb.toString();
               }
           };
    
       }
    
       @Bean
       public CacheManager cacheManager(@SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {
           return new RedisCacheManager(redisTemplate);
       }
    
       @Bean
       public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
           StringRedisTemplate template = new StringRedisTemplate(factory);
           Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
           ObjectMapper om = new ObjectMapper();
           om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
           om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
           jackson2JsonRedisSerializer.setObjectMapper(om);
           template.setValueSerializer(jackson2JsonRedisSerializer);
           template.afterPropertiesSet();
           return template;
       }
    }  
    

    DubboExporterConfiguration.java

    package com.delsk.base;
    
    import com.alibaba.dubbo.config.ApplicationConfig;
    import com.alibaba.dubbo.config.ProtocolConfig;
    import com.alibaba.dubbo.config.ProviderConfig;
    import com.alibaba.dubbo.config.RegistryConfig;
    import com.alibaba.dubbo.config.spring.AnnotationBean;
    import com.alibaba.dubbo.rpc.Exporter;
    import lombok.Data;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    
    /**
     * Created by zhoushengqiang on 17/6/30.
     */
    @Data
    @Configuration
    @ConditionalOnClass(Exporter.class)
    @PropertySource(value = "classpath:/dubbo.properties")
    public class DubboExporterConfiguration {
    
        @Value("${dubbo.application.name}")
        private String applicationName;
    
    
        @Value("${dubbo.registr.protocol}")
        private String protocol;
    
        @Value("${dubbo.registry.address}")
        private String registryAddress;
    
        @Value("${dubbo.protocol.name}")
        private String protocolName;
    
        @Value("${dubbo.protocol.port}")
        private int protocolPort;
    
        @Value("${dubbo.provider.timeout}")
        private int timeout;
    
        @Value("${dubbo.provider.retries}")
        private int retries;
    
        @Value("${dubbo.provider.delay}")
        private int delay;
    
    
        /**
         * 设置dubbo扫描包
         * @param packageName
         * @return
         */
        @Bean
        public static AnnotationBean annotationBean(@Value("${dubbo.annotation.package}") String packageName) {
            AnnotationBean annotationBean = new AnnotationBean();
            annotationBean.setPackage(packageName);
            return annotationBean;
        }
        /**
         * 注入dubbo上下文
         *
         * @return
         */
        @Bean
        public ApplicationConfig applicationConfig() {
            // 当前应用配置
            ApplicationConfig applicationConfig = new ApplicationConfig();
            applicationConfig.setName(this.applicationName);
            return applicationConfig;
        }
    
        /**
         * 注入dubbo注册中心配置,基于zookeeper
         *
         * @return
         */
        @Bean
        public RegistryConfig registryConfig() {
            // 连接注册中心配置
            RegistryConfig registry = new RegistryConfig();
            registry.setProtocol(protocol);
            registry.setAddress(registryAddress);
            return registry;
        }
    
        /**
         * 默认基于dubbo协议提供服务
         *
         * @return
         */
        @Bean
        public ProtocolConfig protocolConfig() {
            // 服务提供者协议配置
            ProtocolConfig protocolConfig = new ProtocolConfig();
            protocolConfig.setName(protocolName);
            protocolConfig.setPort(protocolPort);
            protocolConfig.setThreads(200);
            System.out.println("默认protocolConfig:" + protocolConfig.hashCode());
            return protocolConfig;
        }
    
        /**
         * dubbo服务提供
         *
         * @param applicationConfig
         * @param registryConfig
         * @param protocolConfig
         * @return
         */
        @Bean(name="myProvider")
        public ProviderConfig providerConfig(ApplicationConfig applicationConfig, RegistryConfig registryConfig, ProtocolConfig protocolConfig) {
            ProviderConfig providerConfig = new ProviderConfig();
            providerConfig.setTimeout(timeout);
            providerConfig.setRetries(retries);
            providerConfig.setDelay(delay);
            providerConfig.setApplication(applicationConfig);
            providerConfig.setRegistry(registryConfig);
            providerConfig.setProtocol(protocolConfig);
            return providerConfig;
        }
    
    }
    
    

    DubboExporterConfiguration.java

    package com.delsk.base;
    
    /**
     * Created by zhoushengqiang on 17/6/30.
     */
    import lombok.Data;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    
    import com.alibaba.dubbo.config.ApplicationConfig;
    import com.alibaba.dubbo.config.ConsumerConfig;
    import com.alibaba.dubbo.config.RegistryConfig;
    import com.alibaba.dubbo.config.spring.AnnotationBean;
    import com.alibaba.dubbo.rpc.Invoker;
    
    @Data
    @Configuration
    @ConditionalOnClass(Invoker.class)
    @PropertySource(value = "classpath:/dubbo.properties")
    public class DubboInvokerConfiguration {
    
        @Value("${dubbo.application.name}")
        private String applicationName;
    
    
        @Value("${dubbo.registr.protocol}")
        private String protocol;
    
        @Value("${dubbo.registry.address}")
        private String registryAddress;
    
        @Value("${dubbo.protocol.name}")
        private String protocolName;
    
        @Value("${dubbo.protocol.port}")
        private int protocolPort;
    
        @Value("${dubbo.provider.timeout}")
        private int timeout;
    
    
        /**
         * 设置dubbo扫描包
         *
         * @param packageName
         * @return
         */
        @Bean
        public static AnnotationBean annotationBean(@Value("${dubbo.annotation.package}") String packageName) {
            AnnotationBean annotationBean = new AnnotationBean();
            annotationBean.setPackage(packageName);
            System.out.println("哈哈哈哈-packageName:"+packageName);
            return annotationBean;
        }
    
        /**
         * 注入dubbo上下文
         *
         * @return
         */
        @Bean
        public ApplicationConfig applicationConfig() {
            // 当前应用配置
            ApplicationConfig applicationConfig = new ApplicationConfig();
            applicationConfig.setName(this.applicationName);
            return applicationConfig;
        }
    
        /**
         * 注入dubbo注册中心配置,基于zookeeper
         *
         * @return
         */
        @Bean
        public RegistryConfig registryConfig() {
            // 连接注册中心配置
            RegistryConfig registry = new RegistryConfig();
            registry.setProtocol(protocol);
            registry.setAddress(registryAddress);
            return registry;
        }
    
        /**
         * 默认基于dubbo协议提供服务
         *
         * @return
         */
    /*    @Bean
        public ProtocolConfig protocolConfig() {
            // 服务提供者协议配置
            ProtocolConfig protocolConfig = new ProtocolConfig();
            protocolConfig.setName(protocolName);
            protocolConfig.setPort(protocolPort);
            protocolConfig.setThreads(200);
            System.out.println("默认protocolConfig:" + protocolConfig.hashCode());
            return protocolConfig;
        }*/
    
        /**
         * dubbo消费
         *
         * @param applicationConfig
         * @param registryConfig
         * @return
         */
        @Bean(name = "defaultConsumer")
        public ConsumerConfig providerConfig(ApplicationConfig applicationConfig, RegistryConfig registryConfig) {
            ConsumerConfig providerConfig = new ConsumerConfig();
            return providerConfig;
        }
    }
    

    几个注解的说明:
    @Configuration:可理解为用spring的时候xml里面的<beans>标签
    @Bean:可理解为用spring的时候xml里面的<bean>标签
    @ConditionalOnClass:表示对应的类在classpath目录下存在时,才会去解析对应的配置文件,否则不解析该注解修饰的配置类;
    @MapperScan:注解去扫描所有的Mapper接口
    @PropertySource:主要是让Spring的Environment接口读取属性配置文件用的,这个注解是标识在@Configuration配置类上的。
    @EnableCaching:启用Cache注解支持

    相关文章

      网友评论

          本文标题:platform-base项目内容说明:

          本文链接:https://www.haomeiwen.com/subject/cklzcxtx.html