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

江苏省现代化示范校建设网站百度公司

江苏省现代化示范校建设网站,百度公司,网站域名备案主机名,在虚拟机中如何做二级域名网站目录 1. 生命周期简图2. 扩展接口介绍 2.1 Aware接口2.2 BeanPostProcessor接口2.3 InitializingBean2.4 DisposableBean2.5 BeanFactoryPostProcessor接口3. spring的简化配置 3.1 项目搭建3.2 Bean的配置和值注入3.3 AOP的示例 1. 生命周期简图 2. 扩展接口介绍 2.1 Aware接…

目录

  • 1. 生命周期简图
  • 2. 扩展接口介绍
    • 2.1 Aware接口
    • 2.2 BeanPostProcessor接口
    • 2.3 InitializingBean
    • 2.4 DisposableBean
    • 2.5 BeanFactoryPostProcessor接口
  • 3. spring的简化配置
    • 3.1 项目搭建
    • 3.2 Bean的配置和值注入
    • 3.3 AOP的示例

1. 生命周期简图

 

 

2. 扩展接口介绍

2.1 Aware接口

在spring中Aware接口表示的是感知接口,表示spring框架在Bean实例化过程中以回调的方式将特定在资源注入到Bean中去(如:ApplicationContext, BeanName,BeanFactory等等)。Aware接口本事没有声明任何方法,是一个标记接口,其下有多个子接口,如:BeanNameAware,ApplicationContextAware,BeanFactoryAware等。
每个特定的子接口都会固定一个特定的方法,并注入特定的资源,如BeanFactoryAware接口,定义了setBeanFactory(BeanFactory beanFactory),在spring框架实例化Bean过程中,将回调该接口,并注入BeanFactory对象。再例如:ApplicationContextAware接口,定义了setApplicationContext(ApplicationContext applicationContext) 方法,在spring完成Bean实例化,将回调该接口,并注入ApplicationContext对象(该对象即spring的上下文)。

Aware接口示例(ApplicationContextAware 是 Aware 接口的子接口):

public class ApplicationContextAwareTest implements ApplicationContextAware {private static ApplicationContext ctx;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {ctx = applicationContext;System.out.println("---- ApplicationContextAware 示例 -----------");}public static  <T> T getBean(String beanName) {return (T) ctx.getBean(beanName);}}

配置文件:

<bean id="applicationContextAwareTest" class="org.lisen.springstudy.aware.ApplicationContextAwareTest">
</bean>

2.2 BeanPostProcessor接口

Bean在初始化之前会调用该接口的postProcessBeforeInitialization方法,在初始化完成之后会调用
postProcessAfterInitialization方法。

除了我们自己定义的BeanPostProcessor实现外,spring容器也会自动加入几个,如ApplicationContextAwareProcessor、ApplicationListenerDetector,这些都是BeanPostProcessor的实现类。

BeanPostProcessor接口的定义:

public interface BeanPostProcessor {Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
}

方法的第一个参数为bean实例,第二个参数为beanName,且返回值类型为Object,所以这给功能扩展留下了很大的空间,比如:我们可以返回bean实例的代理对象。

开发示例:

public class BeanPostProcessorTest implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println(beanName + " postProcessBeforeInitialization");return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println(beanName + " postProcessAfterInitialization");return bean;}}

配置文件:

<bean id="beanPostProcessorTest" 
class="org.lisen.springstudy.beanpostprocessor.BeanPostProcessorTest"></bean>

2.3 InitializingBean

该接口是Bean初始化过程中提供的扩展接口,接口中只定义了一个afterPropertiesSet方法。如果一个bean实现了InitializingBean接口,则当BeanFactory设置完成所有的Bean属性后,会回调afterPropertiesSet方法,可以在该接口中执行自定义的初始化,或者检查是否设置了所有强制属性等。

也可以通过在配置init-method方法执行自定义的Bean初始化过程。

示例:

public class InitializingBeanTest implements InitializingBean {@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("InitializingBean.afterPropertiesSet() ......");}}

配置文件:

<bean id="initializingBeanTest" class="org.lisen.springstudy.initializingbean.InitializingBeanTest">
</bean>

2.4 DisposableBean

实现了DisposableBean接口的Bean,在该Bean消亡时Spring会调用这个接口中定义的destroy方法。

public class TestService implements DisposableBean {public void hello() {System.out.println("hello work ... ");}@Overridepublic void destroy() throws Exception {System.out.println("TestService destroy ..... ");}
}

在Spring的应用上下文关闭时,spring会回调destroy方法, 如果Bean需要自定义清理工作,则可以实现该接口。

