Java多重链表_说说我的spring入门5--ioc注解进阶

本文详细介绍了如何在Spring中使用注解进行配置,包括@Configuration、@ComponentScan和@Bean的使用,以及如何将数据库连接信息放入配置类。同时,文章还展示了如何进行单元测试,利用SpringJUnit4ClassRunner进行测试,自动注入业务层对象,简化测试代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文是在spring入门4的基础上,采用注解进行改进的。

基本配置

在bean.xml配置中,下面这条语句是指配置spring扫描注解的文件夹。

我们可以新建一个SpringConfiguration类,@Configuration指明这是一个配置类,与bean.xml等效。@ComponentScan(basePackages = {"com.itheima"})配置spring扫描注解的文件夹。

@Configuration

@ComponentScan(basePackages = {"com.itheima"})

public class SpringConfiguration {

@Bean(name="runner")

@Scope("prototype")

public QueryRunner createQueryRunner(DataSource dataSource){

return new QueryRunner(dataSource);

}

}

配置容器对象

下面的代码是bean.xml中把QueryRunner 放入容器中的实现方式,并且用constrouctor-arg为其配置构造方法注入dataSource。

上面SpringConfiguration类中的createQueryRunner方法通过接收一个dataSource参数返回QueryRunner对象,只要再把返回对象放到spring容器中即可。 @Bean可以实现Bean化。

事实上,上面需要接收一个dataSource参数,spring框架会去容器中查找该参数对应的bean对象,查找方式与AutoWried一样。因此我们需要把dataSource放入spring容器

@Bean(name="dataSource")

public DataSource createDataSource(){

try {

ComboPooledDataSource ds = new ComboPooledDataSource();

ds.setDriverClass("com.mysql.cj.jdbc.Driver");

ds.setJdbcUrl("jdbc:mysql://localhost:3306/myspring?serverTimezone=GMT%2B8");

ds.setUser("root");

ds.setPassword("52wendyma");

return ds;

}catch(Exception e){

throw new RuntimeException(e);

}

}

测试

注意此时需要用AnnotationConfigApplicationContext对象读取配置文件对象。

public void testFindAll() {

//ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");

ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);

IAccountService as = ac.getBean("accountService",IAccountService.class);

//3.执行方法

Listaccounts = as.findAllAccounts();

for(Account account : accounts){

System.out.println(account);

}

}

注意

1 Configuration可以不加

只要

ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class, JdbcConfig.class);

在此,我们把数据库连接信息多带带放到一个配置类。

public class JdbcConfig {

@Bean(name="dataSource")

public DataSource createDataSource(){

try {

ComboPooledDataSource ds = new ComboPooledDataSource();

ds.setDriverClass("com.mysql.cj.jdbc.Driver");

ds.setJdbcUrl("jdbc:mysql://localhost:3306/myspring?serverTimezone=GMT%2B8");

ds.setUser("root");

ds.setPassword("52wendyma");

return ds;

}catch(Exception e){

throw new RuntimeException(e);

}

}

2 import导入子配置类

@Configuration

@ComponentScan(basePackages = {"com.itheima"})

@Import(JdbcConfig.class)

public class SpringConfiguration {

@Bean(name="runner")

@Scope("prototype")

public QueryRunner createQueryRunner(DataSource dataSource){

return new QueryRunner(dataSource);

}

}

此时读取配置文件

ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);

把数据库信息提取成文件

这是对子配置类的改造。使用Value对定义的基本类型注入数据,数据源来自于jntl表达式${jdbc.driver}从配置文件jdbcConfig.properties获取的。

@Value("${jdbc.driver}")

private String driver;

@Value("${jdbc.url}")

private String url;

@Value("${jdbc.username}")

private String username;

@Value("${jdbc.password}")

private String password;

@Bean(name="dataSource")

public DataSource createDataSource() {

try {

ComboPooledDataSource ds = new ComboPooledDataSource();

ds.setDriverClass(driver);

ds.setJdbcUrl(url);

ds.setUser(username);

ds.setPassword(password);

return ds;

} catch (Exception e) {

throw new RuntimeException(e);

}

}

为了获取文件中的数据库连接信息,需要用@PropertySource配置类路径,它指明配置文件的位置。

@Configuration

@ComponentScan(basePackages = {"com.itheima"})

@Import(JdbcConfig.class)

@PropertySource("classpath:jdbcConfig.properties")//类路径

public class SpringConfiguration {

@Bean(name="runner")

@Scope("prototype")

public QueryRunner createQueryRunner(@Qualifier("ds1") DataSource dataSource){

return new QueryRunner(dataSource);

}

}

jdbcConfig.properties文件信息如下:

jdbc.driver=com.mysql.cj.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/myspring?serverTimezone=GMT%2B8

jdbc.username=root

jdbc.password=52wendyma

spring整合junit单元测试

Spring整合junit的配置

1、导入spring整合junit的jar(坐标)

2、使用Junit提供的一个注解把原有的main方法替换了,替换成spring提供的 @Runwith

3、告知spring的运行器,spring和ioc创建是基于xml还是注解的,并且说明位置

@ContextConfiguration

locations:指定xml文件的位置,加上classpath关键字,表示在类路径下

classes:指定注解类所在地位置

导包

首先需要导入spring-test这个jar包(导这个会出错java.lang.NoClassDefFoundError: org/springframework/core/annotation/MergedAnnotations)

org.springframework

spring-test

5.2.5.RELEASE

test

导入下面这个

org.springframework

spring-test

5.0.2.RELEASE

测试类

使用spring框架整合junit之后,测试的时候会自动注入业务层对象,无需再写重复的测试代码块(读取配置文件获得容器,从容器获取业务层对象)。可以直接进行单元测试,极大加快类开发速率。

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(classes = SpringConfiguration.class)

public class AccountServiceTest {

@Autowired

private IAccountService as = null;

@Test

public void testFindAll() {

Listaccounts = as.findAllAccounts();

for(Account account : accounts){

System.out.println(account);

}

}

@Test

public void testFindOne() {

Account account = as.findAccountById(1);

System.out.println(account);

}

@Test

public void testSave() {

Account account = new Account();

account.setName("test");

account.setMoney(12345f);

//3.执行方法

as.saveAccount(account);

}

@Test

public void testUpdate() {

//3.执行方法

Account account = as.findAccountById(4);

account.setMoney(23456f);

as.updateAccount(account);

}

@Test

public void testDelete() {

as.deleteAccount(4);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值