欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring Aware标记接口使用案例解析

程序员文章站 2022-07-04 18:38:19
aware接口是一个标记接口xxxaare在spring中表示对xxx可以感知,通俗点解释就是:如果在某个类里面想要使用spring的一些东西,就可以通过实现xxxaware接口告诉spring,sp...

aware接口是一个标记接口

xxxaare在spring中表示对xxx可以感知,通俗点解释就是:如果在某个类里面想要使用spring的一些东西,就可以通过实现xxxaware接口告诉spring,spring看到后就会送过来,而接受的方式是通过实现接口唯一的方法setxxx.比如applicationcontextaware

applicationcontextaware使用

编写springaware实现applicationcontext接口

package com.rookie.bigdata.aware;

import org.springframework.beans.beansexception;
import org.springframework.context.applicationcontext;
import org.springframework.context.applicationcontextaware;

/**

 * @author rookie
 * @version 1.0
 * @date 2020/3/21 22:58
  */

public class springaware implements applicationcontextaware {

  private applicationcontext applicationcontext;
  
  //spring容器会将applicationcontext对象传入过来
  @override
  public void setapplicationcontext(applicationcontext applicationcontext) throws beansexception {
    system.out.println("传入的ioc容器applicationcontext = [" + applicationcontext + "]");
    this.applicationcontext = applicationcontext;
  }
  
  public applicationcontext getapplicationcontext() {
    return applicationcontext;
  }


}

user实体类

package com.rookie.bigdata.domain;

public class user {
  private string username;
  private string address;

  public string getusername() {
    return username;
  }
  
  public void setusername(string username) {
    this.username = username;
  }
  
  public string getaddress() {
    return address;
  }
  
  public void setaddress(string address) {
    this.address = address;
  }

}

spring-aware.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" 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/tx
          http://www.springframework.org/schema/tx/spring-tx.xsd
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context.xsd">

  <bean id="user" class="com.rookie.bigdata.domain.user">
    <property name="username" value="张三"></property>
    <property name="address" value="北京"></property>
  </bean>
  
  <bean id="springaware" class="com.rookie.bigdata.aware.springaware"></bean>

</beans>

测试类

	 @test
  public void test1(){
    applicationcontext applicationcontext = new classpathxmlapplicationcontext("/aware/spring-aware.xml");

//    user user = (user) applicationcontext.getbean("user");
//    system.out.println(user);

    springaware springaware = (springaware) applicationcontext.getbean("springaware");
    applicationcontext applicationcontext1 = springaware.getapplicationcontext();
    user user = (user) applicationcontext1.getbean("user");
    system.out.println(user);
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。