数据库配置文件
## db.properties
## 加上jdbc.前缀原因是 怕回头你还有些业务 需要使用 重名的属性名!!!
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_db
jdbc.username=root
jdbc.password=root
redis配置文件
redis.host=localhost
redis.port=6379
#最大空闲连接数
redis.maxIdle=30
redis.maxTotal=50
redis.minIdle=10
redis.maxWaitMillis=2000
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.18</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
beans.xml
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<bean id="datasource01" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="datasource02" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="datasource03" class="com.zaxxer.hikari.HikariDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<context:component-scan base-package="com.dao,com.service"></context:component-scan>
@Component
@Repository
public class ProductDaoImpl implements ProductDao{
@Autowired
private DataSource dataSource;
.....
}
@Component
@Service("abc")
public class ProductServiceImpl implements ProductService{.....}
context.getBean(ProductService.class);
使用配置类
DatasourceConfig.java
@PropertySource("classpath:db.properties")
public class DatasourceConfig {
@Value("${jdbc.driver}")
private String driverClassname;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource generateDatasource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClassname);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
Redis.java
@PropertySource("classpath:redis.properties")
public class RedisConfig {
@Value("${redis.host}")
private String host;
@Value("${redis.port}")
private int port;
@Value("${redis.maxIdle}")
private int maxIdle;
@Value("${redis.maxTotal}")
private int maxTotal;
@Value("${redis.minIdle}")
private int minIdle;
@Value("${redis.maxWaitMillis}")
private int maxWaitMillis;
@Bean
public GenericObjectPoolConfig createRedisConfig(){
GenericObjectPoolConfig config=new JedisPoolConfig();
config.setMaxIdle(maxIdle);
config.setMaxTotal(maxTotal);
config.setMinIdle(minIdle);
config.setMaxWaitMillis(maxWaitMillis);
return config;
}
@Bean
public JedisPool createJedisPool(GenericObjectPoolConfig config){
JedisPool pool = new JedisPool(config,host,port);
return pool;
}
}
springconfig.java
@Configuration
@ComponentScan({"com.itheima.dao","com.itheima.service","com.itheima.xyz"})
@Import({DatasourceConfig.class,RedisConfig.class})
public class SpringConfig {
}
测试的方法
方法一
public class ProductTest {
@Test
public void test01() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
ProductService productService = context.getBean(ProductService.class);
System.out.println("测试获取ProductService:"+productService);
}
}
方法二
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class UserTest {
@Autowired
private UserService userService;
@Test
public void test03(){
userService.login("zhangsan","123");
}
@Test
public void test04(){
userService.save(new User());
}