对应工程heima-annotation

前期准备

告知spring在创建容器时要扫描的包,配置所需要的标签不是在beans的约束中,而是一个名称为xmlns:context名称空间和约束中,官网查文档,点击Core,搜索关键字xmlns:context

并且使用context:component-scan标签,属性base-package指定要扫描的包,扫描包下代码中使用到的注解。

<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="cn.smm"></context:component-scan>

</beans>

1.创建对象

和在xml配置文件中编写一个标签实现的功能一样。

@Compoment :用于把当前类对象存入spring容器中。

属性:value:相当于bean的id。当我们不写的时候,默认值是当前类名,且首字母改小写

以下三个注解的作用和属性和component是一样的,作用是提供明确的三层使用的注解,使三层对象更加清晰。

  • @Controller :一般用在表现层
  • @Service :一般用于业务层
  • @Repository :一般用于持久层
//业务层
@Component(value = "accountService")
public class AccountServiceImpl implements IAccountService {
public AccountServiceImpl() {
System.out.println("对象被创建了");
}
//表现层
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService as = (IAccountService) ac.getBean("accountService");

2.注入数据

和在xml配置文件中的bean标签写个property标签的作用一样。

2.1@Autowired

自动按照类型注入。

  • 只要容器有唯一一个bean对象类型和要注入的变量类型匹配,没有则报错。如果匹配数据类型出多个,则匹配变量名称,否则报错。
  • 位置:可以是成员变量,也可以是方法
  • 细节:在使用注解注入时,set方法就不是必须的了
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao = null;

public void saveAccount() {
accountDao.saveAccount();
}
}

2.2@Qualifier

在按照类中注入的基础之上再按照名称注入,它在给类成员注入时不能单独使用。但是在给方法参数注入时可以。要配合Autowired使用。

  • 属性:value:用于指定注入bean的id
public class AccountServiceImpl implements IAccountService {
@Autowired
@Qualifier("accountDao1")
private IAccountDao accountDao1 = null;
private IAccountDao accountDao2 = null;

public void saveAccount() {
accountDao1.saveAccount();
}
}

2.3@Resource

直接按照bean的id注入,可独立使用

  • 属性:name :用于指定的bean的id
public class AccountServiceImpl implements IAccountService {
@Resource(name = "accountDao2")
private IAccountDao accountDao1 = null;
private IAccountDao accountDao2 = null;

public void saveAccount() {
accountDao1.saveAccount();
}
}

总结:以上三个注解只能注入其他bean类型的数据,而基本类型和String类型无法使用上述注解实现。另外,复杂/集合类型的注入只能通过XMLl来实现

2.4@Value

用于注入基本类型和String类型的数据

  • 属性:value:用于指定数据的值,可以用spring中的SpEL(就是spring的el表达式) SpEL的写法:${表达式}

需要搭配注解@PropertySource :用于指定properties文件的位置

  • 属性:value :指定文件的名称和路径,关键字:classpath,表示类路劲下的配置文件。
@PropertySource("classpath:JdbcConfig.properties")
public class JdbcConfig {
@Value("${driver}")
private String driver;
}

//配置文件
driver=com.mysql.jdbc.Driver

3.改变作用范围

作用集合在bean标签中使用scope属性实现的功能是一样的

3.1@Scope

用于指定bean的作用范围

  • 属性:value:指定范围的取值,常用取值:singleton(默认)和prototype
@Component(value = "accountService")
@Scope()
public class AccountServiceImpl implements IAccountService {}

4.生命周期(了解)

和在bean标签中使用init,destroy-method的作用一样

@PostConstruct :用于指定初始化方法

@PreDestroy :用于指定销毁方法

    @PostConstruct
public void init11() {
System.out.println("方法初始化了");
}

@PreDestroy
public void destroy11() {
System.out.println("方法被销毁了");
}