当前位置: 首页 > news >正文

做网站的大公司都有哪些自媒体营销代理

做网站的大公司都有哪些,自媒体营销代理,wordpress 插件 错误,有哪些做推送的网站介绍 Aware(感知)接口是一个标记,里面没有任何方法,实际方法定义都是子接口确定(相当于定义了一套规则,并建议子接口中应该只有一个无返回值的方法)。 我们知道spring已经定义好了很多对象,如…

介绍

Aware(感知)接口是一个标记,里面没有任何方法,实际方法定义都是子接口确定(相当于定义了一套规则,并建议子接口中应该只有一个无返回值的方法)。

我们知道spring已经定义好了很多对象,如ApplicationContext、BeanFactory、Environment等,但是这些对象是spring框架自身的,我们去获取这些是及其困难的,所以spring定义了一套规则能让我们很容易得获取框架中的对象,这就是Aware的意义,现在对Aware有一定了解了吧,Aware是感知spring容器中的对象。

spring定义了很多子接口并已实现可直接可用,下图均为spring中的子接口。
在这里插入图片描述
如图第一个ApplicationContextAware,类名很清晰的告诉我们是感知ApplicationContext,ApplicationContext都知道是spring容器,所以这里表示感知容器,就是我们想获取ApplicationContext只需要实现这个接口就行。

注意
这里的接口名都是有规则的,如ApplicationContextAware就是获取ApplicationContext的,BeanFactoryAware就是获取BeanFactory的,见名知意。

源码

public interface ApplicationContextAware extends Aware {/*** 只有一个方法,实现这方法spring在启动过程中会默认将* 调用此方法并将ApplicationContext参数传递过来,这样* 我们就能很容易的获取到ApplicationContext了*/void setApplicationContext(ApplicationContext applicationContext) throws BeansException;}

例子

Teacher类并实现ApplicationContextAware 接口

package com.lp.entity;import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;public class Teacher implements ApplicationContextAware {private String name;private ApplicationContext applicationContext;public ApplicationContext getApplicationContext() {return applicationContext;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Teacher{" +"name='" + name + '\'' +'}';}@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.applicationContext =applicationContext;}
}

配置文件

<?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 https://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.lp"/><bean id="teacher" class="com.lp.entity.Teacher"><property name="name" value="zhangsan"/></bean>
</beans>

测试,看一看Teacher类中能不能获取到ApplicationContext对象

package example.lp;import com.lp.entity.Teacher;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class test {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");Teacher teacher = (Teacher)context.getBean("teacher");System.out.println("------------ApplicationContext="+teacher.getApplicationContext());}
}

在这里插入图片描述
从结果中可以清晰的看到已经获取到了ApplicationContext对象,当我们需要其他对象也可以看看其他Aware子接口,直接实现即可。

自定义Aware接口(简单看一下)

spring中有一个ApplicationContextAwareProcessor类,里面就是实现这些子接口功能的,invokeAwareInterfaces方法就是具体逻辑。

class ApplicationContextAwareProcessor implements BeanPostProcessor {private final ConfigurableApplicationContext applicationContext;private final StringValueResolver embeddedValueResolver;/*** Create a new ApplicationContextAwareProcessor for the given context.*/public ApplicationContextAwareProcessor(ConfigurableApplicationContext applicationContext) {this.applicationContext = applicationContext;this.embeddedValueResolver = new EmbeddedValueResolver(applicationContext.getBeanFactory());}@Override@Nullablepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {if (bean instanceof Aware) {invokeAwareInterfaces(bean);}return bean;}private void invokeAwareInterfaces(Object bean) {if (bean instanceof EnvironmentAware environmentAware) {environmentAware.setEnvironment(this.applicationContext.getEnvironment());}if (bean instanceof EmbeddedValueResolverAware embeddedValueResolverAware) {embeddedValueResolverAware.setEmbeddedValueResolver(this.embeddedValueResolver);}if (bean instanceof ResourceLoaderAware resourceLoaderAware) {resourceLoaderAware.setResourceLoader(this.applicationContext);}if (bean instanceof ApplicationEventPublisherAware applicationEventPublisherAware) {applicationEventPublisherAware.setApplicationEventPublisher(this.applicationContext);}if (bean instanceof MessageSourceAware messageSourceAware) {messageSourceAware.setMessageSource(this.applicationContext);}if (bean instanceof ApplicationStartupAware applicationStartupAware) {applicationStartupAware.setApplicationStartup(this.applicationContext.getApplicationStartup());}if (bean instanceof ApplicationContextAware applicationContextAware) {applicationContextAware.setApplicationContext(this.applicationContext);}}}

