首页 > 编程知识 正文

spring aop配置详解(spring xml配置)

时间:2023-05-03 06:19:54 阅读:93578 作者:2112

一、springIOC的介绍

应用程序上下文是Spring IoC容器实现的代表,负责实例化、配置和组装Bean

二、环境准备

创建maven项目

2 .添加依赖关系

从属关系

groupid org.spring框架/groupid

artifactidspring -上下文/工件id

5.2.6 .版本/版本

/dependency3.创建实体类

公共类辅助工具

私有字符串名称;

私有距离;

私有字符串中枢;

公共类个人

私有身份;

私有字符串真实名称;

私有字符串名称;

私有辅助辅助辅助;

私有日期;工作日;

私有列表字符串移动;

私有映射,字符串粗略;

4 .在资源文件下创建XML文件springTest.xml

三、实例分析

1 .实现依赖注入的两种方式

基于set法的依存注入1 .注入的属性中需要追加set法

2.name值是set方法的名称

bean类=' com.tedu.liyu.entry.student ' id=' student ',其中bean类等于' com.tedu.liyu.entry.student '

属性名称=' age ' value=' 12 ' /属性

属性名称=' Hobby ' value='王者荣耀' /属性

属性名称='名称'值='李白' /属性

创建/bean测试类:

公共类测试IOC

classpathxmlapplicationcontextioc;

@Before

公共语音论坛(

IOC=newclasspathxmlapplicationcontext (spring测试. XML );

}

@Test

公共语音测试1 () {

student student=IOC.get bean (' student ',Student.class;

系统输出(student );

}

{1}基于构造函数的依赖注入1 .需要向注入的实体类添加带有参数的构建方法

2.name值是构造方法的参数

公共从属(字符串名称、整数距离、字符串中枢) {

this.name=名称;

this.age=age;

霍比(霍比);

} bean类=' com.tedu.liyu.entry.student ' id=' student ',其中,bean类是“第一类”

构造函数- argname='名称'值='十三中' /构造函数- arg

构造函数- argname=' age ' value=' 18 ' /构造函数- arg

构造函数-别名=' hobby ' value='数学' /构造函数- arg

/bean2.复杂类型的注入

由于以下内容采用了set注入,因此需要添加与实体类相关的set方法

bean类=' com.tedu.liyu.entry .人员id='人员'

属性名称=' id '值='1' /属性

<property name="name"> <null></null> </property> <property name="realName" value=""></property> <property name="birthday" value="2020/05/20"></property> <property name="student"> <bean class="com.tedu.liyu.entry.Student"> <property name="name" value="开放的美女"></property> <property name="hobby" value="打篮球"></property> <property name="age" value="18"></property> </bean> </property> <property name="hobbies"> <list> <value>唱歌</value> <value>跳舞</value> </list> </property> <property name="course"> <map> <entry key="1" value="Java"></entry> <entry key="2" value="Js"></entry> </map> </property> </bean>

3.自动注入

<bean class="com.tedu.liyu.entry.Student" id="student"> <constructor-arg name="name" value="十三中"></constructor-arg> <constructor-arg name="age" value="18"></constructor-arg> <constructor-arg name="hobby" value="数学"></constructor-arg> </bean> <bean class="com.tedu.liyu.entry.Person" id="person2" autowire="byName"> <property name="id" value="999"></property> <property name="name" value="666"></property> </bean>

当person对象中需要引用另一个student对象时,在自动注入时,spring会在IOC容器中自动找到匹配的进行注入。

byName:按照名字进行装配,以属性名作为id去容器中查找组件,进行赋值,如果找不到则装配null

byType:按照类型进行装配,以属性的类型作为查找依据去容器中找到这个组件,如果有多个类型相同的bean对象,那么会报异常,如果找不到则装配null

4.Bean的作用域

singleton:单例,只会在IOC容器中创建一次

prototype:多例,每次获取IOC容器都会new一个bean

<bean class="com.tedu.liyu.entry.Student" id="student3" scope="singleton"> </bean>

5.生命周期回调

两种方式:

1.使用接口实现的方式实现生命周期的回调初始化方法:

实现接口InitializingBean,重afterPropertiesSet方法销毁方法:

实现接口DisposableBean,重写destroy方法

<bean class="com.tedu.liyu.entry.Student" id="student"></bean>public class Student implements InitializingBean , DisposableBean { public void afterPropertiesSet() throws Exception { System.out.println("生命周期回调---初始化"); } public void destroy() throws Exception { System.out.println("生命周期回调---销毁"); } }

2.使用对应bean里创建的方法实现生命周期的回调

<bean class="com.tedu.liyu.entry.Student" id="student" init-method="init" destroy-method="destroy"></bean>public class Student{ public void init(){ System.out.println("生命周期回调---初始化"); } public void destroy(){ System.out.println("生命周期回调---销毁"); } }

示例如上:

init-method的值为实体类中自定义初始化的方法名

destroy-method的值为实体类中自定义销毁的方法名

6.懒加载

<!-- 懒加载 使用lazy‐init设置懒加载 默认false:在spring容器创建时加载 true:在使用时,才会加载--> <bean class="com.tedu.liyu.entry.Student" id="student" lazy-init="false"></bean>

7.加载外部配置文件

1.先在resources下添加springContext.properties配置文件

student.name=123 student.age=10 student.hobby=中国<context:property-placeholder location="springContext.properties" file-encoding="UTF-8"/> <bean class="com.tedu.liyu.entry.Student" id="student"> <property name="name" value="${student.name}"></property> <property name="age" value="${student.age}"></property> <property name="hobby" value="${student.hobby}"></property> </bean>

示例如上:

需要注意的是,需要添加file-encoding="UTF-8",不然配置文件中有中文时,引用可能会乱码。

8.SpEL的使用

<bean class="com.tedu.liyu.entry.Student" id="student"> <constructor-arg name="name" value="十三中"></constructor-arg> <constructor-arg name="age" value="18"></constructor-arg> <constructor-arg name="hobby" value="数学"></constructor-arg> </bean> <bean class="com.tedu.liyu.entry.Person" id="person"> <property name="name" value="#{student.name}"></property> <property name="student" value="#{student}"></property> <property name="birthday" value="2020/05/20"></property> </bean>

SpEL:Spring Expression Language,spring的表达式语言,支持运行时查询操作对象

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。