0
点赞
收藏
分享

微信扫一扫

【旧】G003Spring学习笔记-IOC之注解方式实现


重点知识点总结:

2020年3月18日14:19:13

1、曾经的XML配置方式

<bean id="accountService" class="com.zibo.services.impl.IAccountServiceImpl"
scope="" init-method="" destroy-method="">
<property name="" value="" / ref=""/>
</bean>

 

2、注解的分类

用于创建对象的注解:

它们和在xml中编写<bean/>标签实现的功能是一样的;

Component:用于把当前类作为存入spring容器中;

属性:value用于指定bean的id,当我们不写时,默认为当前类名,首字母小写;

Controller:一般用在表现层;

Service:一般用在业务层;

Repository:一般用在持久层;

上面三个注解跟Component的作用是一模一样的,它们三个是spring框架为三层提供的明确注解,使三层对象看起来更清晰;

用于注入数据的注解:

它们和在xml中的<bean/>标签中写一个<property/>的作用是一样的;

用于改变作用范围的注解:

它们和在xml中使用scope属性实现的功能是一样的;

和生命周期有关的注解:

它们和在xml中使用init-method和destroy-method属性实现的功能是一样的;

 

一、注解方式说明及入门示例

1、说明

/**
* 账户的业务层实现类
* 曾经的xml配置:
* <bean id="accountService" class="com.zibo.services.impl.IAccountServiceImpl"
* scope="" init-method="" destroy-method="">
* <property name="" value="" / ref=""/>
* </bean>
* 用于创建对象的;它们和在xml中编写<bean/>标签实现的功能是一样的;
* Component:用于把当前类作为存入spring容器中;
* 属性:value用于指定bean的id,当我们不写时,默认为当前类名,首字母小写;
* Controller:一般用在表现层;
* Service:一般用在业务层;
* Repository:一般用在持久层;
* 上面三个注解跟Component的作用是一模一样的,
* 它们三个是spring框架为三层提供的明确注解,使三层对象看起来更清晰;
* 用于注入数据的;它们和在xml中的<bean/>标签中写一个<property/>的作用是一样的;
* 用于改变作用范围的;它们和在xml中使用scope属性实现的功能是一样的;
* 和生命周期相关的;它们和在xml中使用init-method和destroy-method属性实现的功能是一样的;
*/

 

2、入门示例

代码:

接口IAccountService:

package com.zibo.services;

/**
* 业务层接口
*/
public interface IAccountService {
/**
* 模拟保存账户
*/
void saveAccount();
}

接口实现类AccountServiceImpl:

package com.zibo.services.impl;

import com.zibo.dao.IAccountDao;
import com.zibo.dao.impl.AccountDaoImpl;
import com.zibo.services.IAccountService;
import org.springframework.stereotype.Component;

/**
* 账户的业务层实现类
* 曾经的xml配置:
* <bean id="accountService" class="com.zibo.services.impl.IAccountServiceImpl"
* scope="" init-method="" destroy-method="">
* <property name="" value="" / ref=""/>
* </bean>
* 用于创建对象的;它们和在xml中编写<bean/>标签实现的功能是一样的;
* Component:用于把当前类作为存入spring容器中;
* 属性:value用于指定bean的id,当我们不写时,默认为当前类名,首字母小写;
* Controller:一般用在表现层;
* Service:一般用在业务层;
* Repository:一般用在持久层;
* 上面三个注解跟Component的作用是一模一样的,
* 它们三个是spring框架为三层提供的明确注解,使三层对象看起来更清晰;
* 用于注入数据的;它们和在xml中的<bean/>标签中写一个<property/>的作用是一样的;
* 用于改变作用范围的;它们和在xml中使用scope属性实现的功能是一样的;
* 和生命周期相关的;它们和在xml中使用init-method和destroy-method属性实现的功能是一样的;
*/
@Component(value = "accountService")//如果注解中只有一个value,value是乐意省略的
public class AccountServiceImpl implements IAccountService {
private IAccountDao iAccountDao = new AccountDaoImpl();
@Override
public void saveAccount() {
iAccountDao.saveAccount();
}
}

