# Spring Ioc study **Repository Path**: wgspring/Spring-Ioc-study ## Basic Information - **Project Name**: Spring Ioc study - **Description**: Spring中IoC容器的学习 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: develop - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2017-02-22 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README Spring IoC 练习 --------------- > 1.Spring的配置 在pom.xml中添加spring的依赖 org.springframework spring-context 4.3.6.RELEASE 在resources文件夹下新建spring-config.xml >2.bean的基本使用 spring-config添加bean 其中init-method和destroy-method属性分别指定初始化函数和回收函数, scope="prototype"指定非单例模式,否则默认单例模式 在java文件中获取bean //获取上下文 ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); //通过id获取bean ScrewDriver screwDriver = context.getBean("screwDriver",ScrewDriver.class); screwDriver.useScrew(); //关闭上下文 ((ConfigurableApplicationContext)context).close(); >3.构造函数的注入 通过基本类型参数构造 通过配置文件构造 通过复合类型构造 >4.set函数的注入 通过基本类型set注入配置 通过复合类型set注入配置 通过复合类型set自动注入配置 >4.Annotation定义 首先需要在spring-config.xml中添加 @Component("XXX")用来定义bean @Value("XXX")用来定义数值 @Autowired用来自动配置 @PostConstruct用来指定初始化函数 @PreDestroy用来指定回收函数