main/test 注释方式 连接数据库
main方式
使用AnnotationConfigApplicationContext 扫描注释
AnnotationConfigApplicationContext 扫描注释package club.cser.springroad.jdbc.client;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
public class JdbcTemplateAnnotationClient {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.scan("club.cser.springroad.jdbc.dao");
ctx.refresh();
//DataSource dataSource = ctx.getBean(DataSource.class);
//JdbcTemplate jdbcTemplate = (JdbcTemplate)ctx.getBean("jdbcTemplate");
}
}
Test方式
@Bean 创建三方库提供的类对象
JdbcTemplateBean.java 文件
JdbcTemplateBean.java 文件读取properties文件
test配置context
但是用注释的方式在junit里怎么获得context环境加载bean?
HelloDaoTest.java文件
HelloDaoTest.java文件包版本
Last updated