Client类:

package com.zibo.ui;

import com.zibo.services.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* 模拟表现层,调用业务层
*/
public class Client {
public static void main(String[] args) {
/*
* 获取spring的IOC的核心容器,并根据id获取对象
* ApplicationContext的三个常用实现类:
* 1、ClassPathXmlApplicationContext(xml方式最常用):仅能加载类路径下的配置文件;
* 2、FileSystemApplicationContext:可以加载磁盘任意路径下的配置文件(必须有访问权限);
* 3、AnnotationConfigApplicationContext:用于读取注解创建容器;
*/
//1、获取核心容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2、根据id获取bean对象
IAccountService service = ac.getBean("accountService",IAccountService.class);
System.out.println(service);
}
}

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.zibo"/>
</beans>

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>spring04</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>

</project>

文件位置图:

【旧】G003Spring学习笔记-IOC之注解方式实现_ioc

运行结果:

【旧】G003Spring学习笔记-IOC之注解方式实现_xml_02

 

二、深入注解方式

1、自动按照类型注入

说明:

* 用于注入数据的;它们和在xml中的<bean/>标签中写一个<property/>的作用是一样的;
* Autowired:
* 作用:自动按照类型注入,只要容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功;
* 补充:
* 如果没有则注入失败;
* 如果有多个则在按照类型匹配之后再按照key匹配,匹配到与变量名相同的bean则成功;
* 出现位置:成员变量上、方法上;
* 细节:在使用注解时,set方法就不是必须的了;

代码示例:

(部分代码,其他代码见上面)

接口IAccountDao:

package com.zibo.dao;

public interface IAccountDao {
void saveAccount();
}

接口实现类AccountDaoImpl:

package com.zibo.dao.impl;

import com.zibo.dao.IAccountDao;
import org.springframework.stereotype.Component;

@Component("accountDao")
public class AccountDaoImpl implements IAccountDao {
@Override
public void saveAccount() {
System.out.println("保存了账户111");
}
}

接口实现类AccountDaoImpl2:

package com.zibo.dao.impl;

import com.zibo.dao.IAccountDao;
import org.springframework.stereotype.Component;

@Component("accountDao2")
public class AccountDaoImpl2 implements IAccountDao {
@Override
public void saveAccount() {
System.out.println("保存了账户222");
}
}

接口实现类AccountServiceImpl:

package com.zibo.services.impl;

import com.zibo.dao.IAccountDao;
import com.zibo.dao.impl.AccountDaoImpl;
import com.zibo.services.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
* 账户的业务层实现类
* 曾经的xml配置:
* <bean id="accountService" class="com.zibo.services.impl.IAccountServiceImpl"
* scope="" init-method="" destroy-method="">
* <property name="" value="" / ref=""/>
* </bean>
* 用于创建对象的;它们和在xml中编写<bean/>标签实现的功能是一样的;
* Component:用于把当前类作为存入spring容器中;
* 属性:value用于指定bean的id,当我们不写时,默认为当前类名,首字母小写;
* Controller:一般用在表现层;
* Service:一般用在业务层;
* Repository:一般用在持久层;
* 上面三个注解跟Component的作用是一模一样的,
* 它们三个是spring框架为三层提供的明确注解,使三层对象看起来更清晰;
* 用于注入数据的;它们和在xml中的<bean/>标签中写一个<property/>的作用是一样的;
* Autowired:
* 作用:自动按照类型注入,只要容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功;
* 补充:
* 如果没有则注入失败;
* 如果有多个则在按照类型匹配之后再按照key匹配,匹配到与变量名相同的bean则成功;
* 出现位置:成员变量上、方法上;
* 细节:在使用注解时,set方法就不是必须的了;
* 用于改变作用范围的;它们和在xml中使用scope属性实现的功能是一样的;
* 和生命周期相关的;它们和在xml中使用init-method和destroy-method属性实现的功能是一样的;
*/
@Component("accountService")//如果注解中只有一个value,value是乐意省略的
public class AccountServiceImpl implements IAccountService {
@Autowired//不推荐的方法
//变量名如果是accountDao则匹配到AccountDaoImpl,如果是accountDao2则匹配到AccountDaoImpl2
private IAccountDao accountDao2;

@Override
public void saveAccount() {
accountDao2.saveAccount();
}
}

