配置数据源
导入依赖包
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
编写配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--加载外部文件properties-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" 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>
</beans>
编写jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
常用注解
组件扫描
<!--配置组件扫描 扫描zyh包下的所有-->
<context:component-scan base-package="com.zyh"/>
@Component ——实例化Bean
使用在类上用于实例化Bean
@Controller
使用在web层类上用于实例化Bean
@Service
使用在service层类上用于实例化Bean
@Repository
使用在dao层类上用于实例化Bean
@Autowired ——依赖注入
使用在字段上用于根据类型依赖注入
@Qualifier
结合@Autowired一起使用 用于根据名称进行依赖注入
@Resource ——依赖注入
相当于@Autowired+@Qualifier的结合,按照名称进行注入
@Value 注入普通属性
@Scope 标注Bean的作用范围
prototype 多例的
singleton 单例的
@PostConstruct ——初始化
使用在方法上标注该方法是Bean的初始化方法
@PreDestroy ——销毁
使用在方法上标注该方法是Bean的销毁方法
新注解——用于非自定义的Bean
@Configuration ——定义配置类
用于指定当前类是一个Spring配置类,当创建容器时会从该类上加载注解
ComponentScan ——指定扫描包
用于指定Spring在初始化容器时扫描的包。作用和在Spring的xml配置文件中的<context:component-scan base-package=“com.zyh”/>一样
@Bean
使用在方法上,标注将该方法的返回值存储到Spring容器中
@PropertySource
用于加载.properties文件的配置
@Import
用于导入其他配置类
代码实现
package com.zyh.cofig;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
//标志该类是spring的核心配置类
@Configuration
//<context:component-scan base-package="com.zyh"/>
@ComponentScan("com.zyh")
//<import resource="classpath:"/>
@Import({DataSourceConfiguration.class})
public class SpringCofiguration {
}
package com.zyh.cofig;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
//用于导入properties文件 <context:property-placeholder location="classpath:jdbc.properties"/>
@PropertySource("classpath:jdbc.properties")
public class DataSourceConfiguration {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String user;
@Value("${jdbc.password}")
private String password;
@Bean("dataSource")//spring会将当前方法的返回值以指定名称存储到spring容器中
public DataSource getDataSource() throws Exception {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(driver);
dataSource.setJdbcUrl(url);
dataSource.setUser(user);
dataSource.setPassword(password);
return dataSource;
}
}
public static void main(String[] args) {
// ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext app = new AnnotationConfigApplicationContext(SpringCofiguration.class);
UserService userService = app.getBean(UserService.class);
userService.save();
}
整合Junit
整合思路
- 让SpringJunit负责创建Spring容器,但是需要将配置文件的名称告诉他
- 将需要进行测试Bean直接在测试类中进行注入
集成步骤
- 导入spring集成Junit的坐标
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
- 使用@Runwith注解替换原来的运行期
- 使用ContextConfiguration指定配置文件或配置类
- 使用@Autowired注入需要测试的对象
- 创建测试方法进行测试
@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration("classpath:applicationContext.xml")
@ContextConfiguration(classes = {SpringCofiguration.class})
public class SpringJunitTest {
@Autowired
@Qualifier("userService")
private UserService userService;
@Test
public void test1(){
userService.save();
}
}