1.首先maven导入spring这几个依赖
<artifactId>spring-core</artifactId>
<artifactId>spring-beans</artifactId>
<artifactId>spring-context</artifactId>
<artifactId>spring-expression</artifactId>
加一个日志
<artifactId>commons-logging</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
2.什么是IOC和底层原理
IOC是通过控制反转,把创建对象和对象之间的调用交给spring来完成,而不用new对象,从而达到降低耦合度的目的。底层是通过xml解析,工厂模式,反射来实现的。
底层原理:
3.BeanFactory接口:
spring提供两种IOC容器实现方式:
(1):beanFactory:spring内部实用接口,一般是开发人员不会使用
(2):ApplicationContext:BeanFactory的子接口,开发者使用
ApplicationContext的两个实现类:
ClassPathXmlApplicationContext(“相对地址”)
FileSystemXmlApplicationContext(“绝对地址”)
IOC操作bean管理(spring创建对象,spring注入属性)的两种方式
第一种方式:基于xml配置文件
4.1基于xml配置文件创建对象
public class User {
public void add(){
System.out.println("add...");
}
}
bean.xml:
<bean id="user" class="com.lbl.spring5.User"> </bean>
测试
@Test
public void testadd(){
//加载spring配置文件
ApplicationContext context
// = new FileSystemXmlApplicationContext("E:\\study\\spring5_demo1\\src\\main\\resources\\bean1.xml");
= new ClassPathXmlApplicationContext("bean.xml");//相对路径下resources下
//绝对路径下,从磁盘开始写
User user = context.getBean("user", User.class);
System.out.println(user);
user.add();
}
4.2基于xml配置文件注入基本属性
注入属性有两种方法:(1):通过类生成属性的set方法
(2):通过类生成有参构造器
4.2.1:通过类生成属性的set方法:
Book.class:
public class Book {
private String bname;
private String bauthor;
public void setBname(String bname) {
this.bname = bname;
}
public void setBauthor(String bauthor) {
this.bauthor = bauthor;
}
public void testdemo(){
System.out.println("book");
}
}
bean.xml
<bean id="book" class="com.lbl.spring5.Book">
<!-- <!– 使用property完成属性注入 注意这里靠Book的set方法起作用 –>-->
<property name="bname" value="易筋经"></property><!-->name和Book的属性值bname对应-->
<property name="bauthor" value="lbl"></property><!--name和Book的属性值bauthor对应 靠set方法-->
</bean>
测试:
@Test
public void testbook(){
//加载spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");//相对路径下resources下
Book book = context.getBean("book", Book.class);
System.out.println(book);
book.testdemo();
}
4.2.2:通过类生成有参构造器:
Order.class:
public class Order {
public Order(String oname, String address) {
this.oname = oname;
this.address = address;
}
private String oname;
private String address;
}
public void testdemo(){
System.out.println("order");
}
bean.xml:
<bean id="order" class="com.lbl.spring5.Order">
<constructor-arg name="oname" value="电脑"></constructor-arg>
<constructor-arg name="address" value="China"></constructor-arg>
<!-- <constructor-arg index="0" value="213"></constructor-arg> 构造参数索引注入属性-->
</bean>
测试:
@Test
public void testorder(){
//加载spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");//相对路径下resources下
Order order = context.getBean("order", Order.class);
System.out.println(order);
order.testdemo();
}
4.3基于xml配置文件注入其他属性
4.3.1 注入的属性为null
<!-- null值 -->
<property name="属性名称"><null/></property>
4.3.2 注入的属性为特殊字符
<property name="属性名称" ><value><![CDATA[<<南京>>]]></value></property>
4.3.3 注入的属性为外部bean(对象)
首先是创建UserService类,UserDao作为其一个属性:
public class UserService {
//创建UserDao类型属性,生成set方法
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add() {
System.out.println("service add。。。。。");
//普通方法实现创建UserDao对象
// UserDao userDao = new UserDaoImpl();
// userDao.update();
userDao.update();
}
}
UserDao类:
public interface UserDao {
public void update();
}
和其实现类:
UserDaoImpl类:
public class UserDaoImpl implements UserDao{
@Override
public void update() {
System.out.println("dao update...");
}
}
bean.xml:
<!-- 创建service和dao对象创建 -->
<bean id="userService" class="com.lbl.spring5.service.UserService">
<!-- 注入userDao对象
name对应的值是属性名称,不是类名
-->
<property name="userDao" ref="userDaoimpl">
</property><!--可根据ref选择注入已注入的对象作为属性-->
</bean>
<bean id="userDaoimpl" class="com.lbl.spring5.dao.UserDaoImpl"></bean>
<bean id="userDaoimpl2" class="com.lbl.spring5.dao.UserDaoImpl"></bean>
测试类:
@Test
public void test(){
//加载spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");//相对路径下resources下
UserService service = context.getBean("userService", UserService.class);
System.out.println(service);
service.add();
}
4.3.4 注入的属性为内部bean(对象)
Employee类:
public class Employee {
private String ename;
private String gender;
private Dept dept;
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
}
public void print() {
System.out.println(ename);
System.out.println(gender);
dept.add();
}
}
Dept类:
public class Dept {
private String dname;
public void setDname(String dname) {
this.dname = dname;
}
public void add(){
System.out.println(dname);
}
}
bean.xml:
<!-- 内部bean-->
<bean id="employee" class="com.lbl.spring5.bean.Employee">
<property name="ename" value="lbl"></property>
<property name="gender" value="boy"></property>
<!-- <property name="dept" ref="dept"></property>-->
<property name="dept">
<bean id="dept" class="com.lbl.spring5.bean.Dept">
<property name="dname" value="研发部"></property>
</bean>
</property>
</bean >
测试类:
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");//相对路径下resources下
Employee employee = context.getBean("employee", Employee.class);
employee.print();
}
4.3.5:注入属性-级联赋值
<!--级联赋值 第一种-->
<!-- <bean id="employee" class="com.lbl.spring5.bean.Employee">-->
<!-- <property name="ename" value="lbl"></property>-->
<!-- <property name="gender" value="boy"></property>-->
<!-- <property name="dept" ref="dept"></property>-->
<!-- </bean >-->
<!-- <bean id="dept" class="com.lbl.spring5.bean.Dept">-->
<!-- <property name="dname" value="研发部"></property>-->
<!-- </bean >-->
<!-- 级联赋值 第二种 (需要employee中有get方法)-->
<bean id="employee" class="com.lbl.spring5.bean.Employee">
<property name="ename" value="lbl"></property>
<property name="gender" value="boy"></property>
<property name="dept" ref="dept"></property>
<property name="dept.dname" value="技术部"></property><!--需要employee中有get方法来得到值-->
</bean >
<bean id="dept" class="com.lbl.spring5.bean.Dept">
<property name="dname" value="研发部"></property>
</bean >
4.3.6:注入属性为集合
Stu类:
数组,List,Map,Set集合注入基本属性
public class Stu {
//数组类型属性
private String[] stu;
//list集合类型属性
private List<String> list;
//Map集合类型属性
private Map<String,String> map;
//Set集合类型属性
private Set<String> set;
public void setSet(Set<String> set) {
this.set = set;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setStu(String[] stu) {
this.stu = stu;
}
public void setCourselist(List<Course> courselist) {
this.courselist = courselist;
}
public void test(){
// System.out.println(Arrays.toString(stu));
// System.out.println(list);
// System.out.println(map);
// System.out.println(set);
System.out.println(courselist);
}
}
bean.xml
<!-- 数组类型属性注入-->
<bean id="stu" class="com.lbl.collection.Stu">
<property name="stu" >
<array>
<value>java课程</value>
<value>数据库课程</value>
</array>
</property>
<!-- list类型属性注入-->
<property name="list" >
<list>
<value>lbl</value>
<value>ls</value>
</list>
</property>
<!-- map类型属性注入-->
<property name="map" >
<map>
<entry key="JAVA" value="java"></entry>
<entry key="C" value="c"></entry>
</map>
</property>
<!-- set类型属性注入-->
<property name="set" >
<set>
<value>Mysql</value>
<value>Redis</value>
</set>
</property>
</bean>
注入集合为的数据类型是对象:
bean.xml
<bean>
<property name="courselist">
<list>
<ref bean="course1"></ref>
<ref bean="course2"></ref>
</list>
</property>
</bean>
<!--创建多个course对象-->
<bean id="course1" class="com.lbl.collection.Course">
<property name="cname" value="数学"></property>
</bean>
<bean id="course2" class="com.lbl.collection.Course">
<property name="cname" value="英语"></property>
</bean>
4.3.7:提取集合注入部分:
<!--提取list集合类型属性注入-->
<util:list id="bookList">
<value>人间失格</value>
<value>百年孤独</value>
<value>美国大城市的死与生</value>
</util:list>
<!--提取list集合了下属性注入使用-->
<bean id="book" class="com.lbl.collection.Book" scope="prototype"><!--默认单实例且在加载配置文件时就已经创建好了-->
<!--多实例是在context.getBean方法时才创建对象-->
<property name="list" ref="bookList"></property>
</bean>
: