spring IOC使用
1.创建一个Spring
1.创建一个java项目

2.添加jar包
2.1添加spring4个基础jar包,和一个日志包

3.添加Spring 配置文件
点击查看代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
2.通过IOC 容器管理Bean 对象
2.1 修改配置文件
点击查看代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 将bean给spring管理-->
<bean id="addUsers" class="com.njsxt.service.impl.UsersServiceImpl" />
</beans>
2.2 通过spring
获取bean
点击查看代码
public static void main(String[] args) {
//老方法
/*UsersService usersService = new UsersServiceImpl();
usersService.addUsers();*/
//springioc管理bean
//加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UsersService usersService = (UsersService) applicationContext.getBean("addUsers");
usersService.addUsers();
}
3.Spring IOC 容器创建Bean 对象的三种方式
构造方法创建bean
静态工程创建bean
静态工厂类
点击查看代码
/**
* 静态工厂
*/
public class ObjectFactory {
//获取UsersService
public static UsersService getInstance() {
return new UsersServiceImpl();
}
}
添加配置文件
点击查看代码
<!-- 静态工厂-->
<bean id="usersService2" class="com.njsxt.factory.ObjectFactory" factory-method="getInstance"/>
动态工厂创建bean
动态工厂类
点击查看代码
/**
* 动态工厂
*/
public class DynamicObjectFactory {
public UsersService getInstance() {
return new UsersServiceImpl();
}
}
添加配置文件
点击查看代码
<!-- 动态工厂-->
<bean id="dynamicObjectFactory" class="com.njsxt.factory.DynamicObjectFactory" />
<bean id="usersService3" factory-bean="dynamicObjectFactory" factory-method="getInstance" />
</beans>
配置文件中可以用name属性给id起别名可以起别名,一样用,name可以起多个,用逗号分隔
点击查看代码
<bean id="usersService" name="name1,name2,name3" class="com.njsxt.service.impl.UsersServiceImpl"/>
4.spring ioc 类获取bean的二种方式
1.通过类型获取对象时,如果同一类型存在多个对象,我们可以使
用id 或name 来识别需要获取的对象。
点击查看代码
//类获取 通过指定指定id名,来获取
UsersServiceImpl bean = applicationContext.getBean("usersService", UsersServiceImpl.class);
bean.addUsers();
2.获取容器中所有bean的ID,在通过方法一获取bean
点击查看代码
//先获取配置文件中所有的id
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
UsersServiceImpl bean = applicationContext.getBean(beanDefinitionNames[0], UsersServiceImpl.class);
bean.addUsers();
5.spring ioc 容器创建对象的策略
交给ioc管理的bean对象可以通过lazy-init="boolean"进行延迟创建和立即创建,前提:scope 属性为singleton 才有效,如果scope 属性为pototype,无论lazy-init 的属性值是什么,都只在通过getbean 时进行实例化。
立即创建
点击查看代码
<bean id="usersService" name="name1,name2,name3" class="com.njsxt.service.impl.UsersServiceImpl" lazy-init="false"/>
延迟创建
点击查看代码
<!-- 将bean给spring管理 构造方法创建 name可以起别名 lazy-init="true" 代表延迟创建 -->
<bean id="usersService" name="name1,name2,name3" class="com.njsxt.service.impl.UsersServiceImpl" lazy-init="true"/>
6.Bean 对象的作用域
作用域:作用域限定了Spring Bean 的作用范围,在Spring 配置文件定义Bean 时,通过
声明scope 配置项,可以灵活定义Bean 的作用范围。
scope 属性的值:
singleton
prototype
3.5.2.1 singleton(单例)
singleton 为scope 属性的默认值。当scope 属性的值为singleton 时,Spring IOC 容器启
动时会立即实例化一次Bean 对象,并一直被Spring IOC 容器所缓存,所以生命周期较长。
singleton 特点:
Spring IOC 容器启动时会创建Bean 对象
每次调用getBean 都返回spring 容器中的唯一一个对象
3.5.2.2 prototype(多例)
当scope 属性的值为prototype 时,每次调用调用getBean 方法时都会返回一个新的Bean
对象,Spring IOC 容器并不会缓存该对象,所以就不再负责管理它的生命周期。
prototype 特点:
Spring IOC 容器启动时不会创建Bean 对象。
每次调用getBean 都会创建一个新Bean 对象。

浙公网安备 33010602011771号