栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring注解驱动开发(三)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring注解驱动开发(三)

FactoryBean

返回为单例时

多例时

 

想返回beanFactory本身时,加前缀

 

Bean的生命周期 

 

 

 

 

 

 主要代码

    
    @Bean
    public PersonFactoryBean personFactoryBean(){
        return new PersonFactoryBean();
    }

package com.example.paymentdemo.main;

import com.example.paymentdemo.entity.Person;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.FactoryBean;

import java.awt.*;


public class PersonFactoryBean implements FactoryBean {
    //返回一个对象,这个对象回添加到容器中
    @Override
    public Person getObject() throws Exception {
        return new Person();
    }

    //返回对象类型
    @Override
    public Class getObjectType() {
        return Person.class;
    }

    //返回是否单例
    //true 单例,在容器中保持一份
    //false 多实例,每次获取都回创建一个新的bean
    @Override
    public boolean isSingleton() {
        return FactoryBean.super.isSingleton();
        //return false;
    }
}
package com.example.paymentdemo.main;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class TestPeronFactoryBean {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext("com.example.paymentdemo");
        Object personFactoryBean = applicationContext.getBean("personFactoryBean");
        Object personFactoryBean1 = applicationContext.getBean("personFactoryBean");
        //虽然获取到的bean是getObjectBean()创建对象
        //虽然注册的personFactoryBean但是获取到的确实person
        //想获取personFactroyBean本身的话
        Object bean = applicationContext.getBean("&personFactoryBean");
        System.out.println("工厂为==="+bean);
        System.err.println(personFactoryBean.getClass());
        System.out.println(personFactoryBean == personFactoryBean1);

    }
}

package com.example.paymentdemo.config;

import com.example.paymentdemo.entity.Car;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;


@Configuration
@Component("com.example.paymentdemo")
public class MainConfigOfLifeCircle {
    @Bean(initMethod = "init",destroyMethod = "destory")
    public Car car(){
        return new Car();
    }
}

package com.example.paymentdemo.entity;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;


@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("前置处理beanName"+beanName+"====bean"+bean);
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("后置处理beanName"+beanName+"====bean"+bean);
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }
}
package com.example.paymentdemo.entity;


public class Car {
    public Car(){
        System.out.println("构造器===");
    }

    public void init(){
        System.out.println("init===");
    }
    public void destory(){
        System.out.println("destory====");
    }
}

 

package com.example.paymentdemo.entity;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;


@Component
public class Cat implements InitializingBean, DisposableBean {
    public Cat(){
        System.out.println("cat  构造器");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("在容器销毁时调用");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("在bean创建完成并且赋值完成调用");

    }
}

package com.example.paymentdemo.entity;

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;


@Component
public class Dog {
    public Dog(){
        System.out.println("dog construct ===");
    }
    //对象创建并赋值之后调用
    @PostConstruct
    public void init(){
        System.out.println("dog  init");
    }
    //移除对象之前会调用
    @PreDestroy
    public void destroy(){
        System.out.println("dog  destroy");
    }
}

 

package com.example.paymentdemo.main;

import com.example.paymentdemo.config.MainConfigOfLifeCircle;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class LifeCycle {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext("com.example.paymentdemo");
        System.out.println("====容器创建完成");

        //多实例bean在获取的时候才初始化
        //Object bean = applicationContext.getBean("");
        //关闭容器
        applicationContext.close();
    }
}

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/771039.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号