• spring 后处理器


    Bean后处理器

    新建maven项目并添加spring依赖,目录结构如下

    Axe

    public interface Axe {
        public String chop();
    }

    Person

    public interface Person {
        public void useAxe();
    }

    SteelAxe

    public class SteelAxe
            implements Axe
    {
        public SteelAxe()
        {
            System.out.println("Spring实例化依赖bean:SteelAxe实例...");
        }
        public String chop()
        {
            return "钢斧砍柴真快";
        }
    }
    View Code

    Chinese

    public class Chinese implements Person,InitializingBean {
        private Axe axe;
        private String name;
    
        public Chinese() {
            System.out.println("Spring 实例化主调bean:Chinese实例...");
        }
    
        public void setAxe(Axe axe) {
            this.axe = axe;
        }
    
        public void setName(String name) {
            System.out.println("Spring执行setName()方法注入依赖关系...");
            this.name = name;
        }
    
        public void useAxe(){
            System.out.println(name+axe.chop());
        }
    
        public void init(){
            System.out.println("正在执行初始化方法init...");
        }
    
        public void afterPropertiesSet() throws Exception {
            System.out.println("正在执行初始化方法afterPropertiesSet...");
        }
    }
    View Code

    MyBeanPostProcessor

    package org.mythsky.springdemo;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    import org.springframework.lang.Nullable;
    
    public class MyBeanPostProcessor implements BeanPostProcessor {
    
        @Nullable
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("Bean后处理器在初始化之前对"+beanName+"进行增强处理...");
            return bean;
        }
    
        @Nullable
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("Bean后处理器在初始化之后对"+beanName+"进行增强处理...");
            if(bean instanceof Chinese){
                Chinese c=(Chinese)bean;
                c.setName("Hello world!");
            }
            return bean;
        }
    }
    View Code

    测试BeanTest

    package org.mythsky.springdemo;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class BeanTest {
        public static void main(String[] args){
            ApplicationContext ctx=new ClassPathXmlApplicationContext("services.xml");
            Person p=(Person)ctx.getBean("chinese");
            p.useAxe();
        }
    }
    View Code

    services.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:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="steelAxe" class="org.mythsky.springdemo.SteelAxe"></bean>
        <bean id="chinese" class="org.mythsky.springdemo.Chinese" init-method="init" p:axe-ref="steelAxe" p:name="依赖注入的值"></bean>
        <bean class="org.mythsky.springdemo.MyBeanPostProcessor"></bean>
    
    
    </beans>
    View Code

    测试结果

     容器后处理器

    MyBeanFactoryPostProcessor

    package org.mythsky.springdemo;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    
    public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
        public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
            System.out.println("程序对Spring所做的BeanFactory的初始化没有改变...");
            System.out.println("Spring容器是:"+configurableListableBeanFactory);
        }
    }
    View Code

    services.xml

    <bean class="org.mythsky.springdemo.MyBeanFactoryPostProcessor"></bean>

    运行上面的测试

     

    PropertyPlaceholderConfigurer

    services.xml

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>dbconn.properties</value>
                </list>
            </property>
        </bean>
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
              p:driverClass="${jdbc.driverClassName}"
              p:jdbcUrl="${jdbc.url}"
              p:user="${jdbc.username}"
              p:password="${jdbc.password}"></bean>
    View Code

    dbconn.properties

    jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://10.200.151.28:3306/spring
    jdbc.username=root
    jdbc.password=pass

    MysqlTest

    package org.mythsky.springdemo;
    
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MysqlTest {
        public static void main(String[] args){
            ApplicationContext ctx=new ClassPathXmlApplicationContext("services.xml");
            ComboPooledDataSource dataSource= (ComboPooledDataSource) ctx.getBean("dataSource");
            System.out.println(dataSource.getDriverClass());
            System.out.println(dataSource.getJdbcUrl());
            System.out.println(dataSource.getUser());
            System.out.println(dataSource.getPassword());
        }
    }
    View Code

    添加c3p0依赖

    <dependency>
                <groupId>com.mchange</groupId>
                <artifactId>c3p0</artifactId>
                <version>0.9.5.2</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.8-dmr</version>
            </dependency>
    View Code

    运行结果

    PropertyOverrideConfigurer

    services.xml

        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
              p:driverClass="${jdbc.driverClassName}"
              p:jdbcUrl="${jdbc.url}"
              p:user="${jdbc.username}"
              p:password="${jdbc.password}"></bean>
        <bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
            <property name="locations">
                <list>
                    <value>db.properties</value>
                </list>
            </property>
        </bean>
        <bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"></bean>
    View Code

    db.properties

    dataSource2.driverClass=com.mysql.jdbc.Driver
    dataSource2.jdbcUrl=jdbc:mysql://10.200.151.28:3306/spring
    dataSource2.user=root
    dataSource2.password=pass
    View Code

    MysqlTest

    ComboPooledDataSource dataSource= (ComboPooledDataSource) ctx.getBean("dataSource2");

    运行结果同上。

    以上两种配置可以简写

    <context:property-placeholder location="dbconn.properties"></context:property-placeholder>
        <context:property-override location="db.properties"></context:property-override>
  • 相关阅读:
    (转)一文讲清TCP/IP 协议
    Java框架之Spring Boot和Spring Cloud区别
    php大力力 [006节]初步接触认识phpMyAdmin
    php大力力 [005节] php大力力简单计算器001
    php大力力 [004节]PHP常量MAMP环境下加载网页
    php大力力 [003节]php在百度文库的几个基础教程mac环境下文本编辑工具
    php大力力 [002节]mac php环境安装,mamp安装 ,phpMyAdmin启动
    php大力力 [001节]2015-08-21.php在百度文库的几个基础教程新手上路日记 大力力php 大力同学 2015-08-21 15:28
    C# DataSet与DataTable的区别和用法 ---转载
    【SqlServer】利用sql语句附加,分离数据库-----转载
  • 原文地址:https://www.cnblogs.com/uptothesky/p/7976262.html
Copyright © 2020-2023  润新知