Client类:

package com.zibo.ui;

import com.zibo.services.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* 模拟表现层,调用业务层
*/
public class Client {
public static void main(String[] args) {
/*
* 获取spring的IOC的核心容器,并根据id获取对象
* ApplicationContext的三个常用实现类:
* 1、ClassPathXmlApplicationContext(xml方式最常用):仅能加载类路径下的配置文件;
* 2、FileSystemApplicationContext:可以加载磁盘任意路径下的配置文件(必须有访问权限);
* 3、AnnotationConfigApplicationContext:用于读取注解创建容器;
*/
//1、获取核心容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2、根据id获取bean对象
IAccountService service = ac.getBean("accountService",IAccountService.class);
service.saveAccount();
}
}

文件位置图:

【旧】G003Spring学习笔记-IOC之注解方式实现_xml_03

运行结果:

【旧】G003Spring学习笔记-IOC之注解方式实现_ioc_04

 

2、Qualifier

说明:

* Qualifier:
* 细节:在使用注解时,set方法就不是必须的了;
* 作用:当自动类型注入出现多个匹配对象时,且变量名匹配不到相同的bean时,使用此方式;
* 说明:它在给类成员注入时不能单独使用,在给方法注入时可单独使用;
* 属性:value用于指定注入bean的id;

使用示例:

@Autowired//不推荐的方法
@Qualifier("accountDao")//指定使用accountDao,则变量名是accountDao2也必须使用accountDao
//变量名如果是accountDao则匹配到AccountDaoImpl,如果是accountDao2则匹配到AccountDaoImpl2
private IAccountDao accountDao2;

备注:

@Qualifier要用在@Autowired下面;

 

3、Resource

说明:

* Resource:
* 作用:直接按照bean的id注入,可以独立使用;
* 属性:name用于指定bean的id;

使用示例:

@Resource(name = "accountDao2")

注意:

不需要使用@Autowired;

在maven配置文件pom.xml中加入依赖:(???)

<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.1</version>
</dependency>

总结:

* 总结:以上三个注解只能注入其他bean类型的数据,基本类型和String类型不能通过上述方法实现;
* 另外,集合类型的注入只能通过xml实现;

 

4、Value

说明:

* Value:
* 作用:用于注入基本类型和String类型;
* 属性:value指定数据的值,它可以使用spring中的SpEL表达式(也就是Spring的EL表达式)
* SpEL的写法:${表达式}

 

5、作用范围和生命周期的注解实现

作用范围:

说明:

* 用于改变作用范围的;它们和在xml中使用scope属性实现的功能是一样的;
* Scope:
* 作用:指定Bean的作用范围;
* 属性:value指定取值的范围,常用取值:singleton(单例,默认) prototype(多例)
* 出现位置:类上;

代码示例:

@Scope("prototype")//多例测试

IAccountService service = ac.getBean("accountService",IAccountService.class);
IAccountService service2 = ac.getBean("accountService",IAccountService.class);
System.out.println(service==service2);//false

生命周期:

说明:

* 和生命周期相关的;它们和在xml中使用init-method和destroy-method属性实现的功能是一样的;
* PreDestroy:
* 作用:用于指定销毁方法;
* PostConstruct:
* 作用:用于指定初始化方法;

代码示例:

实现类:

package com.zibo.services.impl;

import com.zibo.dao.IAccountDao;
import com.zibo.dao.impl.AccountDaoImpl;
import com.zibo.services.IAccountService;
import jdk.jfr.Frequency;
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;
import javax.annotation.Resource;

