Spring学习笔记(一)

mac2026-05-20  5

Spring概述

1、Spring的定义 Spring是分层的javaSE/EE全栈式轻量级开源框架,以IOC和AOP为内核,提供表现层SpringMVC和持久层SpringJDBC以及业务层事务管理等众多的企业级应用技术,还能整合开源世界众多著名的第三方框架和类库,逐渐成为使用最多的javaEE企业应用开源框架。 2、Spring的优势 ①方便解耦,简化开发。 ②支持AOP编程 ③支持声明式事务 ④方便程序的测试 ⑤方便集成各种优秀的框架 ⑥降低javaEEAPI的使用难度 3、Spring的体系结构

程序的耦合和解耦

一、程序的耦合和解耦思路分析

如果在pom.xml文件中去掉mysql的依赖, 1、使用DriverManager.registerDriver(new com.mysql.jdbc.Driver());,运行main就会发生编译错误。 2、使用Class.forName(“com.mysql.jdbc.Driver”);,运行main就会发生运行时异常。

package com.how2java; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; /** * 程序的耦合, * 耦合:程序间的依赖关系,包括类之间依赖、方法之间依赖, * 解耦:降低程序间的依赖关系, * 而实际开发中编译期不依赖,运行时才依赖, * 解决思路: * ①通过读取配置文件获取要创建的对象的全限定类名; * ②使用反射创建对象,避免使用new关键字。 */ public class JdbcDemo1 { public static void main(String[] args) throws Exception { //1.注册驱动 // DriverManager.registerDriver(new com.mysql.jdbc.Driver()); Class.forName("com.mysql.jdbc.Driver"); //2.获取连接 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/spring?characterEncoding=UTF-8", "root", "admin"); //3.获取操作数据库的预处理对象 PreparedStatement ps = conn.prepareStatement("select * from account"); //4.执行sql语句,得到结果集 ResultSet rs = ps.executeQuery(); //5.遍历结果集 while(rs.next()) { System.out.println(rs.getString("name")); } //6.释放资源 rs.close(); conn.close(); } } Error:(20, 56) java: 程序包com.mysql.jdbc不存在 Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) at com.how2java.JdbcDemo1.main(JdbcDemo1.java:21)

二、工厂模式解耦

1、编写工厂类和配置文件 bean.properties

accountService=com.how2java.service.impl.AccountServiceImpl accountDao=com.how2java.dao.impl.AccountDaoImpl

BeanFactory.java

package com.how2java.factory; import java.io.InputStream; import java.util.Properties; /** * 一个创建Bean对象的工厂。 * Bean:在计算机英语中有可重用组件的含义。 * JavaBean:用java语言编写的可重用组件。 * 创建service和dao对象: * ①需要一个配置service和dao的配置文件,包含唯一标志和全限定类名(key=value); * ②通过读取配置文件中的内容反射对象。 * 配置文件可以是xml,也可以是properties。 */ public class BeanFactory { //定义一个Properties对象 private static Properties props; //使用静态代码块为Properties对象赋值 static{ try { //实例化对象 props = new Properties(); //获取properties文件的流对象 InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties"); props.load(in); } catch(Exception e) { throw new ExceptionInInitializerError("初始化properties失败"); } } /** * 根据bean的名称获取Bean对象 * @param beanName * @return */ public Object getBean(String beanName) { Object bean = null; try { String beanPath = props.getProperty(beanName); bean = Class.forName(beanPath).newInstance(); } catch(Exception e) { e.printStackTrace(); } return bean; } }

2、工厂模式解耦 IAccountDao.java

package com.how2java.dao; public interface IAccountDao { public void saveAccount(); }

AccountDaoImpl.java

package com.how2java.dao.impl; import com.how2java.dao.IAccountDao; public class AccountDaoImpl implements IAccountDao{ @Override public void saveAccount() { System.out.println("保存了账户"); } }

IAccountService.java

package com.how2java.service; public interface IAccountService { public void saveAccount(); }

AccountServiceImpl.java

package com.how2java.service.impl; import com.how2java.dao.IAccountDao; import com.how2java.factory.BeanFactory; import com.how2java.service.IAccountService; public class AccountServiceImpl implements IAccountService { // private IAccountDao accountDao = new AccountDaoImpl(); private IAccountDao accountDao = (IAccountDao) new BeanFactory().getBean("accountDao"); @Override public void saveAccount() { accountDao.saveAccount(); } }

Client.java

package com.how2java.ui; import com.how2java.factory.BeanFactory; import com.how2java.service.IAccountService; import com.how2java.service.impl.AccountServiceImpl; /** * 模拟一个表现层,调用业务层 */ public class Client { public static void main(String[] args) { // IAccountService as = new AccountServiceImpl(); IAccountService as = (IAccountService) new BeanFactory().getBean("accountService"); as.saveAccount(); } }

3、工厂模式解耦改造 BeanFactory.java

package com.how2java.factory; import java.io.InputStream; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.Properties; /** * 一个创建Bean对象的工厂。 * Bean:在计算机英语中有可重用组件的含义。 * JavaBean:用java语言编写的可重用组件。 * 创建service和dao对象: * ①需要一个配置service和dao的配置文件,包含唯一标志和全限定类名(key=value); * ②通过读取配置文件中的内容反射对象。 * 配置文件可以是xml,也可以是properties。 */ public class BeanFactory { //定义一个Properties对象 private static Properties props; //定义一个Map用于存放创建的对象 private static Map<String, Object> beans; //使用静态代码块为Properties对象赋值 static{ try { //实例化对象 props = new Properties(); //获取properties文件的流对象 InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties"); props.load(in); //实例化容器 beans = new HashMap<String, Object>(); //取出配置文件中所有的Key Enumeration keys = props.keys(); //遍历枚举 while(keys.hasMoreElements()) { //取出每个keys String key = keys.nextElement().toString(); //根据key获取value String beanPath = props.getProperty(key); //反射创建对象 Object value = Class.forName(beanPath).newInstance(); //把key和value存入容器 beans.put(key, value); } } catch(Exception e) { throw new ExceptionInInitializerError("初始化properties失败"); } } /** * 根据bean名称获取对象 * @param beanName * @return */ public static Object getBean(String beanName) { return beans.get(beanName); } /** * 根据bean的名称获取Bean对象 * @param beanName * @return public Object getBean(String beanName) { Object bean = null; try { String beanPath = props.getProperty(beanName); bean = Class.forName(beanPath).newInstance();//每次都会调用默认构造函数创建对象。 } catch(Exception e) { e.printStackTrace(); } return bean; } */ }

Client.java

package com.how2java.ui; import com.how2java.factory.BeanFactory; import com.how2java.service.IAccountService; import com.how2java.service.impl.AccountServiceImpl; /** * 模拟一个表现层,调用业务层 */ public class Client { public static void main(String[] args) { // IAccountService as = new AccountServiceImpl(); //AccountServiceImpl是多例的 //改成单例后执行效率更高 //但单例对象中一般不会有可以改变的类成员 for(int i = 0; i < 5; i++) { IAccountService as = (IAccountService) new BeanFactory().getBean("accountService"); System.out.println(as); as.saveAccount(); } } }
最新回复(0)