前人总结的解决问题的思想
一般都混合使用,要根据具体业务去借鉴设计,而非生搬硬套
现有业务场景,才有设计模式
绿色–创建型
土黄色–结构
红色–行为型
/**
*
* https://blog.csdn.net/qq_33591903/article/details/91444342
* Class.getResource(String name):在当前调用类的同一路径下查找该资源
*
* ClassLoader.getResource(String name):在根目录下查找该资源文件,即"/"或classpath目录
*
*
*
* URL url1 = getClass().getResource("/");
* System.out.println("根目录的路径 :" + url1); //file:/D:/spring_read/spring-dim/spring-dim-core/target/classes/
*
* URL url1 = getClass().getResource("");
* file:/D:/spring_read/spring-dim/spring-dim-core/target/classes/org/springdim/core/
*
*
* URL url1 = getClass().getResource("start");
* 该类的路径在org/springdim/core
* URL url1 = getClass().getResource("a.txt");
* 如果在下面放a.txt是找不到,需要放在target/classes/org/springdim/core/
* 有包org.springdim.core.start
* URL url1 = getClass().getResource("start");
* 返回的路径是: file:/D:/spring_read/spring-dim/spring-dim-core/target/classes/org/springdim/core/start
*
*
*
* URL resource = this.getClass().getClassLoader().getResource("org/springdim");
* //getResource("org/springdim"); file:/D:/spring_read/spring-dim/spring-dim-core/target/classes/org/springdim
* //getResource(""); file:/D:/spring_read/spring-dim/spring-dim-core/target/classes/
* //URL resource1 = this.getClass().getClassLoader().getResource("/");
* //getClassLoader() 已经代表根路径,不能再加/
* System.out.println(resource);
*/
在每个接口如此写jdbc
比如crud
final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
final String DBURL = "jdbc:mysql://127.0.0.1:3306/javaweb?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
final String DBUSER = "root";
final String DBPASS = "root";
Connection conn = null;
try {
Class.forName(DBDRIVER);
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
String sql = "select * from user where username=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "username");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
int id = rs.getInt(1);
//业务逻辑
}
rs.close();
pstmt.close();
} catch (Exception e) {
out.println(e);
} finally {
conn.close();
}
你crud就需要四次连接数据库,能否做连接复用,一次连接供crud复用。数据库连接池解决如下【线程池是线程复用,数据库连接池是数据库连接对象复用。设计思想都是资源复用:用完不立即丢弃,而是先存到队列里面休眠】
@Component
public class BasicDBSource implements DBSource {
private Properties pro;//属性对象
private String url;
private String user;
private String password;
private int max;//最大连接数
private List<Connection> connections;//集合类存放被关闭的Connection对象
public BasicDBSource() throws IOException, ClassNotFoundException {
this("D:\\spring_read\\spring-dim\\spring-dim-jdbc\\src\\main\\resources\\myjdbc.properties");//调用带参的构造函数
}
public BasicDBSource(String configFile) throws IOException, ClassNotFoundException {
//获取属性文件,因为我将url,user,password,max,driver的属性值存放在一个myjdbc.properties的属性文件里
//这样设计的目的也是为了提高代码的低偶合度,为重构做些小小的贡献
pro = new Properties();
pro.load(new FileInputStream(configFile));
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
max = Integer.parseInt(pro.getProperty("poolmax"));
Class.forName(pro.getProperty("driver"));
connections = new ArrayList<Connection>();
}
public synchronized void closeConnection(Connection con) throws SQLException {
//如果集合中的Connection对象个数大于等于允许的最大连接数,则关闭连接
//否则,将要关闭的Connection对象放入集合栈中,等需要连接时再弹出
if (connections.size() >= max) {
con.close();
} else {
connections.add(con);
}
}
public synchronized Connection getConnection() throws SQLException {
//要获取连接时,先判断集合栈中是否存有压入的Connection对象,如果没有,则像原来一样创建连接
//如果有,则弹出集合栈中的Connection对象,无需再行连接
if (connections.size() == 0) {
return DriverManager.getConnection(url, user, password);
} else {
int last = connections.size() - 1;
return connections.remove(last);
}
}
}
https://blog.csdn.net/qq_41063141/article/details/105258266
好好看看自己写的这篇文章
初衷:资源能够共享,只需要赋值/初始化一次,大家都能够重复利用(单例需要提供一个全局访问点)
应用:Listener,Calender,IOC容器,配置信息Config,工厂类也是单例
解决问题:恶劣环境(程序的健全性)
spring中的应用
spring的IOC中的 bean默认是单例:使用注册登记式实现的,每使用一次,都往固定的容器中去注册并且将使用过的对象缓存,下次去取,就从缓存中取,以保证每次获取的都是同一个对象
从self4j看设计模式
https://blog.csdn.net/sweetyi/article/details/104817574
self4j的使用
https://blog.csdn.net/weixin_43085797/article/details/105767714?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.nonecase