/**
* 账户的业务层实现类
* 曾经的xml配置:
* <bean id="accountService" class="com.zibo.services.impl.IAccountServiceImpl"
* scope="" init-method="" destroy-method="">
* <property name="" value="" / ref=""/>
* </bean>
* 用于创建对象的;它们和在xml中编写<bean/>标签实现的功能是一样的;
* Component:用于把当前类作为存入spring容器中;
* 属性:value用于指定bean的id,当我们不写时,默认为当前类名,首字母小写;
* Controller:一般用在表现层;
* Service:一般用在业务层;
* Repository:一般用在持久层;
* 上面三个注解跟Component的作用是一模一样的,
* 它们三个是spring框架为三层提供的明确注解,使三层对象看起来更清晰;
* 用于注入数据的;它们和在xml中的<bean/>标签中写一个<property/>的作用是一样的;
* Autowired:
* 作用:自动按照类型注入,只要容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功;
* 补充:
* 如果没有则注入失败;
* 如果有多个则在按照类型匹配之后再按照key匹配,匹配到与变量名相同的bean则成功;
* 出现位置:成员变量上、方法上;
* Qualifier:
* 细节:在使用注解时,set方法就不是必须的了;
* 作用:当自动类型注入出现多个匹配对象时,且变量名匹配不到相同的bean时,使用此方式;
* 说明:它在给类成员注入时不能单独使用,在给方法注入时可单独使用;
* 属性:value用于指定注入bean的id;
* Resource:
* 作用:直接按照bean的id注入,可以独立使用;
* 属性:name用于指定bean的id;
* 总结:以上三个注解只能注入其他bean类型的数据,基本类型和String类型不能通过上述方法实现;
* 另外,集合类型的注入只能通过xml实现;
* Value:
* 作用:用于注入基本类型和String类型;
* 属性:value指定数据的值,它可以使用spring中的SpEL表达式(也就是Spring的EL表达式)
* SpEL的写法:${表达式}
* 用于改变作用范围的;它们和在xml中使用scope属性实现的功能是一样的;
* Scope:
* 作用:指定Bean的作用范围;
* 属性:value指定取值的范围,常用取值:singleton(单例,默认) prototype(多例)
* 出现位置:类上;
* 和生命周期相关的;它们和在xml中使用init-method和destroy-method属性实现的功能是一样的;
* FreDestroy:
* 作用:用于指定销毁方法;
* PostConstruct:
* 作用:用于指定初始化方法;
*/
@Component("accountService")//如果注解中只有一个value,value是乐意省略的
//@Scope("prototype")//多例测试
public class AccountServiceImpl implements IAccountService {
// @Autowired//不推荐的方法
// @Qualifier("accountDao")//指定使用accountDao,则变量名是accountDao2也必须使用accountDao
@Resource(name = "accountDao2")
//变量名如果是accountDao则匹配到AccountDaoImpl,如果是accountDao2则匹配到AccountDaoImpl2
private IAccountDao accountDao2;
@PostConstruct
private void init(){
System.out.println("初始化。。。");
}
@PreDestroy
private void destroy(){
System.out.println("销毁。。。");
}
@Override
public void saveAccount() {
accountDao2.saveAccount();
}
}

测试类:

package com.zibo.ui;

import com.zibo.services.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* 模拟表现层,调用业务层
*/
public class Client {
public static void main(String[] args) {
/*
* 获取spring的IOC的核心容器,并根据id获取对象
* ApplicationContext的三个常用实现类:
* 1、ClassPathXmlApplicationContext(xml方式最常用):仅能加载类路径下的配置文件;
* 2、FileSystemApplicationContext:可以加载磁盘任意路径下的配置文件(必须有访问权限);
* 3、AnnotationConfigApplicationContext:用于读取注解创建容器;
*/
//1、获取核心容器对象
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2、根据id获取bean对象
IAccountService service = ac.getBean("accountService",IAccountService.class);
service.saveAccount();
ac.close();
}
}

运行结果:

【旧】G003Spring学习笔记-IOC之注解方式实现_spring_05

 

 

 

举报

相关推荐

0 条评论