# spring_ioc **Repository Path**: taylorswift_meimie/spring_ioc ## Basic Information - **Project Name**: spring_ioc - **Description**: Spring学习 1.程序之间的耦合IOC初步理解 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-01-30 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # spring_ioc #### 学习大纲 1. 程序之间的耦合IOC初步理解 2. 自己实现Ioc通过容器获取对象 3. spring中的Ioc和DI学习 4. spring的常用注解 #### Ioc和DI概念 1. IOC:(Inverse Of Control:反转控制)降低程序间的耦合 2. DI:(Dependency Injection:依赖注入) #### 自己实现Ioc private static Properties properties; private static Map beans; static { InputStream resourceAsStream = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties"); properties = new Properties(); try { properties.load(resourceAsStream); } catch (IOException e) { e.printStackTrace(); throw new ExceptionInInitializerError("init error"); } beans = new HashMap(); Enumeration keys = properties.keys(); while (keys.hasMoreElements()) { String key = keys.nextElement().toString(); String value = properties.getProperty(key); try { Object object = Class.forName(value).newInstance(); beans.put(key, object); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } #### Ioc和DI xml配置方式 1 3 3 4 22 q q1 q1 q2 q4 q3 q3 1 1 2 2 1 4 5 5 7 8 #### Ioc和DI 注解配置方法 xml文件: // 配置类 @Component("adminService") @Controller("") // 表现层 @Service("") // 业务层 @Repository("") // 持久层 // 注入 @Antowired // 类型注入 @Qualifier("") // 在按照类型注入的基础上,再按照bean的id注入 @Resource // 直接按照Bean的id注入 @Value // 注入基本数据类型 @Scope() // 作用范围 singleton prototype @PostConstruct // 指定初始化方法 @PreDestroy // 指定销毁方法 /** * Spring 纯注解配置 */ @Configuration() // 用于指定当前类是一个 spring 配置类 @ComponentScan("com.cherry") // 要扫描的包 @Bean("dataSource") // 该注解只能写在方法上,表明使用此方法创建一个对象,并且放入 spring 容器。 @Import // 导入其他配置类 @@PropertySource("classpath:jdbc.properties") // 用于加载.properties 文件中的配置 #### Gitee Feature 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. 码云官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解码云上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是码云最有价值开源项目,是码云综合评定出的优秀开源项目 5. 码云官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. 码云封面人物是一档用来展示码云会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)