Spring框架-IOC
一、概述
Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。官方下载地址: http://repo.spring.io
二、特点:
1.轻量
2.控制反转
3.面向切面
4.容器
三、IOC
控制反转(IOC),在以前开发时,对象在程序内部创建,对象的控制权属于程序。程序应了Spring框架后,对象的创建交给Spirng框架来处理。也就是表示对象的控制权属于Spring这个外部框架,我们控制发生反转。从另一个角度来说,程序需要某对象,由于程序本身是没有创建对象,这个对象需要依赖外部框架来注入这个对象到程序中来。所以也叫依赖注入(DI)。
四、使用步骤:
1.添加配置文件: applicationcontext.xml,这个文件建议保留。
2.添加相关的jar包,我们需要什么功能组件,则添加对应的Jar包就可。
commons-logging-1.1.1.jar --不是Spring框架提供的包,日志记录包
spring-beans-4.3.10.RELEASE.jar
spring-context-4.3.10.RELEASE.jar
spring-context-support-4.3.10.RELEASE.jar
spring-core-4.3.10.RELEASE.jar
spring-expression-4.3.10.RELEASE.jar
3.在配置文件中配置对象,运行时Spring框架自动创建配置的对象
<!-- 配置对象,框架运行时,自动创建这个对象,存储在IOC容器中 -->
<bean id="stu1" class="com.sgxy.entity.Student">
<property name="id" value="10001"/>
<property name="age" value="20"/>
<property name="name" value="张明"/>
<property name="sex" value="男"/>
</bean>
4.在程序中获取IOC容器,再从IOC容器中取出所需的对象并操作对象
//获取IOC容器,容器的创建需要依赖配置文件,在加载配置文件时,自动创建容器中所配置的对象。
ApplicationContext cxt = new ClassPathXmlApplicationContext("config/applicationcontext.xml");
System.out.println("-------------------");
//从IOC容器中取对象
Student stu = (Student) cxt.getBean("stu1");
System.out.println(stu);
五、Bean创建
Bean创建有3种方式:
1.采用构造函数来创建Bean
<bean id="g1" class="com.sgxy.entity.Grade">
<property name="gradeId" value="1"/>
<property name="gradeName" value="一年级"/>
</bean>
<!-- 配置对象,框架运行时,自动创建这个对象,存储在IOC容器中 -->
<bean id="stu1" class="com.sgxy.entity.Student">
<!-- 调用无参的构造函数创建对象,再给对象的属性赋值 -->
<property name="id" value="10001"/>
<property name="age" value="20"/>
<property name="name" value="张明"/>
<property name="sex" value="男"/>
<property name="grade">
<!-- 写在property这个节点下面的Bean,叫内部Bean,作用域只在这个对象中。 -->
<bean id="g2" class="com.sgxy.entity.Grade">
<property name="gradeId" value="2"/>
<property name="gradeName" value="2年级"/>
</bean>
</property>
</bean>
<bean id="stu2" class="com.sgxy.entity.Student">
<!-- 调用带参数的构造函数,可以根据索引或参数名称来进行赋值-->
<constructor-arg name="id" value="10002"/>
<constructor-arg name="name" value="李华"/>
<constructor-arg name="sex" value="男"/>
<constructor-arg name="age" value="22" />
<constructor-arg name="grade" ref="g1"/>
</bean>
2.采用静态工厂方法来创建Bean
工厂的创建:
/**
* 静态工厂,有一个静态方法,方法中创建对象并返回
*/
public class StudentFactory {
public static Student createStudent() {
Grade g = new Grade();
g.setGradeId(3);
g.setGradeName("三年级");
Student stu = new Student(10002,"朱小军","男",21,g);
return stu;
}
}
配置文件的配置:
<!-- 根据静态工厂的方法来创建Bean对象 -->
<bean id="stu3" class="com.sgxy.util.StudentFactory"
factory-method="createStudent"></bean>
3.采用实例工厂方法来创建Bean
实例工本实现
/**
* 实例工厂,有一个方法,方法中创建对象并返回
*/
public class StudentFactory {
public Student createStudent() {
Grade g = new Grade();
g.setGradeId(3);
g.setGradeName("三年级");
Student stu = new Student(10002,"朱小军","男",21,g);
return stu;
}
}
配置文件配置
<!-- 创建工厂对象 -->
<bean id="studentFactory" class="com.sgxy.util.StudentFactory"/>
<!-- 根据实例工厂的方法来创建Bean对象 -->
<bean id="stu3" factory-bean="studentFactory" factory-method="createStudent"></bean>
六、Bean的作用域
springIOC容器中的对象都默认是单例模式的。
Student stu = (Student) cxt.getBean("stu3");
System.out.println(stu.hashCode());
//从容器中又获取一次对象
Student stu2 = (Student) cxt.getBean("stu3");
System.out.println(stu2.hashCode());
输出结果:
如何配置Bean对象的作用域,scope属性可以修改。取值范围:singleton(默认),prototype,request,session
singleton:单例模式
prototype:非单例模式
request,session:这两个只能Web项目中有效,request表示一个请求,Session表示一次会话。
Beanr创建是在Spring框架加载时就创建了。有时想要需要对象时才加载对象,我们设置对象为延迟加载。 lazy-init=“true”
<!-- 配置对象,框架运行时,自动创建这个对象,存储在IOC容器中 -->
<bean id="stu1" class="com.sgxy.entity.Student" lazy-init="true">
<!-- 调用无参的构造函数创建对象,再给对象的属性赋值 -->
<property name="id" value="10001"/>
<property name="age" value="20"/>
<property name="name" value="张明"/>
<property name="sex" value="男"/>
<property name="grade">
<!-- 写在property这个节点下面的Bean,叫内部Bean,作用域只在这个对象中。 -->
<bean id="g2" class="com.sgxy.entity.Grade">
<property name="gradeId" value="2"/>
<property name="gradeName" value="2年级"/>
</bean>
</property>
</bean>
使用效果:
如果想要配置文件中所有的Bean都延迟加载,则在beans节点中添加:
default-lazy-init="true"
Bean对象的销毁,默认是创建了Bean,这个对象一直驻扎在 IOC容器中,要等到IOC容器销毁才释放对象。可以在创建Bean时设置初始化方法和销毁的方法:
<bean id="xxx" class=“biz.OrderServiceBean" init-method="init" destroy-method="close"/>
七、Bean的注入
如果一个Bean中引用另一个Bean,则注入方式有内部Bean及ref引用外部Bean的注入。
内部Bean:
<!-- 配置对象,框架运行时,自动创建这个对象,存储在IOC容器中 -->
<bean id="stu1" class="com.sgxy.entity.Student">
<!-- 调用无参的构造函数创建对象,再给对象的属性赋值 -->
<property name="id" value="10001"/>
<property name="age" value="20"/>
<property name="name" value="张明"/>
<property name="sex" value="男"/>
<property name="grade">
<!-- 写在property这个节点下面的Bean,叫内部Bean,作用域只在这个对象中。 -->
<bean id="g2" class="com.sgxy.entity.Grade">
<property name="gradeId" value="2"/>
<property name="gradeName" value="2年级"/>
</bean>
</property>
</bean>
ref引用外部Bean
<bean id="g1" class="com.sgxy.entity.Grade">
<property name="gradeId" value="1"/>
<property name="gradeName" value="一年级"/>
</bean>
<bean id="stu3" class="com.sgxy.entity.Student" destroy-method="close">
<!-- 调用带参数的构造函数,可以根据索引或参数名称来进行赋值-->
<constructor-arg name="id" value="10003"/>
<constructor-arg name="name" value="肖月"/>
<constructor-arg name="sex" value="女"/>
<constructor-arg name="age" value="22" />
<property name="grade" ref="g1"/> <!-- ref引用外部Bean -->
</bean>
八、集合的装配:
1.list集合,在年级对象中有一个学生的list集合属性:
<property name="students">
<!-- 装配List集合 -->
<list>
<bean class="com.sgxy.entity.Student" p:id="101" p:sex="男" p:name="赵一" p:age="20"></bean>
<bean class="com.sgxy.entity.Student" p:id="102" p:sex="男" p:name="赵二" p:age="21"></bean>
<bean class="com.sgxy.entity.Student" p:id="103" p:sex="男" p:name="赵三" p:age="22"></bean>
<bean class="com.sgxy.entity.Student" p:id="104" p:sex="男" p:name="赵四" p:age="23"></bean>
<bean class="com.sgxy.entity.Student" p:id="105" p:sex="男" p:name="赵四" p:age="24"></bean>
</list>
</property>
2.set集合,在年级对象中有一个学生的set集合
<property name="students">
<!-- 装配set集合 -->
<set>
<bean class="com.sgxy.entity.Student" p:id="101" p:sex="男" p:name="赵一" p:age="20"></bean>
<bean class="com.sgxy.entity.Student" p:id="102" p:sex="男" p:name="赵二" p:age="21"></bean>
<bean class="com.sgxy.entity.Student" p:id="103" p:sex="男" p:name="赵三" p:age="22"></bean>
<bean class="com.sgxy.entity.Student" p:id="104" p:sex="男" p:name="赵四" p:age="23"></bean>
<bean class="com.sgxy.entity.Student" p:id="105" p:sex="男" p:name="赵四" p:age="24"></bean>
</set>
</property>
3.map集合,在年级对象中有一个学生的map集合,键是字符串,值是学生对象
<property name="students">
<!-- 装配map集合 -->
<map>
<entry key="101">
<bean class="com.sgxy.entity.Student" p:id="101" p:sex="男" p:name="赵一" p:age="20"></bean>
</entry>
<entry key="102">
<bean class="com.sgxy.entity.Student" p:id="102" p:sex="男" p:name="赵二" p:age="21"></bean>
</entry>
<entry key="103">
<bean class="com.sgxy.entity.Student" p:id="103" p:sex="男" p:name="赵三" p:age="22"></bean>
</entry>
</map>
</property>
九、对象装配:
@Service 建议用在Service层中的类上
@Component 如果这个类不属于业务层,持久层,控制器层,则用这个注解,表示是一个组件。
@Controller 建议用在控制器层中的类上
@Repository 建议用在持久层中的类上
注解的方式进行配置,必须在配置文件中进行处理:
<!-- 启用注解配置,-->
<context:annotation-config/>
<!-- 扫描包中的类,把有注解的类自动创建出对象,存储在IOC容器中 -->
<context:component-scan base-package="com.sgxy.service"/>
案例:
/**
* 添加如**解,表示这个类可以由Spring扫描到,并创建出对象
* @Service 建议用在Service层中的类上
* @Component 如果这个类不属于业务层,持久层,控制器层,则用这个注解,表示是一个组件。
* @Controller 建议用在控制器层中的类上
* @Repository 建议用在持久层中的类上
* 这几个注解的作用是一样的,有4个的原因是区别不同层里面的类而已。
*/
@Service
public class StudentSerivce {
//@Autowired //注解表示从IOC容器注入自动按类型来注入对象
@Resource(name="stu12") //这是按名称来装配对象
//@Autowired @Qualifier("stu1") //这两个注解结合使用也可以按名称来装配
Student student;
public Student getStudent() {
return student;
}
}
@Autowired //注解表示从IOC容器注入自动按类型来注入对象,可以写在属性前或set方法前。
@Resource(name="stu12") //这是按名称来装配对象
@Autowired @Qualifier("stu1") //这两个注解结合使用也可以按名称来装配。
上一篇: 详解pandas赋值失败问题解决
下一篇: MySQL常用分库分表方案汇总
推荐阅读