上海网站建设公司sky上海企业服务云下载
- 作者: 五速梦信息网
- 时间: 2026年03月21日 09:24
当前位置: 首页 > news >正文
上海网站建设公司sky,上海企业服务云下载,wordpress 顺序,重庆旅游攻略目录 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的示例
- 生命周期简图 2. 扩展接口介绍 2.1 Aware接…目录 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的示例
- 生命周期简图 2. 扩展接口介绍 2.1 Aware接口 在spring中Aware接口表示的是感知接口表示spring框架在Bean实例化过程中以回调的方式将特定在资源注入到Bean中去如ApplicationContext, BeanName,BeanFactory等等。Aware接口本事没有声明任何方法是一个标记接口其下有多个子接口如BeanNameAwareApplicationContextAwareBeanFactoryAware等。 每个特定的子接口都会固定一个特定的方法并注入特定的资源如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 idapplicationContextAwareTest classorg.lisen.springstudy.aware.ApplicationContextAwareTest /bean2.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 idbeanPostProcessorTest classorg.lisen.springstudy.beanpostprocessor.BeanPostProcessorTest/bean2.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 idinitializingBeanTest classorg.lisen.springstudy.initializingbean.InitializingBeanTest /bean2.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的定义信息并执行一些自定义的操作如属性检查等。
- spring的简化配置 3.1 项目搭建 启用注解对spring的配置进行简化。 创建一个maven web工程将web改为web3.1参考第一次课件修改pom.xml文件引入必要的包 propertiesspring.version5.3.18/spring.versionjunit.version4.12/junit.version/propertiesdependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion\({spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-web/artifactIdversion\){spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-webmvc/artifactIdversion\({spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-orm/artifactIdversion\){spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-aspects/artifactIdversion\({spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-test/artifactIdversion\){spring.version}/version/dependency!– junit 测试 –dependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion\({junit.version}/versionscopetest/scope/dependency/dependencies在resources根目录下添加spring的配置文件 spring.xml ?xml version1.0 encodingUTF-8?beans xmlnshttp://www.springframework.org/schema/beansxmlns:contexthttp://www.springframework.org/schema/contextxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://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.xsdcontext:component-scan base-packagecom.zking/!-- 配置properties文件与Spring Value 配合使用 方式一 --!-- bean property namelocationslistvalueclasspath:/test.properties/value/list/property/beanbean property nameproperties refconfigProp/property/bean --!-- 配置properties文件与Spring Value 配合使用 方式二 。也可以不使用xml的方式配置使用程序方式进行配置可以参考ConfigurationBean 方式三--bean idpropPlaceholder classorg.springframework.beans.factory.config.PropertyPlaceholderConfigurerproperty namelocationslistvalueclasspath:/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;}}在resources根目录下新建一个test.properties文件和spring.xml的配置文件中的配置是相对应的 3.2 Bean的配置和值注入 创建并注册一个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 ];}}通过容器获取BeanClassPathXmlApplicationContext 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);
- 上一篇: 上海网站建设公公司网络规划设计师教程 pdf
- 下一篇: 上海网站建设公司电wordpress织梦
相关文章
-
上海网站建设公公司网络规划设计师教程 pdf
上海网站建设公公司网络规划设计师教程 pdf
- 技术栈
- 2026年03月21日
-
上海网站建设工资多少seo优化排名易下拉试验
上海网站建设工资多少seo优化排名易下拉试验
- 技术栈
- 2026年03月21日
-
上海网站建设高端定制东莞寮步做网站的有吗
上海网站建设高端定制东莞寮步做网站的有吗
- 技术栈
- 2026年03月21日
-
上海网站建设公司电wordpress织梦
上海网站建设公司电wordpress织梦
- 技术栈
- 2026年03月21日
-
上海网站建设公司推上海关键词优化排名哪家好
上海网站建设公司推上海关键词优化排名哪家好
- 技术栈
- 2026年03月21日
-
上海网站建设管理磁力猫搜索引擎入口官网
上海网站建设管理磁力猫搜索引擎入口官网
- 技术栈
- 2026年03月21日
