2002 Rod Johnon <Expoer One-on-one j2eedevelopment and Design>
Spring 2003,IOC Aop
Spring data,spring boot,spring cloud,spring framework,spring social
IOC:控制反转(DI:依赖注入)
1.搭建Spring环境
下载jar
http://maven.springframework.org/release/org/springframework/spring/:spring-framework-4.3.9.RELEASE-dist.zip
开发spring至少需要使用的jar(5个+1个):
spring-aop.jar 开发AOP特性时需要的JAR
spring-beans.jar 处理Bean的jar <bean>
spring-context.jar 处理spring上下文的jar <context>
spring-core.jar spring核心jar
spring-expression.jar spring表达式
三方提供的日志jar
commons-logging.jar 日志
2.编写配置文件
为了编写时有一些提示、自动生成一些配置信息:
方式一:增加sts插件
可以给eclipse增加支持spring的插件:spring tool suite(https://spring.io/tools/sts/all)
下载springsource-tool-suite-3.9.4.RELEASE-e4.7.3a-updatesite.zip,然后在Eclipse中安装
方式二:
直接下载sts工具(相当于一个集合了Spring tool suite的Eclipse):https://spring.io/tools/sts/
新建:bean configuration -> applicationContext.xml
3.开发Spring程序(IOC)
ApplicationContext conext = new ClassPathXmlApplicationContext(“applicationContext.xml”) ;
//执行从springIOC容器中获取一个id为student的对象
Student student = (Student)conext.getBean(“student”) ;
可以发现,springioc容器帮我们new了对象,并且给对象赋了值
SpringIOC发展史:
1.
Student student = new Student();
student.setXxx();
2.
简单工厂
3.
ioc(超级工厂)
IOC(控制反转)也可以称之为DI(依赖注入):
控制反转:将创建对象、属性值的方式进行了反转,从new、setXxx()反转为了从springIOC容器getBean()
依赖注入:将属性值注入给了属性,将属性注入给了bean,将bean注入给了ioc容器
总结:ioc/di,无论要什么对象,都可以直接去springioc容器中获取,而不需要自己操作(new/setXxx())
因此之后的ioc分为2步:1 先给springioc中存放对象并赋值 2 拿
DI:依赖注入
Teacher
Course:cname teacher
IOC容器赋值:
如果是简单类型(8个基本+String),value=
如果是对象类型,ref=“需要引用的id值”,因此实现了对象与对象之间的依赖关系
依赖注入3种方式:
1.set注入:通过setXxx()赋值
赋值,默认使用的是set方法();
依赖注入底层是通过反射实现的。
<property…>
2.构造器注入:通过构造方法赋值
<constructor-arg value="ls" type="String" index="0" name="name"></constructor-arg>
需要注意:如果<constructor-arg>
的顺序与构造方法参数的顺序不一致,则需要通过type或者index或name指定。
3.p命名空间注入
引入p命名空间:xmlns:p=“http://www.springframework.org/schema/p”
<bean id="course" class="org.lanqiao.entity.Course" p:courseHour="300" p:courseName="hadoop" p:teacher-ref="teacher">
简单类型:
p:属性名=“属性值”
引用类型(除了String外):
p:属性名-ref=“引用的id”
注意多个p赋值的时候要有空格。
注意:
无论是String还是Int/short/long,在赋值时都是value=“值” ,
因此建议此种情况需要配合name/type进行区分
示例:注入各种集合数据类型: List/Set/map/properties
set、list、数组 各自都有自己的标签<set> <list> <array>
,但是也可以混着用
<bean id="collectionDemo" class="org.lanqiao.entity.AllCollectionType" ><!-- 通过set方式赋值 <property name="listElement"><list><value>足球</value><value>篮球</value><value>乒乓球</value></list></property>--><constructor-arg name="listElement"><list><value>足球xx</value><value>篮球xx</value><value>乒乓球xxx</value></list></constructor-arg><property name="arrayElement"><!-- <array><value>足球1</value><value>篮球1</value><value>乒乓球1</value></array>--><list><value>足球1</value><value>篮球1</value><value>乒乓球1</value></list></property><property name="setElement"><!-- <set><value>足球2</value><value>篮球1</value><value>乒乓球2</value></set>--><list><value>足球2</value><value>篮球1</value><value>乒乓球2</value></list></property><property name="mapElement"><map><entry><key><value>foot</value></key><value>足球3</value></entry><entry><key><value>basket</value></key><value>篮球3</value></entry><entry><key><value>pp3</value></key><value>乒乓球</value></entry></map></property><property name="propsElement"><props><prop key="foot4">足球4</prop><prop key="basket4">篮球4</prop><prop key="pp4">乒乓球4</prop></props></property></bean>
给对象类型赋值null :
<property name="name" > <null/> -->注意 没有<value></property>
赋空值 “”
<property name="name" > <value></value> </property>或者<property name="name" value="" >
在ioc中定义bean的前提:该bean的类必须提供了无参构造
自动装配(只适用于ref类型 ):约定优于配置
自动装配:
<bean … class=“org.lanqiao.entity.Course” autowire=“byName|byType|constructor|no” > byName本质是byId
byName:自动寻找:其他bean的id值=该Course类的属性名
byType: 其他bean的类型(class)是否与该Course类的ref属性类型一致(注意,此种方式必须满足:当前Ioc容器中只能有一个Bean满足条件)
constructor:其他bean的类型(class)是否与该Course类的构造方法参数的类型一致;此种方式的本质就是byType
可以在头文件中 一次性将该ioc容器的所有bean统一设置成自动装配:
<beans xmlns="http://www.springframework.org/schema/beans"
...
default-autowire="byName">
自动装配虽然可以减少代码量,但是会降低程序的可读性,使用时需要谨慎。
使用注解定义bean:通过注解的形式将bean以及相应的属性值放入ioc容器
<context:component-scan base-package="org.lanqiao.dao">
</context:component-scan>
Spring在启动的时候,会根据base-package在该包中扫描所有类,查找这些类是否有注解@Component(“studentDao”),如果有,则将该类加入spring Ioc容器。
@Component细化:
dao层注解:@Repository
service层注解:@Service
控制器层注解:@Controller