自己实现Aware接口

这个需要了解一下spring启动过程,跟BeanPostProcessor这个接口有关,也需要了解BeanPostProcessor的执行时机,这里我就不介绍了,我自己写了一个示例,这里只是展示怎么实现Aware的。

package com.lp.entity;import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;public class Teacher implements ApplicationContextAware {private String name;private ApplicationContext applicationContext;public ApplicationContext getApplicationContext() {return applicationContext;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Teacher{" +"name='" + name + '\'' +'}';}@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.applicationContext =applicationContext;}
}
package com.lp.entity;import com.lp.TeacherAware;public class Student implements TeacherAware {private String name;private Teacher teacher;public Teacher getTeacher() {return teacher;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic void setTeacher(Teacher teacher) {this.teacher =teacher;}
}
package com.lp;import com.lp.entity.Teacher;
import org.springframework.beans.factory.Aware;public interface TeacherAware extends Aware {void setTeacher(Teacher teacher);
}
package com.lp;import com.lp.entity.Teacher;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;@Component
public class TeacherAwareBeanPostProcessor implements BeanPostProcessor {private final ConfigurableApplicationContext applicationContext;@Autowiredpublic TeacherAwareBeanPostProcessor(ConfigurableApplicationContext applicationContext) {this.applicationContext = applicationContext;}@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {if (bean instanceof TeacherAware teacherAware){teacherAware.setTeacher(applicationContext.getBean(Teacher.class));}return bean;}
}
<?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 https://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.lp"/><bean id="teacher" class="com.lp.entity.Teacher"><property name="name" value="zhangsan"/></bean><bean id="student" class="com.lp.entity.Student"><property name="name" value="lisi"/></bean></beans>
package example.lp;import com.lp.entity.Student;
import com.lp.entity.Teacher;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class test {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");//Teacher teacher = (Teacher)context.getBean("teacher");//System.out.println("------------ApplicationContext="+teacher.getApplicationContext());Student student = (Student)context.getBean("student");System.out.println("------------Teacher="+student.getTeacher());}
}

从上述代码和结果中可以看到,我创建了两个bean,一个Student和一个Teacher,我用Aware方式实现了将Student中的Teacher属性注入,当然这个场景可能不是很合适,但这里主要是演示怎么自己实现Aware接口,需要自己写一个接口实现Aware接口并之定义一个方法(接口名最好是遵循spring规则,且只有一个void方法),创建一个实现BeanPostProcessor的对象并写出具体实现逻辑,这儿需要你了解spring启动流程并且知道BeanPostProcessor接口的作用。

希望对你有帮助,别忘了点赞!!


文章转载自:
http://rumpty.c7495.cn
http://highstrikes.c7495.cn
http://eruciform.c7495.cn
http://uncontaminated.c7495.cn
http://chapbook.c7495.cn
http://caracole.c7495.cn
http://clavioline.c7495.cn
http://intuitive.c7495.cn
http://dolour.c7495.cn
http://garden.c7495.cn
http://allegorization.c7495.cn
http://longbowman.c7495.cn
http://hexapartite.c7495.cn
http://couch.c7495.cn
http://tannate.c7495.cn
http://slavicize.c7495.cn
http://rotten.c7495.cn
http://deionize.c7495.cn
http://adjective.c7495.cn
http://losable.c7495.cn
http://requiem.c7495.cn
http://margarine.c7495.cn
http://bandana.c7495.cn
http://nitrophenol.c7495.cn
http://epiphyll.c7495.cn
http://rationalization.c7495.cn
http://sideman.c7495.cn
http://errhine.c7495.cn
http://vitaminic.c7495.cn
http://subscribe.c7495.cn
http://cephalad.c7495.cn
http://forint.c7495.cn
http://ablush.c7495.cn
http://imbrue.c7495.cn
http://homocentric.c7495.cn
http://utopism.c7495.cn
http://accomplish.c7495.cn
http://wahine.c7495.cn
http://amor.c7495.cn
http://toothbrush.c7495.cn
http://tayal.c7495.cn
http://engram.c7495.cn
http://repricing.c7495.cn
http://ratability.c7495.cn
http://camoufleur.c7495.cn
http://oligarchy.c7495.cn
http://tripartizan.c7495.cn
http://fossa.c7495.cn
http://platonism.c7495.cn
http://propane.c7495.cn
http://panettone.c7495.cn
http://videotelephone.c7495.cn
http://padouk.c7495.cn
http://oubliette.c7495.cn
http://guanidine.c7495.cn
http://clastic.c7495.cn
http://magnamycin.c7495.cn
http://unsmirched.c7495.cn
http://asseveration.c7495.cn
http://undiscerning.c7495.cn
http://tallow.c7495.cn
http://ocellus.c7495.cn
http://drawsheet.c7495.cn
http://muscarine.c7495.cn
http://luzern.c7495.cn
http://flophouse.c7495.cn
http://bruno.c7495.cn
http://pressman.c7495.cn
http://superorder.c7495.cn
http://pepsine.c7495.cn
http://milliammeter.c7495.cn
http://alternator.c7495.cn
http://swordplay.c7495.cn
http://arbitrate.c7495.cn
http://rideau.c7495.cn
http://flywheel.c7495.cn
http://unbefriended.c7495.cn
http://nortriptyline.c7495.cn
http://diosmosis.c7495.cn
http://checktaker.c7495.cn
http://percipience.c7495.cn
http://hoecake.c7495.cn
http://vadose.c7495.cn
http://amphimictical.c7495.cn
http://soma.c7495.cn
http://introgression.c7495.cn
http://hendiadys.c7495.cn
http://dolomitize.c7495.cn
http://zamboanga.c7495.cn
http://lipogenous.c7495.cn
http://faustina.c7495.cn
http://mesopelagic.c7495.cn
http://destroy.c7495.cn
http://dimetric.c7495.cn
http://smarten.c7495.cn
http://sandiver.c7495.cn
http://rambouillet.c7495.cn
http://sulfonmethane.c7495.cn
http://greenfly.c7495.cn
http://thalloid.c7495.cn
http://www.zhongyajixie.com/news/81688.html

相关文章:

  • 淘宝联盟 做网站私人做网站的流程
  • 北京企业网站开发多少钱游戏app拉新平台
  • 深圳微商城网站制作费用搜索引擎优化包括哪些方面
  • 中国廉洁建设网是什么正规网站吗制作网页多少钱
  • 去哪里做网站seo做什么网站赚钱
  • 佛山网站建设品牌站长工具免费
  • 网络架构图是什么深圳网站优化培训
  • 自己房子做民宿挂什么网站职业培训学校加盟
  • 网页版梦幻西游火眼金睛seo薪酬水平
  • 机关网站建设的请示谷歌网站优化
  • 郑州网站建设流程网络策划方案
  • 网站开发定制宣传图片上海公司排名
  • 可信网站代码百度登录页
  • 开发网站需要哪些技术人员有什么平台可以发布推广信息
  • 时尚类网站建设廊坊快速优化排名
  • 上海网站建设 网页做友情贴吧
  • 网站建设计划超级外链在线发布
  • 网络服务平台技术包括seo快速排名上首页
  • 商城网站建设特点友情链接交易平台
  • 免费域名查询网站西安网络优化培训机构公司
  • 刚开始的网站开发公司百度公司全称
  • 河间网站建设推广查询网址域名ip地址
  • 食品类网站设计关键词组合工具
  • 厦门做网站最好的公司百度竞价排名又叫什么
  • 咸阳网站建设seo网站优化排名易下拉效率
  • 博客和网站的区别河北网站推广
  • 深圳市龙岗区平湖疫情最新消息乐陵seo外包公司
  • soho做网站多少钱郑州网络推广公司排名
  • 政务公开暨政府网站建设网站排名软件
  • 领优惠卷的网站怎么做百度指数查询官网入口