要想使用纯注解开发,还需了解以下几个注解
- @Configuration:相当于
,将此类作为xml配置文件applicationContext.xml
- @ComponentScan:相当于
开启包扫描<context:component-scan base-package=""/>
- @PropertySource:相当于
,导入外部配置文件<context:property-placeholder location=""/>
- @Bean:将第三方类(对象)交给ioc容器,让他去为需要的地方注入此对象
- @Import:相当于
引入另一个xml配置<import resource=""/>
Spring配置类
@Configuration // applicationContext.xml
@ComponentScan(value = "com.lepeng") // <context:component-scan base-package=""/>
@PropertySource("classpath:jdbc.properties")// <context:property-placeholder location=""/>
public class SpringConfig {
@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("dataSource")
public DataSource dataSource() {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driverClassName);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
return druidDataSource;
}
// 创建queryRunner(第三方对象)
@Bean("queryRunner")
public QueryRunner queryRunner(DataSource dataSource) { //方法形参位置,会自动从ioc容器获取对象实例,完成注入
QueryRunner queryRunner = new QueryRunner(dataSource);
return queryRunner;
}
}
这个类也就对应我们编写的xml配置文件:
<!--开启注解组件扫描-->
<context:component-scan base-package="com.lepeng"/>
<!--导入外部的properties配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--创建连接池-->
<bean id="dataSource" 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>
<!--创建queryRunner-->
<bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean>
实体类
public class Account {
private int id;
private String name;
private double money;
//自动生成set和get方法 toString方法
}
dao层
@Repository
public class AccountDaoImpl implements AccountDao {
//简单的jdbc封装对象,比mybatis框架更轻量级 //参数是连接池对象
//QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
//声明一个操作数据库的CRUD对象
@Autowired
private QueryRunner queryRunner;
public List<Account> findAll() throws SQLException {
return queryRunner.query("SELECT * FROM account", new BeanListHandler<Account>(Account.class));
}
}
service层
@Service
public class AccountServiceImpl implements AccountService {
//声明dao对象
@Autowired
private AccountDao accountDao;
public List<Account> findAll() throws SQLException {
return accountDao.findAll();
}
}
测试类
public class AccountTest {
@Test
public void test01() throws Exception {
// 加载配置文件,初始化spring环境(ioc容器)
// ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfig.class);
// 获得指定类型对象实例
AccountService accountService = app.getBean(AccountService.class);
// 测试
List<Account> list = accountService.findAll();
for (Account account : list) {
System.out.println(account);
}
}
}