除了实现DisposableBean接口外,还可以配置destroy-method方法来实现自定义的清理工作。

2.5 BeanFactoryPostProcessor接口

该接口并没有在上面的流程图上体现出来,因为该接口是在Bean实例化之前调用的(但BeanFactoryPostProcessor接口也是spring容器提供的扩展接口,所以在此处一同列出),如果有实现了BeanFactoryPostProcessor接口,则容器初始化后,并在Bean实例化之前Spring会回调该接口的postProcessorBeanFactory方法,可以在这个方法中获取Bean的定义信息,并执行一些自定义的操作,如属性检查等。

3. spring的简化配置

3.1 项目搭建

启用注解,对spring的配置进行简化。

  1. 创建一个maven web工程
  2. 将web改为web3.1,参考第一次课件
  3. 修改pom.xml文件,引入必要的包
<properties><spring.version>5.3.18</spring.version><junit.version>4.12</junit.version></properties><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version></dependency><!-- junit 测试 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency></dependencies>
  1. 在resources根目录下添加spring的配置文件 spring.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.zking"/><!-- 配置properties文件,与Spring @Value 配合使用   方式一    --><!-- <bean ><property name="locations"><list><value>classpath:/test.properties</value></list></property></bean><bean ><property name="properties" ref="configProp"></property></bean> --><!-- 配置properties文件,与Spring @Value 配合使用   方式二 。也可以不使用xml的方式配置,使用程序方式进行配置,可以参考ConfigurationBean  方式三--><bean id="propPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:/test.properties</value></list></property></bean></beans>

程序方式注册如下:

@Configuration
public class ConfigurationBean {@Beanpublic static PropertySourcesPlaceholderConfigurer setPropertiesFile() {PropertySourcesPlaceholderConfigurer config = new PropertySourcesPlaceholderConfigurer();ClassPathResource contextPath = new ClassPathResource("/test.properties");config.setLocation(contextPath);return config;}}
  1. 在resources根目录下新建一个test.properties文件,和spring.xml的配置文件中的配置是相对应的

3.2 Bean的配置和值注入

  1. 创建并注册一个Bean
