Spring—IOC控制反转(注解方式)
-
使用步骤:
-
在resources下新建一个bean.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"> <!--告知spring在创建容器时要扫描的包,配置所需要的标签不是在beans的约束中,而是在一个名称为context名称空间和约束中--> <context:component-scan base-package="com.itheima"></context:component-scan> </beans>
-
-
注解的分类
/** * 注解的分类 * * 用于创建对象的 * 他们的作用就和在XML文件中编写一个<bean></bean>标签实现的功能时一样的 * @Component:用于把当前类对象存入sprint容器中 * 属性: * value:用于指定bean的id,当我们不写时,他的默认值就是当前类名且首字母该小写 * @Controller:一般用在表现层 * @Service:一般用在业务层 * @Repository:一般用在持久层 * 以上三个注解的作用和属性和@Component时一模一样的 * 他们三个时spring框架为我们提供明确的三层使用的,使我们对三层对象更加清晰 * 用于注入数据的 * 他们的作用就和在XML文件中的bean标签中写一个<property></property>标签的作用时一样的 * @Autowired: * 作用:自动按照类型注入。只要容器中有唯一的一个bean对象类型和要注入的变量的类型匹配,那么就会注入成功 * 只要容器中有多个bean对象类型和要注入的变量的类型匹配,会先匹配ioc容器中bean的类型,再根据属性的变量名去匹配ioc容器id,如果匹配上,则注入成功 * 出现位置:可以时变量上,也可以时方法上 * 细节:在使用@Autowired注入时,set方法就不是必须的了 * @Qualifier: * 作用:在按照类中注入的基础之上再按照名称注入。它给类成员注入时不能单独使用(配合@Autowired),给方法参数注入时可以单独使用 * @Resource * 作用:直接按照bean的id注入,它可以单独使用(必给注解写属性,默认使用按照名称注入,之后再按照类型注入) * 属性:name 用于指定bean的id * ===========以上三个注入都只能注入其他bean类型的数据,而基本类型和String类型无法使用上述注解注入,集合类型只能通过XML方式实现============== * @Value: * 作用:用于注入基本类型数据和String类型数据 * 属性: * value:用于指定数据的值。它可以使用spring中的SpEL(也就是spring的el表达式 ${表达式}) * 用于改变作用范围的 * 他们的作用就和在bean标签中使用scope属性实现的功能时一样的 * @Scpoe: * 作用:用于指定bean的作用范围 * 属性: * value:指定范围的取值,常用取值:singleton prototype * 和生命周期相关的(了解) * 他们的作用就和在bean标签中使用init-method和destroy-method的作用时一样的 * @PreDestroy:用于指定销毁的方法 * @PostConstruct:用于指定初始化的方法 */
** * 该类是一个配置类,他的作用和bean.xml是一样的 * @Configuration * 作用:指当前类是一个配置类 * 细节:当配置类作为AnnotationConfigApplicationContext对象创建的参数时,该注解可以不写 * @ComponentScan * 作用:通过注解指定spring在创建容器时要扫描的包 * 指定创建容器时要扫描的包 * @Bean: * 作用:用于把当前方法的返回值作为bean对象存入spring的ioc容器中 * 属性: * name:用于指定bean的id,不写时默认值是当前方法的名称 * 细节: * 当我们使用注解配置方法时,如果方法有参数,那么spring回去容器中查找有没有可用的bean对象 * 查找的方式和Autowired注解时一样的 * @import:用于导入其他的配置类 * 属性:value 用于指定其他配置类的字节码 * 当我们使用import注解之后,有import注解的就是父配置类,导入的就是子配置类 * @PropertySource: * 作用:用于指定properties文件的位置 * 属性: * value:指定文件的路径和名称(前面加上classpath:) * */ @Configuration @ComponentScan(basePackages = "com.itheima") //指定创建容器时要扫描的包 //上面的与<context:component-scan base-package="com.itheima"></context:component-scan>作用一样 @PropertySource("classpath:jdbcConfig.properties") public class SpringConfiguration { @Value("${driver}") private String driver; @Value("${url}") private String url; @Value("${user}") private String username; @Value("${password}") private String password; @Bean(name = "runner") public QueryRunner createQUeryrUNNER(DataSource ds){ return new QueryRunner(ds); } /* * 创建数据源对象 * */ @Bean(name = "ds") public DataSource createDataSource(){ try { ComboPooledDataSource ds = new ComboPooledDataSource(); ds.setDriverClass(driver); ds.setJdbcUrl(url); ds.setUser(username); ds.setPassword(password); return ds; } catch (PropertyVetoException e) { throw new RuntimeException(e); } } }
配置文件:jdbcConfig.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/eesy user=root password=root
测试类
@Test public void testFindAll(){ // ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml"); //参数就是被@Configuration注解的类 ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class); AccountServiceImpl as = ac.getBean("accountService", AccountServiceImpl.class); List<Account> lists = as.findAllAccount(); for (Account list : lists) { System.out.println(list); } }
-
spring整合junit的配置
-
导入依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.2.RELEASE</version> </dependency>
-
使用junit提供的一个注解把原有的main方法替换了,替换成spring提供的(原有的junit执行的时候不会创建ioc容器)@Runwith
-
告知spring的运行器,spring的ioc创建是基于xml还是注解的,并说明位置
@ContextConfiguration
locations:指定xml文件的位置用来创建容器,加上classpath关键字,表示在类路径下
classes:指定注解类所在的位置
当我们使用spring5.x时,junit版本必须是4.12及以上
@RunWith(SpringJUnit4ClassRunner.class) /*SpringConfiguration就是自定义的配置类*/ @ContextConfiguration(classes = SpringConfiguration.class) public class AccountServiceTest { @Autowired private IAccountService as =null; @Test public void testFindAll(){ // ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml"); //参数就是被@Configuration注解的类 // ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class); // AccountServiceImpl as = ac.getBean("accountService", AccountServiceImpl.class); List<Account> lists = as.findAllAccount(); for (Account list : lists) { System.out.println(list); } }
-