Spring boot 反射获取db账户密码

项目中使用Quartz,需要配置db信息,但是因为yml里面已经配置了,不能在 quartz.properties 里面重复配置,所以直接从datasource里面获取,又因为proj中datasouce的实现类不一定,所以不能直接强转实现类.

package <%=packageName%>.config;


import <%=packageName%>.schedule.factory.TaskJobFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.io.IOException;
import javax.sql.DataSource;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.Properties;

@Configuration
public class QuartzConfiguration {

    @Autowired
    TaskJobFactory jobFactory;
    @Autowired
    private DataSource dataSource;

    @Bean(name = "SchedulerFactory")
    @Qualifier("QuartzSchedulerFactory")
    public SchedulerFactoryBean schedulerFactoryBean() throws IOException, SQLException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
        propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
        propertiesFactoryBean.setProperties(getDatasourceProperties());
        propertiesFactoryBean.afterPropertiesSet();
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        factory.setQuartzProperties(propertiesFactoryBean.getObject());
        factory.setJobFactory(jobFactory);
        return factory;
    }

    private Properties getDatasourceProperties() throws IOException, SQLException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Properties properties = new Properties();
        Class c = dataSource.getClass();
        Method getPassword = c.getMethod("getPassword", null);
        Method getUsername = c.getMethod("getUsername", null);
        Method getDriver = c.getMethod("getDriverClassName", null);
        Object driver = getDriver.invoke(dataSource, null);
        Object pwd = getPassword.invoke(dataSource, null);
        Object username = getUsername.invoke(dataSource, null);
        properties.setProperty("org.quartz.dataSource.myDS.driver", driver.toString());
        properties.setProperty("org.quartz.dataSource.myDS.URL", dataSource.getConnection().getMetaData().getURL());
        properties.setProperty("org.quartz.dataSource.myDS.user", username.toString());
        properties.setProperty("org.quartz.dataSource.myDS.password", pwd.toString());
        return properties;
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值