Spring
Spring创建对象原理
创建User1, User2, User3实体类
方法1和方法2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package com.course.factory;
import java.io.IOException; import java.io.InputStream; import java.util.Properties;
public class BeanFactory {
public static <T> T getBean(Class<?> clazz) { T t = null; try { t = (T) clazz.newInstance(); } catch (Exception e) { e.printStackTrace(); } return t; }
public static <T> T getBean(String path) { T t = null; try { Class<?> clazz = Class.forName(path); t = (T) clazz.newInstance(); } catch (Exception e) { e.printStackTrace(); } return t; } }
|
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.course.test;
import com.course.factory.BeanFactory; import com.course.vo.*;
public class Test {
public static void main(String[] args) { User1 user1 = BeanFactory.getBean(User1.class); System.out.println(user1.toString()); User2 user2 = BeanFactory.getBean("com.course.vo.User2"); System.out.println(user2.toString()); } }
|
方法3
bean.properties
1 2 3
| User1=com.course.vo.User1 User2=com.course.vo.User2 User3=com.course.vo.User3
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| package com.course.factory;
import java.io.IOException; import java.io.InputStream; import java.util.Properties;
public class BeanFactory {
private static Map<String, String> mappings = new HashMap<>(); static { Properties properties = new Properties(); InputStream is = BeanFactory.class.getClassLoader().getResourceAsStream("com/course/config/bean.properties"); try { properties.load(is); Set<Entry<Object,Object>> entrySet = properties.entrySet(); for (Entry<Object, Object> entry : entrySet) { mappings.put(entry.getKey().toString(), entry.getValue().toString()); } } catch (IOException e) { e.printStackTrace(); } } public static <T> T getBean(String key) { T t = null; try { String path = mappings.get(key); Class<T> clazz = (Class<T>) Class.forName(path); t = clazz.newInstance(); } catch (Exception e) { e.printStackTrace(); } return t; } }
|
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.course.test;
import com.course.factory.BeanFactory; import com.course.vo.*;
public class Test {
public static void main(String[] args) { User3 user3 = BeanFactory.getBean("User3"); System.out.println(user3.toString()); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| package com.course.factory;
import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; import java.util.Set;
public class BeanFactory {
public static Map<String, Object> ioc = new HashMap<>(); static { Properties properties = new Properties(); InputStream is = BeanFactory.class.getClassLoader().getResourceAsStream("com/course/config/bean.properties"); try { properties.load(is); Set<Entry<Object,Object>> entrySet = properties.entrySet(); for (Entry<Object, Object> entry : entrySet) { String key = entry.getKey().toString(); Class<?> clazz = Class.forName(entry.getValue().toString()); Object obj = clazz.newInstance(); ioc.put(key, obj); } } catch (Exception e) { e.printStackTrace(); } } }
|
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.course.test;
import com.course.factory.BeanFactory; import com.course.vo.*;
public class Test {
public static void main(String[] args) { User3 user3 = (User3) BeanFactory.ioc.get("User3"); System.out.println(user3.toString()); } }
|
得到资源的说明
1.从src目录下面去得到资源文件
1
| InputStream is=BeanFactory.class.getClassLoader().getResourceAsStream("com/course/config/bean.properties");
|
2.从src的根目录下面得到资源文件
1
| InputStream is=BeanFactory.class.getResourceAsStream("/bean.properties");
|
3.从当前类的根目录下面得到资源文件
1
| InputStream is=BeanFactory.class.getResourceAsStream("bean.properties");
|
Spring
Spring容器读取配置文件,根据配置文件创建相应的对象,并将对象放入IoC容器(Map)中,然后应用程序根据id(key)从IoC容器(Map)中取出相应的对象。
以后开发过程中只要看到new操作,就应该使用IoC容器。但是这只针对成员变量,不针对局部变量。
实现IOC和DI要使用的技术:
- 配置依赖关系 xml配置文件
- xml的解析 dom4j sax(jdk自带的)
- 工厂设计模式
- 反射技术
- 【注解】:通过反射获取注解对象,进而获取注解对象的value
1 2 3 4
| @WebServlet("/user/getUser") public class UserController {
}
|
1 2 3
| Class<?> clazz = Class.forName(path); WebServlet annotation = clazz.getAnnotation(WebServlet.class); String[] value = annotation.value();
|
最终目的:
程序的高内聚、低耦合
spring官方网站:https://spring.io/
spring相关jar包:
- spring-aop-4.3.7.RELEASE.jar: Spring对aop的支持
- spring-aspects-4.3.7.RELEASE.jar: Spring对aspects的支持,aspects是一个AOP开源库
- spring-beans-4.3.7.RELEASE.jar: Spring对配置文件对象的解析与创建
- spring-context-4.3.7.RELEASE.jar: Spring上下文的支持包
- spring-context-support-4.3.7.RELEASE.jar: Spring上下文集成的一些第三方库,例如:定时器
- spring-core-4.3.7.RELEASE.jar: Spring核心包
- spring-expression-4.3.7.RELEASE.jar: Spring的相关表达式包
- spring-instrument-4.3.7.RELEASE.jar: Spring的辅助包
- spring-instrument-tomcat-4.3.7.RELEASE.jar: Spring的辅助包,对tomcat的支持
- spring-jdbc-4.3.7.RELEASE.jar: Spring对数据访问的支持,包含JdbcTemplate
- spring-jms-4.3.7.RELEASE.jar: Spring对消息中间件的支持,可以集成MQ
- spring-messaging-4.3.7.RELEASE.jar: 同spring-jms-4.3.7.RELEASE.jar
- spring-orm-4.3.7.RELEASE.jar和spring-oxm-4.3.7.RELEASE.jar: 都是对第三方ORM框架的扩展和集成
- spring-test-4.3.7.RELEASE.jar: Spring测试包
- spring-tx-4.3.7.RELEASE.jar: Spring对所有ORM框架的事务支持
- spring-web-4.3.7.RELEASE.jar: Spring对所有的控制层框架的集成支持
- spring-webmvc-4.3.7.RELEASE.jar: Spring对SpringMVC的支持
- spring-webmvc-portlet-4.3.7.RELEASE.jar:
- spring-websocket-4.3.7.RELEASE.jar: Spring对websocket的支持