示例来源本博客中上一个xml方式实现的单表操作基础上修改为注解开发
新建一个maven工程命名为:studentAnnoationIoc
将xml方式创建的代码尽数拷贝过来,做一定调整并新建替代的类,项目环境搭建完成后的结构如下:
需要修改的代码类如下:
1.StudentDaoImpl.java中添加红色框中的并删除相应set方法
2.StudentServiceImpl.java中的代码修改添加红色框中的并删除相应set方法
3.在resources中新建jdbcConfig.properties其中代码如下:
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring?serverTimezone=UTC
jdbc.username=root
jdbc.password=123456
4.config包中的SpringConfiguration.java和jdbcConfig.java中代码如下:
①:SpringConfiguration.java:
该类是一个配置类,他的作用和bean.xml是一样的
spring中的新注解:configuration 作用指定当前类是一个配置类
细节:当配置类作为AnnotationConfigApplicationContext对象创建的对象时,该注解可以不写。
ComponentScan 作用用于通过注解指定spring在创建容器时要扫描的包
value 作用和basePackages的作用是一样的,都是用于指定创建容器时要扫描的包
使用此注解等同于在xml中配置了
<context:component-scan base-package=“com.lut”></context:component-scan>
Bean 用于把当前方法的返回值作为bean对象存入spring的ioc容器中
属性:name用于指定bean的id,默认值是当前方法的名称
细节:当我们使用注解配置方法时,如果方法有参数,spring框架回去容器中查找有没有可用的bean对象,查找的方式和Autowired注解的作用是一样的
Import:用于导入其他的配置类
value:用于指定其他配置类的字节码,当我们使用Import注解的类就是父配置类,而导入的都是子配置类 PropertySource用于指定properties文件的位置 value指定文件的名称和路径关键字classpath表示类路径下。
package config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
@ComponentScan(basePackages = "com.lut")
@Import(jdbcConfig.class)
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfiguration {
}
②:jdbcConfig.java
package config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import javax.sql.DataSource;
public class jdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
//创建QueryRunner对象
@Bean(name="runner")
@Scope("prototype")
public QueryRunner createQueryRunner(@Qualifier("dataSource")DataSource dataSource){
return new QueryRunner(dataSource);
}
//创建数据源对象
@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);
}
}
}
5.创建测试类
使用junit单元测试
spring整合junit的配置
1.导入 spring整合junit的jar包
2.使用junit提供的注解把原有的main方法替换了,替换成spring提供的
@RunWith
3.告知spring运行器,spring的ioc创建是基于注解的还是xml的,并且注明位置
@ContextConfiguration()
locations:指定xml文件的位置,加上classpath关键字,表示在类路径下。
classes:指定注解类所在的位置
当我们使用spring5.x版本的使用,要求junit版本4.1.2以上
StudentServiceTest.java如下
package com.lut.test;
import com.lut.domain.Student;
import com.lut.service.IStudentService;
import config.SpringConfiguration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class StudentServiceTest {
@Autowired
private IStudentService studentService=null;
@Test
public void testFindAll(){
//3.执行方法
List<Student>students=studentService.findAllStudent();
for(Student student:students){
System.out.println(student);
}
}
@Test
public void testFindById(){
//3.执行方法
Student student=studentService.findStudentById(1);
System.out.println(student);
}
@Test
public void testSave(){
//3.执行方法
Student student=new Student();
student.setName("test");
student.setMajor("Science");
studentService.saveStudent(student);
}
@Test
public void testUpdate(){
//3.执行方法
Student student=new Student();
student.setId(2);
student.setName("update");
student.setMajor("Ph");
studentService.updateStudent(student);
}
@Test
public void testDelete(){
//3.执行方法
studentService.deleteStudent(1);
}
}
测试成功纯注解就实现了。