spring
2021-05-01 10:28
标签:接口 查询 osi jetty 解耦合 not instance 依赖 ack Spring是一个轻量级的JavaEE解决方案,整合众多优秀的设计模式。 概念:通过??类,创建对象; 简单:把接?的实现类,硬编码在程序中; 配置文件 applicationContext.properties: 问题:简单??会存在?量的代码冗余。 通???的代码: Spring本质: ?? ApplicationContext (applicationContext.xml) 使用工具idea 依赖查询网站:https://mvnrepository.com/; 配置 Spring 的 jar 包: spring 标签:接口 查询 osi jetty 解耦合 not instance 依赖 ack 原文地址:https://www.cnblogs.com/instinct-em/p/13218775.htmlTable of Contents
1. 引言
1.1 EJB存在的问题
1.2 什么是Spring
开源: Tomcat resion jetty
收费: weblogic webaphere
img1
1.3 ??设计模式
User user = new User();
UserDAO userDAO = new UserDAOImpl();
UserService userService = new UserServiceImpl();
1.4 简单工厂的设计
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;
}
}
# 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
1.5 通用工厂的设计
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;
}
}
1.6 通???的使??式
1.7 总结:
2. 第一个 Spring 程序
2.1 环境搭建
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-contextartifactId>
version>5.1.4.RELEASEversion>
dependency>