1)最终address:
查找路径比较长,很有趣
JFrog
原始步骤1)进入 spring.io,点击右上角黑色标记边的标记
2)进入 git,找到 Binaries下面的 Spring Framework Artifacts
3)进一步找到Downlaoding a Distribution,下面有 https://repo.spring.io
4)x选择artifactory,spring
复制url to file
下载最新版 -dist.zip
2.新建项目
四个核心jar:bean core context expression
还缺一个
下载binaries zip解压
复制5个jar到这里lib
导入到项目
src 下创建XML文件
IOC原理
IOC基于ICO容器完成,其底层就是bean工厂
xml+反射
Application context类结构
ctrl+h
上面有FileSystemXML和classPathXml,前者指物理路径,后者指相对于src路径
对比beanFactory
,在上面ctrl+H 打开
绿色是接口,蓝色是实现类
其下比较重要的接口是
IOC操作Bean管理
1)创建对象“编写xml文件
2)注入属性:不用setter/getter设置属性
两种实现方式:
1)基于XML配置实现
a)对象创建
b)
2)基于注解实现
基于XML配置创建对象
2.1)在XML中使用bean标签,并配置对应属性
<bean id="user" class="com.i7i8i9.spring5.User"></bean>
2.2) id属性:获取对象的唯一标记
class属性:创建对象所在的类的全路径
name属性:里面可以有符号比如/,以前用于struts1 现在不太用
3)默认调用无参构造方法
如果只有有参构造函数,那么
No default constructor found; nested exception is java.lang.NoSuchMethodException:
3)基于XML注入属性
DI:依赖注入,需要在创建对象基础上完成
第一种注入:set方法
第二种:通过有参构造函数注入
第三种:通过xml
<bean id="book" class="com.i7i8i9.spring5.Book"> <property name="bName" value="八卦"></property> <property name="bAuthor" value="杜甫"></property> </bean>
测试
public class TestBook1 { @Test public void testBook1(){ BeanFactory context=new ClassPathXmlApplicationContext("bean1.xml"); Book book=context.getBean("book", Book.class); System.out.println(book.getbName()); }
//bean1.xml是指在src下的目录
ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml"); Book book=context.getBean("book", Book.class); System.out.println(book.getbName());
3)使用有参构造注入
<bean id="orders" class="com.i7i8i9.spring5.Orders"> <constructor-arg name="oName" value="订单名称"></constructor-arg> <constructor-arg name="address" value="东京"></constructor-arg>
<!-- 还可以通过索引值来赋值--> <constructor-arg index="0" value="ff"></constructor-arg> </bean>
测试:
@Test public void TestOrders(){ ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml"); Orders orders= context.getBean("orders",Orders.class); orders.ordersTest(); }
4)p名称空间注入
针对 xml property标签的简化
给xml文件添加p约束
,也叫p空间
xmlns:p="http://www.springframework.org/schema/p"
<bean id="book" class="com.i7i8i9.spring5.Book" p:bName="唐诗三百首" p:bAuthor="王维"> </bean>
5)设置其他类型
I. 字面量:null 特殊符号
xml中:
<property name="bAddress"> <null></null> </property>
II. 转义
<property name="bAddress" value="<>南京"></property>
1) <> 即<>
2)特殊符号处理方法2:value标签下增加<![[]]>
第二个中括号前写CDATA , 里面就是要呈现的值<<>>
<property name="bAddress"> <value> <![CDATA[<<南京>>]]> </value> </property>
III.内部外部bean和级联赋值
1)外部bean:如通过service调用dao方法
-> xml中定义两个bean,属性使用 ref
<bean id="userSeivice" class="com.i7i8i9.spring5.service.UserService">
<!-- names是属性名称,非字面量不能用data,只能用ref,内容就是xml定义bean的id值-->
<property name="userDao" ref="userDao1"></property>
</bean>
<!-- 必须用实现类,不能用接口放在class里面-->
<bean id="userDao1" class="com.i7i8i9.spring5.dao.UserDaoImpl"></bean>
2)内部beanneibu bean就是xml中bin里面有bin
举例:部门与员工就是1对多,通过在员工类中增加部门属性实现
public class Department {
private String dName;
public void setdName(String dName) {
this.dName = dName;
}
}
public class Employee {
private String eName;
private String gender;
public void setDepartment(Department department) {
this.department = department;
}
//员工所属部门
private Department department;
public void seteName(String eName) {
this.eName = eName;
}
public void setGender(String gender) {
this.gender = gender;
}
}
设置xml,在bean里面加bean,把department认为是employee的属性
<bean id="employee" class="com.i7i8i9.spring5.bean.Employee">
<property name="eName" value="杜甫"></property>
<property name="gender" value="男"></property>
<property name="department">
<bean id="department" class="com.i7i8i9.spring5.bean.Department">
<property name="dName" value="财务部"></property></bean>
</property>
</bean>
3)级联1:xml中使用外部bin +ref
<bean id="department" class="com.i7i8i9.spring5.bean.Department">
<!-- 级联赋值方式1:外部bin-->
<property name="dName" value="财务部"></property></bean>
<bean id="employee" class="com.i7i8i9.spring5.bean.Employee">
<property name="eName" value="杜甫"></property>
<property name="gender" value="男"></property>
<property name="department" ref="department"></property>
</bean>
方式2:表达式形式:
department.dName
java类需要设置针对对象属性设置get方法
XML
<bean id="employee" class="com.i7i8i9.spring5.bean.Employee">
<property name="eName" value="李清照"></property>
<property name="gender" value="女"></property>
<property name="department" ref="department"></property>
<property name="department.dName" value="信息部"></property>
</bean>
java类需要设置针对对象属性设置get方法
public Department getDepartment() {
return department;
}
4)注入集合类
注入数组
类:
public class Students {
private String[] courses;
private List<String> list;
private Map<String,String> maps;
private Set<String> sets;
public void setSets(Set<String> sets) {
this.sets = sets;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public void setCourses(String[] courses) {
this.courses = courses;
}
}
XML:
<bean id="students" class="com.i7i8i9.spring5.collectiontype.Students">
<!-- 数组,用list或array-->
<property name="courses">
<array>
<value>英语</value>
<value>音乐</value>
</array>
</property>
<!-- 集合-->
<property name="list">
<list>
<value>李白</value>
<value>杜甫</value>
</list>
</property>
<!-- Map-->
<property name="maps">
<map>
<entry key="Cat" value="cat"></entry>
<entry key="Dog" value="dog"></entry>
</map>
</property>
<!-- Set-->
<property name="sets">
<set>
<value>China</value>
<value>USA</value>
</set>
</property>
</bean>
在集合里面设置对象值
1)在xml中创建多个对象,再把对象写入ref
<property name="coursesList">
<list>
<ref bean="course1"></ref>
<ref bean="course2"></ref>
<ref bean="course3"></ref>
<ref bean="course4"></ref>
</list>
</property>
</bean>
<!-- 定义多个课程对象-->
<bean id="course1" class="com.i7i8i9.spring5.collectiontype.Courses">
<property name="cNames" value="法语"></property>
</bean>
<bean id="course2" class="com.i7i8i9.spring5.collectiontype.Courses">
<property name="cNames" value="西班牙语"></property>
</bean>
<bean id="course3" class="com.i7i8i9.spring5.collectiontype.Courses">
<property name="cNames" value="德语"></property>
</bean>
<bean id="course4" class="com.i7i8i9.spring5.collectiontype.Courses">
<property name="cNames" value="韩语"></property>
</bean>
把集合注入部分提取出来
1)XML引入名称空间util
xmlns:utl="http://www.springframework.org/schema/util"
同时,xsi中也要复制一个http,并把里面的beans改成util,一共三处
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:utl="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
">
2)使用util标签
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:utl="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
">
<!-- 提取-->
<utl:list id="bookList">
<value>苏州</value>
<value>杭州</value>
<value>扬州</value>
</utl:list>
<!-- 注入-->
<bean id="book" class="com.i7i8i9.spring5.collectiontype.Book">
<property name="list" ref="bookList">
</property>
</bean>
</beans>