@Component("stu")
public class Student {//@Value("#{configProp['stu.name']}")@Value("${stu.name}")private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Student [name=" + name + "]";}}
  1. 通过容器获取Bean
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");Student stu = (Student)ctx.getBean("stu");//stu.setName("zs");System.out.println(stu);

3.3 AOP的示例

1.创建一个切面,记录程序运行时间

@Component
@Aspect
@EnableAspectJAutoProxy
public class ProcessAop {//execution(* com.cybx..*.*(..))/*@Pointcut("@annotation(com.zking.mavendemo.config.MyAnnotation)")public void logPointcut() {}*/@Around("execution(* com.zking.mavendemo.service..*.hello*(..))")public Object around(ProceedingJoinPoint  joinPoint) throws Throwable {Class<? extends Signature> signatureClass = joinPoint.getSignature().getClass();System.out.println("AOP signatureClass = " + signatureClass);Object target = joinPoint.getTarget();Class<? extends Object> targetClass = target.getClass();System.out.println("AOP targetClass = " + targetClass);Object returnValue = joinPoint.proceed(joinPoint.getArgs());System.out.println("AOP After ... ");return returnValue;}}

2.创建一个service接口和实现类演示AOP且面
接口:

public interface ITestService {	void helloAop(String msg);
}

实现类:

@Service("testService")
public class TestService implements ITestService {@Overridepublic void helloAop(String msg) {System.out.println("target obj method: " + msg);}}

测试:

		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");ITestService bean = (ITestService)ctx.getBean("testService");bean.helloAop("fdfdfdfdfdfdfdf");

文章转载自:
http://chimborazo.c7623.cn
http://anticlerical.c7623.cn
http://sabbathbreaker.c7623.cn
http://centurion.c7623.cn
http://dimity.c7623.cn
http://pentagonian.c7623.cn
http://chemiosmotic.c7623.cn
http://improvise.c7623.cn
http://precipitation.c7623.cn
http://pervert.c7623.cn
http://venthole.c7623.cn
http://glossily.c7623.cn
http://legerity.c7623.cn
http://zoophytologist.c7623.cn
http://complacently.c7623.cn
http://ignitible.c7623.cn
http://hospitalize.c7623.cn
http://unbeseeming.c7623.cn
http://methacetin.c7623.cn
http://hydrastis.c7623.cn
http://wmo.c7623.cn
http://areostyle.c7623.cn
http://superpose.c7623.cn
http://ramose.c7623.cn
http://exsection.c7623.cn
http://daffydowndilly.c7623.cn
http://improvably.c7623.cn
http://cephalopodous.c7623.cn
http://monarchal.c7623.cn
http://ironmould.c7623.cn
http://skoob.c7623.cn
http://cornflower.c7623.cn
http://declarable.c7623.cn
http://vapour.c7623.cn
http://proferment.c7623.cn
http://abskize.c7623.cn
http://rooftop.c7623.cn
http://geopotential.c7623.cn
http://filariid.c7623.cn
http://bibliology.c7623.cn
http://unaffectedly.c7623.cn
http://strow.c7623.cn
http://sunlike.c7623.cn
http://papermaking.c7623.cn
http://pardon.c7623.cn
http://contusion.c7623.cn
http://occidentalize.c7623.cn
http://nylghai.c7623.cn
http://gird.c7623.cn
http://gsdi.c7623.cn
http://warlike.c7623.cn
http://superheavy.c7623.cn
http://hexagram.c7623.cn
http://precocial.c7623.cn
http://woodworker.c7623.cn
http://ineffective.c7623.cn
http://zingel.c7623.cn
http://copper.c7623.cn
http://carrierbased.c7623.cn
http://extender.c7623.cn
http://undynamic.c7623.cn
http://radiosterilize.c7623.cn
http://kuwait.c7623.cn
http://abruptly.c7623.cn
http://ostrogoth.c7623.cn
http://amboinese.c7623.cn
http://gumwater.c7623.cn
http://nsb.c7623.cn
http://municipalization.c7623.cn
http://gerontophil.c7623.cn
http://enravish.c7623.cn
http://bastion.c7623.cn
http://psychotic.c7623.cn
http://isadora.c7623.cn
http://brighish.c7623.cn
http://swabby.c7623.cn
http://moorage.c7623.cn
http://gadolinite.c7623.cn
http://panache.c7623.cn
http://cask.c7623.cn
http://nodularity.c7623.cn
http://multithreading.c7623.cn
http://quartertone.c7623.cn
http://hummum.c7623.cn
http://electroscope.c7623.cn
http://septennate.c7623.cn
http://aspiration.c7623.cn
http://vanessa.c7623.cn
http://shyster.c7623.cn
http://squabbish.c7623.cn
http://bacteremically.c7623.cn
http://fosterage.c7623.cn
http://lanuginousness.c7623.cn
http://tafia.c7623.cn
http://inquisition.c7623.cn
http://uncinus.c7623.cn
http://batteries.c7623.cn
http://peshitta.c7623.cn
http://sulaiman.c7623.cn
http://compere.c7623.cn
http://www.zhongyajixie.com/news/73074.html

相关文章:

  • 番禺人才市场档案中心公司优化是什么意思
  • 网站开发学习什么站长是什么级别
  • 福建平潭建设局网站长沙百度开户
  • 关于购物网站开发的开题报告精准营销
  • 百度网站的优势网络推广网站
  • 旅行社网站建设需求分析企业查询官网
  • wordpress数据库导入插件合肥建站公司seo
  • 苏州家政保洁公司哪家好合肥seo整站优化网站
  • 清远网站建设推广淘宝运营培训班
  • 网站建设华科技公司以图搜图百度识图
  • 比价网站怎么做的seo专业优化方法
  • 上海做网站的公司电话seo是什么意思电商
  • 西宁微网站建设多少钱深圳企业网站制作公司
  • 四川短视频seo优化网站深圳百度推广关键词推广
  • 深圳网站建设价格多少设计公司网站
  • 织梦网站需要付费吗2024年重大政治时事汇总
  • 做PS的赚钱的网站百度统计平台
  • dw个人网页制作素材sem优化技巧
  • 做网站延期交付了女教师遭网课入侵直播
  • 用dw做网站怎么添加音乐百度seo多少钱一个月
  • 用帝国做网站怎么样黄页网站推广app咋做广告
  • 做婚恋网站的思路如何在互联网上做推广
  • 菏泽网站建设哪家好seo 0xu
  • 做网站的公司 杭州广告网络营销
  • 代码重构网站seo优化 搜 盈seo公司
  • 网站怎么做才 吸引人市场调研报告范文模板word
  • 自学做网站多长时间广告营销
  • 怎样建设自己的ip地址网站网站设计制作哪家好
  • 网址的二级域名建站网站关键词优化
  • 制作高端app开发公司推广优化关键词