标签:接口 查询 osi jetty 解耦合 not instance 依赖 ack
Spring是一个轻量级的JavaEE解决方案,整合众多优秀的设计模式。
概念:通过??类,创建对象;
User user = new User(); UserDAO userDAO = new UserDAOImpl();
简单:把接?的实现类,硬编码在程序中;
UserService userService = new UserServiceImpl();
package com.baizhiedu.basic; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class BeanFactory { private static Properties env = new Properties(); static{ try { //第一步 获得IO输入流 InputStream inputStream = BeanFactory.class.getResourceAsStream("/applicationContext.properties"); //第二步 文件内容 封装 Properties集合中 key = userService value = com.baizhixx.UserServiceImpl env.load(inputStream); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } /* 对象的创建方式: 1. 直接调用构造方法 创建对象 UserService userService = new UserServiceImpl(); 2. 通过反射的形式 创建对象 解耦合 Class clazz = Class.forName("com.baizhiedu.basic.UserServiceImpl"); UserService userService = (UserService)clazz.newInstance(); */ public static UserService getUserService() { UserService userService = null; try { //com.baizhiedu.basic.UserServiceImpl Class clazz = Class.forName(env.getProperty("userService")); userService = (UserService) clazz.newInstance(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return userService; } public static UserDAO getUserDAO(){ UserDAO userDAO = null; try { Class clazz = Class.forName(env.getProperty("userDAO")); userDAO = (UserDAO) clazz.newInstance(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return userDAO; } }
配置文件 applicationContext.properties:
# Properties 集合 存储 Properties文件的内容 # 特殊Map key=String value=String # Properties [userService = com.baizhiedu.xxx.UserServiceImpl] # Properties.getProperty("userService") userService = com.baizhiedu.basic.UserServiceImpl userDAO = com.baizhiedu.basic.UserDAOImpl
问题:简单??会存在?量的代码冗余。
img2
通???的代码:
package com.baizhiedu.basic; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class BeanFactory { private static Properties env = new Properties(); static{ try { //第一步 获得IO输入流 InputStream inputStream = BeanFactory.class.getResourceAsStream("/applicationContext.properties"); //第二步 文件内容 封装 Properties集合中 key = userService value = com.baizhixx.UserServiceImpl env.load(inputStream); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } /* key 小配置文件中的key [userDAO,userService] */ public static Object getBean(String key){ Object ret = null; try { Class clazz = Class.forName(env.getProperty(key)); ret = clazz.newInstance(); } catch (Exception e) { e.printStackTrace(); } return ret; } }
Spring本质: ?? ApplicationContext (applicationContext.xml)
使用工具idea
依赖查询网站:https://mvnrepository.com/;
配置 Spring 的 jar 包:
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.4.RELEASE</version> </dependency>
标签:接口 查询 osi jetty 解耦合 not instance 依赖 ack
原文地址:https://www.cnblogs.com/instinct-em/p/13218775.html