大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
P6Spy使用:[url]http://donald-draper.iteye.com/blog/2319646[/url]
使用P6Spy的时候用到这一句我们来看这一句的内涵:
P6DataSource p6DSource = new P6DataSource(cpDSource)
// P6DataSource p6DSource = new P6DataSource(cpDSource)
public class P6DataSource extends P6Base
implements DataSource, Referenceable, Serializable
{
//source是通过构造传入的数据源c3p0或Druid
public P6DataSource(DataSource source)
{
rds = source;
}
//初始化驱动及日志模块
public static void initMethod()
{
P6SpyDriverCore.initMethod((com.p6spy.engine.spy.P6SpyDriver.class).getName());
}
//获取Connection
public Connection getConnection()
throws SQLException
{
if(rds == null)
bindDataSource();
return P6SpyDriverCore.wrapConnection(rds.getConnection());
}
protected DataSource rds;
protected String rdsName;
//通过static语句块调用初始化方法
static
{
initMethod();
}
}
初始化属性线路:初始化驱动及日志相关信息
P6DataSource,通过static语句块调用初始化方法initMethod(),initMethod()中一句很重要的
P6SpyDriverCore.initMethod((com.p6spy.engine.spy.P6SpyDriver.class).getName());再来看看P6SpyDriverCore
的initMethod的方法都做了什么事情
public abstract class P6SpyDriverCore
implements Driver
{
public static synchronized void initMethod(String spydriver)
{
if(initialized)
return;
String path = P6SpyProperties.getPropertiesPath();
if(path == null)
{
foundSpyProperties = false;
return;
}
foundSpyProperties = true;
//初始化spy.properties属性文件
P6SpyProperties properties = new P6SpyProperties();
P6SpyOptions coreOptions = new P6SpyOptions();
OptionReloader.add(coreOptions, properties);
String className = "no class";
String classType = "driver";
try
{
//realdriver
ArrayList driverNames = null;
//日志模块
ArrayList modules = null;
//获取驱动名
driverNames = P6SpyOptions.allDriverNames();
//获取所有日志模块
modules = P6SpyOptions.allModules();
boolean hasModules = modules.size() > 0;
Iterator i = null;
classType = "driver";
Driver realDriver;
for(i = driverNames.iterator(); i.hasNext(); P6LogQuery.logDebug("Registered driver: " + className + ", realdriver: " + realDriver))
{
P6SpyDriver spy = null;
if(hasModules)
{
spy = new P6SpyDriver();
DriverManager.registerDriver(spy);
}
className = (String)i.next();
deregister(className);
realDriver = (Driver)P6Util.forName(className).newInstance();
if(P6SpyOptions.getDeregisterDrivers())
//注册驱动realdriver=com.mysql.jdbc.Driver
DriverManager.registerDriver(realDriver);
if(hasModules)
{
spy.setPassthru(realDriver);
realDrivers.add(realDriver);
}
}
if(hasModules)
{
factories = new ArrayList();
classType = "factory";
com.p6spy.engine.common.P6Options options;
for(i = modules.iterator(); i.hasNext(); P6LogQuery.logDebug("Registered factory: " + className + " with options: " + options))
{
className = (String)i.next();
//module.log=com.p6spy.engine.logging.P6LogFactory
//module.outage=com.p6spy.engine.outage.P6OutageFactory
P6Factory factory = (P6Factory)P6Util.forName(className).newInstance();
factories.add(factory);
options = factory.getOptions();
if(options != null)
OptionReloader.add(options, properties);
}
}
initialized = true;
for(Enumeration e = DriverManager.getDrivers(); e.hasMoreElements(); P6LogQuery.logDebug("Driver manager reporting driver registered: " + e.nextElement()));
}
catch(Exception e)
{
String err = "Error registering " + classType + " [" + className + "]\nCaused By: " + e.toString();
P6LogQuery.logError(err);
throw new P6DriverNotFoundError(err);
}
}
//P6DataSource的getConnection方法条用P6SpyDriverCore的wrapConnection(Connection realConnection)方法
public static Connection wrapConnection(Connection realConnection)
throws SQLException
{
Connection con = realConnection;
if(factories != null)
{
for(Iterator it = factories.iterator(); it.hasNext();)
{
P6Factory factory = (P6Factory)it.next();
//这里是重点,这里是通过P6Factory来获取连接,P6SpyDriverCore
//在初始化initMethod已经P6LogFactory,P6OutageFactory
//module.log=com.p6spy.engine.logging.P6LogFactory
//module.outage=com.p6spy.engine.outage.P6OutageFactory
con = factory.getConnection(con);
}
}
return con;
}
private String getRealUrl(String url)
{
if(P6SpyOptions.getUsePrefix())
return url.startsWith("p6spy:") ? url.substring("p6spy:".length()) : null;
else
return url;
}
public boolean acceptsURL(String p0)
throws SQLException
{
String realUrl = getRealUrl(p0);
boolean accepts = false;
if(passthru == null && initialized)
{
if(realDrivers.size() == 0)
throw new SQLException("P6 has no drivers registered");
findPassthru(realUrl);
if(passthru == null)
throw new SQLException("P6 can't find a driver to accept url (" + realUrl + ") from the " + realDrivers.size() + " drivers P6 knows about. The current driver is null");
}
if(realUrl != null)
accepts = passthru.acceptsURL(realUrl);
return accepts;
}
protected Driver passthru;
protected static boolean initialized = false;
protected static ArrayList factories;
[color=red] protected static ArrayList realDrivers = new ArrayList();[/color]
protected static boolean foundSpyProperties;
}
这里我们先来看一下p6spy的属性配置,再看P6LogFactory获取连接及日志
P6SpyProperties属性文件操作类:
public class P6SpyProperties
{
public static void initMethod()
{
//设置属性文件名
setSpyProperties(System.getProperty("spy.properties", "spy.properties"));
}
public P6SpyProperties()
{
File propertiesFile = new File(propertiesPath);
if(propertiesFile.exists())
{
long lastModified = propertiesFile.lastModified();
if(lastModified != propertiesLastModified)
{
propertiesLastModified = lastModified;
//加载spy.properties文件
properties = P6Util.loadProperties(SPY_PROPERTIES_FILE);
} else
{
properties = null;
}
}
}
public static String getPropertiesPath()
{
return propertiesPath;
}
//设置属性文件名
public static void setSpyProperties(String _properties)
{
SPY_PROPERTIES_FILE = _properties != null ? _properties : "spy.properties";
propertiesPath = findPropertiesPath();
propertiesLastModified = -1L;
}
protected static String findPropertiesPath()
{
String propertiesPath = P6Util.classPathFile(SPY_PROPERTIES_FILE);
if(propertiesPath != null)
{
File propertiesFile = new File(propertiesPath);
if(propertiesFile.exists())
return propertiesPath;
}
return null;
}
public Properties forceReadProperties()
{
File propertiesFile = new File(propertiesPath);
if(propertiesFile.exists())
{
long lastModified = propertiesFile.lastModified();
properties = P6Util.loadProperties(SPY_PROPERTIES_FILE);
}
return properties;
}
protected static final String OPTIONS_FILE_PROPERTY = "spy.properties";
protected static final String DFLT_OPTIONS_FILE = "spy.properties";
protected static String SPY_PROPERTIES_FILE;
protected static long propertiesLastModified = -1L;
protected static String propertiesPath;
public Properties properties;
//通过静态语句块调用initMethod方法
static
{
initMethod();
}
}
P6SpyOptions配置选项类:set,get方法对应配置文件选项
public class P6SpyOptions extends P6Options
{
public static void setAutoflush(String _autoflush)
{
autoflush = P6Util.isTrue(_autoflush, false);
}
public static boolean getAutoflush()
{
return autoflush;
}
public static void setExclude(String _exclude)
{
exclude = _exclude;
}
public static void setExcludecategories(String _excludecategories)
{
excludecategories = _excludecategories;
}
public static void setFilter(String _filter)
{
filter = P6Util.isTrue(_filter, false);
}
public static void setIncludecategories(String _includecategories)
{
includecategories = _includecategories;
}
public static void setDeregisterDrivers(String trueOrFalse)
{
deregister = P6Util.isTrue(trueOrFalse, false);
}
public static void setLogfile(String _logfile)
{
logfile = _logfile;
if(logfile == null)
logfile = "spy.log";
}
public static void setAppender(String className)
{
appender = className;
}
//实际驱动设置
public static void setRealdriver(String _realdriver)
{
realdriver = _realdriver;
}
public static void setRealdriver2(String _realdriver2)
{
realdriver2 = _realdriver2;
}
public static void setAppend(String _append)
{
append = P6Util.isTrue(_append, true);
}
public static boolean getAppend()
{
return append;
}
public static void setSpydriver(String _spydriver)
{
spydriver = _spydriver;
if(spydriver == null)
spydriver = "com.p6spy.engine.spy.P6SpyDriver";
}
public static String getSpydriver()
{
return spydriver;
}
//设置日志日期格式
//dateformat=hh:mm:ss,SSS
public static void setDateformat(String _dateformat)
{
dateformat = _dateformat;
if(_dateformat == null || _dateformat.equals(""))
dateformatter = null;
else
dateformatter = new SimpleDateFormat(_dateformat);
}
public static void setStringmatcher(String _stringmatcher)
{
stringmatcher = _stringmatcher;
if(stringmatcher == null || stringmatcher.equals(""))
stringmatcher = "com.p6spy.engine.common.SubstringMatcher";
try
{
stringMatcherEngine = (StringMatcher)P6Util.forName(stringmatcher).newInstance();
}
catch(InstantiationException e)
{
P6LogQuery.logError("Could not instantiate string matcher class: " + stringmatcher);
e.printStackTrace();
}
catch(IllegalAccessException e)
{
P6LogQuery.logError("Could not instantiate string matcher class: " + stringmatcher);
e.printStackTrace();
}
catch(ClassNotFoundException e)
{
P6LogQuery.logError("Could not instantiate string matcher class: " + stringmatcher);
e.printStackTrace();
}
}
//设置重新加载配置文件间隔
public static void setReloadPropertiesInterval(String _reloadpropertiesinterval)
{
reloadPropertiesInterval = P6Util.parseLong(_reloadpropertiesinterval, -1L);
reloadMs = reloadPropertiesInterval * 1000L;
}
public static void setRealDataSource(String _realdatasource)
{
realdatasource = _realdatasource;
}
//当配置文件修改时,重新加载属性文件
public void reload(P6SpyProperties properties)
{
P6LogQuery.logDebug(getClass().getName() + " reloading properties");
modules = properties.getReverseOrderedList("module.");
driverNames = properties.getReverseOrderedList("realdriver");
properties.setClassValues(com.p6spy.engine.common.P6SpyOptions.class);
configureReloadingThread();
P6LogQuery.initMethod();
P6LogQuery.logInfo("reloadProperties() successful");
}
//获取所有模块
public static ArrayList allModules()
{
return modules;
}
//获取所有驱动
public static ArrayList allDriverNames()
{
return driverNames;
}
protected static Thread reloadThread = null;
protected static OptionReloader reloader = null;
public static final String DRIVER_PREFIX = "realdriver";
public static final String MODULE_PREFIX = "module.";
private static ArrayList modules;
private static ArrayList driverNames;
private static boolean usePrefix;
private static boolean autoflush;
private static String exclude;
private static boolean filter;
private static String include;
private static String logfile;
private static String appender;
private static String realdriver;
private static String realdriver2;
private static String realdriver3;
private static String spydriver;
private static boolean append;
private static String properties;
private static boolean deregister;
private static String dateformat;
private static SimpleDateFormat dateformatter;
private static String includecategories;
private static String excludecategories;
private static String stringmatcher;
private static StringMatcher stringMatcherEngine;
private static String sqlExpression;
private static boolean stackTrace;
private static String stackTraceClass;
private static boolean reloadProperties;
private static long reloadPropertiesInterval;
private static long reloadMs;
private static String jndicontextfactory;
private static String jndicontextproviderurl;
private static String jndicontextcustom;
private static String realdatasource;
private static String realdatasourceclass;
private static String realdatasourceproperties;
private static long executionThreshold;
}
自此属性文件配置线路完毕。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/185679.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...