spring_IOC总结(三)注解配置--依赖注入
2021-02-27 本文已影响0人
Raral
基于注解依赖注入
- 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>
- 使用注解--依赖注入
package com.itheima.service.impl;
import com.itheima.dao.IAccountDao;
import com.itheima.dao.impl.AccountDaoImpl;
import com.itheima.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/**
* XML配置
* bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"
* scope="" init-method="" destory-method="">
* property name="" value="" ref=""> /property>
* /bean>
* 用于创建对象的
* 他们的作用就在和xml配置文件中编辑一个 bean 标签实现的功能是一样
* @Component:
* 作用: 用于把当前类对象存入spring容器中
* 属性:
* value: 用于指定bean的id。 当我们不写时候,首字母改小写。
* @Controller: 一般用于视图层
* @Service: 一般用在业务层
* @Repostitory: 一般用在持久层
*
*
* 用于注入数据的
* 他们的作用就和xml配置文件中bean标签里的 property 标签 功能一样
* @Autowired:
* 作用: 自动按照类型注入,只要容器中有唯一的一个bean对象类型和注入的变量类型匹配,就剋以额注入成功。
* 如果ioc容器bean类型和注入数据匹配不到类型,则会报错;
* 如果匹配多个,则先通过注入类型匹配,然后再通过 注入变量名称匹配,再找不到就报错。
* 出现位置:
* 可以是变量上,可以是方法上
* 细节:
* 在使用注解时,可以不用set方法
* @Qualifier:
* 作用:在按照类型注入基础之上再按照变量名称注入。不能单独使用,必须配合 @Autowired
* 属性:
* value: 用于指定注入bean的id。
* @Resource
* 作用:直接按照bean的id注入。可以单独使用
* 属性:
* name: 用于指定bean的id
*
注意: 以上注入都只能注入其他的bean类型的数据,而基本类型和String类型无法使用上述注解实现
另外集合类型注入只能通过xml来实现。
@Value
作用: 用于注入基本类型和String类型的数据
属性:
value: 用于指定数据的值。它可以使用spring的SpEl表达式 jsp/mybatis/spring es6
SpEl的写法: ${表达式}
一般获取spring配置文件的数据
* 用于改变作用域范围
* 他们的作用就和bean标签中 使用 scope属性作用一样
@Scope:
作用:用于指定bean的作用范围
属性:
value: 指定范围的取值。常用取值: sigleton prototype
* 生命周期相关的 (了解)
* 他们的作用就和bean标签使用init-method和destory-method作用是一样的
@PreDestory:
作用: 用于指定销毁方法
@PreInit:
作用: 用于指定初始化方法
*
* spring 新注解 ( 可以去掉xml文件)
* @Configuration
* 表示一个配置类
*@ComponentScan(“com.itheima”)
等同于 <context:component-scan base-package="com.itheima"></context:component-scan>
* @Bean
* 把一个方法的返回值存入到 spring容器中
* name:用于指定bean的id,当不写默认值当前方法名称
*
*细节:
如果使用xml,获取spring容器方法:
// 1. 获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2. 得到业务层对象
IAccountService as = ac.getBean("accountService",IAccountService.class);
//3. 执行方法
List<Account> accounts = as.findAllAccount();
for(Account account: accounts) {
System.out.println(account);
}
如果使用配置类,则获取spring容器要变
// 1. 获取容器
ApplicationContext ac = new AnnotationConfigApplicationContext(springConfiguration.class);
//2. 得到业务层对象
IAccountService as = ac.getBean("accountService",IAccountService.class);
//3. 执行方法
List<Account> accounts = as.findAllAccount();
for(Account account: accounts) {
System.out.println(account);
}
* */
@Service("accountService")
@Scope("prototype")
public class AccountServiceImpl implements IAccountService {
@Autowired
@Qualifier("accountDao")
private IAccountDao accountDao;
public AccountServiceImpl() {
System.out.println("构造函数创建");
}
public void saveAccount() {
accountDao.saveAccount();
}
@PostConstruct
public void init() {
System.out.println("初始化方法");
}
@PreDestroy
public void destory() {
System.out.println("销毁方法");
}
}