RootConfig类
Posted yujie666
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RootConfig类相关的知识,希望对你有一定的参考价值。
1 package com.ssm.yjblogs.config; 2 3 import java.util.Properties; 4 5 import javax.sql.DataSource; 6 7 import org.apache.commons.dbcp.BasicDataSourceFactory; 8 import org.mybatis.spring.SqlSessionFactoryBean; 9 import org.mybatis.spring.mapper.MapperScannerConfigurer; 10 import org.springframework.context.annotation.Bean; 11 import org.springframework.context.annotation.ComponentScan; 12 import org.springframework.context.annotation.ComponentScan.Filter; 13 import org.springframework.context.annotation.Configuration; 14 import org.springframework.context.annotation.FilterType; 15 import org.springframework.core.io.ClassPathResource; 16 import org.springframework.core.io.Resource; 17 //import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; 18 //import org.springframework.data.redis.core.RedisTemplate; 19 //import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; 20 //import org.springframework.data.redis.serializer.RedisSerializer; 21 //import org.springframework.data.redis.serializer.StringRedisSerializer; 22 import org.springframework.jdbc.datasource.DataSourceTransactionManager; 23 import org.springframework.stereotype.Repository; 24 import org.springframework.stereotype.Service; 25 import org.springframework.transaction.PlatformTransactionManager; 26 import org.springframework.transaction.annotation.EnableTransactionManagement; 27 import org.springframework.transaction.annotation.TransactionManagementConfigurer; 28 29 //import redis.clients.jedis.JedisPoolConfig; 30 31 @Configuration 32 //定义Spring 扫描的包 33 @ComponentScan(value= "com.*", includeFilters= {@Filter(type = FilterType.ANNOTATION, value ={Service.class})}) 34 //使用事务驱动管理器 35 @EnableTransactionManagement 36 //实现接口TransactionManagementConfigurer,这样可以配置注解驱动事务 37 public class RootConfig implements TransactionManagementConfigurer { 38 39 private DataSource dataSource = null; 40 41 /** 42 * 配置数据库. 43 * @return 数据连接池 44 */ 45 @Bean(name = "dataSource") //使用@Bean装配数据源 P245 46 public DataSource initDataSource() { 47 if (dataSource != null) { 48 return dataSource; 49 } 50 Properties props = new Properties(); 51 props.setProperty("driverClassName", "com.mysql.jdbc.Driver");//jdbc驱动 52 props.setProperty("url", "jdbc:mysql://localhost:3306/chapter22");//url 53 props.setProperty("username", "yujie");//数据库账号 54 props.setProperty("password", "123456");//数据库密码 55 props.setProperty("maxActive", "200");//最大连接数量 56 props.setProperty("maxIdle", "20");//最大的空闲连接数量 57 props.setProperty("maxWait", "30000");//最大的等待时间,单位是毫秒 58 try { 59 dataSource = BasicDataSourceFactory.createDataSource(props); 60 } catch (Exception e) { 61 e.printStackTrace(); 62 } 63 return dataSource; 64 } 65 66 /*** 67 * 配置SqlSessionFactoryBean 68 * @return SqlSessionFactoryBean 69 */ 70 @Bean(name="sqlSessionFactory") 71 public SqlSessionFactoryBean initSqlSessionFactory() { 72 SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean(); 73 sqlSessionFactory.setDataSource(initDataSource()); 74 //配置MyBatis配置文件 75 //src下的mybatis配置文件 76 Resource resource = new ClassPathResource("mybatis/mybatis-config.xml"); 77 sqlSessionFactory.setConfigLocation(resource); 78 return sqlSessionFactory; 79 } 80 81 /*** 82 * 通过自动扫描,发现MyBatis Mapper接口 83 * @return Mapper扫描器 84 */ 85 //P325 86 @Bean 87 public MapperScannerConfigurer initMapperScannerConfigurer() { 88 MapperScannerConfigurer msc = new MapperScannerConfigurer(); 89 //以com.开头的 90 msc.setBasePackage("com.*"); 91 //设置SqlSessionFactoryBean名字 92 msc.setSqlSessionFactoryBeanName("sqlSessionFactory"); 93 //把注解为@Repository的接口扫描为Mapper对象,存放在容器中,对于多个包的扫描可以用半角逗号分隔开来 94 //注解为@Repository的接口表示数据访问层(DAO, Data Access Object) 95 msc.setAnnotationClass(Repository.class); 96 return msc; 97 } 98 99 100 /** 101 * 实现接口方法,注册注解事务,当@Transactional 使用的时候产生数据库事务 102 */ 103 @Override 104 @Bean(name="annotationDrivenTransactionManager") 105 //P334 106 public PlatformTransactionManager annotationDrivenTransactionManager() { 107 DataSourceTransactionManager transactionManager = 108 new DataSourceTransactionManager(); 109 transactionManager.setDataSource(initDataSource()); 110 return transactionManager; 111 } 112 113 // @Bean(name = "redisTemplate") 114 // public RedisTemplate initRedisTemplate() { 115 // JedisPoolConfig poolConfig = new JedisPoolConfig(); 116 // //最大空闲数 117 // poolConfig.setMaxIdle(50); 118 // //最大连接数 119 // poolConfig.setMaxTotal(100); 120 // //最大等待毫秒数 121 // poolConfig.setMaxWaitMillis(20000); 122 // //创建Jedis链接工厂 123 // JedisConnectionFactory connectionFactory = new JedisConnectionFactory(poolConfig); 124 // connectionFactory.setHostName("localhost"); 125 // connectionFactory.setPort(6379); 126 // //调用后初始化方法,没有它将抛出异常 127 // connectionFactory.afterPropertiesSet(); 128 // //自定Redis序列化器 129 // RedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer(); 130 // RedisSerializer stringRedisSerializer = new StringRedisSerializer(); 131 // //定义RedisTemplate,并设置连接工程[修改为:工厂] 132 // RedisTemplate redisTemplate = new RedisTemplate(); 133 // redisTemplate.setConnectionFactory(connectionFactory); 134 // //设置序列化器 135 // redisTemplate.setDefaultSerializer(stringRedisSerializer); 136 // redisTemplate.setKeySerializer(stringRedisSerializer); 137 // redisTemplate.setValueSerializer(stringRedisSerializer); 138 // redisTemplate.setHashKeySerializer(stringRedisSerializer); 139 // redisTemplate.setHashValueSerializer(stringRedisSerializer); 140 // return redisTemplate; 141 // } 142 143 }
无行号版本:
package com.ssm.yjblogs.config; import java.util.Properties; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSourceFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.mapper.MapperScannerConfigurer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; //import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; //import org.springframework.data.redis.core.RedisTemplate; //import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; //import org.springframework.data.redis.serializer.RedisSerializer; //import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.TransactionManagementConfigurer; //import redis.clients.jedis.JedisPoolConfig; @Configuration //定义Spring 扫描的包 @ComponentScan(value= "com.*", includeFilters= {@Filter(type = FilterType.ANNOTATION, value ={Service.class})}) //使用事务驱动管理器 @EnableTransactionManagement //实现接口TransactionManagementConfigurer,这样可以配置注解驱动事务 public class RootConfig implements TransactionManagementConfigurer { private DataSource dataSource = null; /** * 配置数据库. * @return 数据连接池 */ @Bean(name = "dataSource") //使用@Bean装配数据源 P245 public DataSource initDataSource() { if (dataSource != null) { return dataSource; } Properties props = new Properties(); props.setProperty("driverClassName", "com.mysql.jdbc.Driver");//jdbc驱动 props.setProperty("url", "jdbc:mysql://localhost:3306/chapter22");//url props.setProperty("username", "yujie");//数据库账号 props.setProperty("password", "123456");//数据库密码 props.setProperty("maxActive", "200");//最大连接数量 props.setProperty("maxIdle", "20");//最大的空闲连接数量 props.setProperty("maxWait", "30000");//最大的等待时间,单位是毫秒 try { dataSource = BasicDataSourceFactory.createDataSource(props); } catch (Exception e) { e.printStackTrace(); } return dataSource; } /*** * 配置SqlSessionFactoryBean * @return SqlSessionFactoryBean */ @Bean(name="sqlSessionFactory") public SqlSessionFactoryBean initSqlSessionFactory() { SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean(); sqlSessionFactory.setDataSource(initDataSource()); //配置MyBatis配置文件 //src下的mybatis配置文件 Resource resource = new ClassPathResource("mybatis/mybatis-config.xml"); sqlSessionFactory.setConfigLocation(resource); return sqlSessionFactory; } /*** * 通过自动扫描,发现MyBatis Mapper接口 * @return Mapper扫描器 */ //P325 @Bean public MapperScannerConfigurer initMapperScannerConfigurer() { MapperScannerConfigurer msc = new MapperScannerConfigurer(); //以com.开头的 msc.setBasePackage("com.*"); //设置SqlSessionFactoryBean名字 msc.setSqlSessionFactoryBeanName("sqlSessionFactory"); //把注解为@Repository的接口扫描为Mapper对象,存放在容器中,对于多个包的扫描可以用半角逗号分隔开来 //注解为@Repository的接口表示数据访问层(DAO, Data Access Object) msc.setAnnotationClass(Repository.class); return msc; } /** * 实现接口方法,注册注解事务,当@Transactional 使用的时候产生数据库事务 */ @Override @Bean(name="annotationDrivenTransactionManager") //P334 public PlatformTransactionManager annotationDrivenTransactionManager() { DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(); transactionManager.setDataSource(initDataSource()); return transactionManager; } // @Bean(name = "redisTemplate") // public RedisTemplate initRedisTemplate() { // JedisPoolConfig poolConfig = new JedisPoolConfig(); // //最大空闲数 // poolConfig.setMaxIdle(50); // //最大连接数 // poolConfig.setMaxTotal(100); // //最大等待毫秒数 // poolConfig.setMaxWaitMillis(20000); // //创建Jedis链接工厂 // JedisConnectionFactory connectionFactory = new JedisConnectionFactory(poolConfig); // connectionFactory.setHostName("localhost"); // connectionFactory.setPort(6379); // //调用后初始化方法,没有它将抛出异常 // connectionFactory.afterPropertiesSet(); // //自定Redis序列化器 // RedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer(); // RedisSerializer stringRedisSerializer = new StringRedisSerializer(); // //定义RedisTemplate,并设置连接工程[修改为:工厂] // RedisTemplate redisTemplate = new RedisTemplate(); // redisTemplate.setConnectionFactory(connectionFactory); // //设置序列化器 // redisTemplate.setDefaultSerializer(stringRedisSerializer); // redisTemplate.setKeySerializer(stringRedisSerializer); // redisTemplate.setValueSerializer(stringRedisSerializer); // redisTemplate.setHashKeySerializer(stringRedisSerializer); // redisTemplate.setHashValueSerializer(stringRedisSerializer); // return redisTemplate; // } }
以上是关于RootConfig类的主要内容,如果未能解决你的问题,请参考以下文章
elasticsearch代码片段,及工具类SearchEsUtil.java
Android 逆向类加载器 ClassLoader ( 类加载器源码简介 | BaseDexClassLoader | DexClassLoader | PathClassLoader )(代码片段