1.基于xml方式创建对象

<!--配置User类对象的创建  -->
<bean id="user" class="com.at.spring5.User"></bean>

(1)在spring配置文件中,使用bean标签,标签里面添加对应属性,就可以实现对象创建

(2)在bean标签有很多属性
*id属性:唯一标识
*class属性:包类路径

(3)创建对象时,默认执行无参构造方法

2.基于xml方式注入属性
(1)DI:依赖注入->注入属性
第一种注入方式:使用set方法注入
创建Book类

package com.at.spring5;

public class Book {
private String bName;
private String author;

public void setbName(String bName) {
this.bName = bName;
}

public void setAuthor(String author) {
this.author = author;
}

public void testDemo(){
System.out.println(bName+"::"+author);
}
}

配置bean1.xml文件

<?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">

<!--配置User类对象的创建 -->
<bean id="book" class="com.at.spring5.Book">
<property name="bName" value="易筋经"></property>
<property name="author" value="八段锦"></property>
</bean>
</beans>

创建Test类进行测试

public class TestSpring5 {
@Test
public void testAdd(){
//1.加载spring配置文件------》在该步骤创建对象
ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
//2.获取配置创建对象
Book book = context.getBean("book", Book.class);
book.testDemo();
}
}

第二种注入方式:使用有参构造注入
创建Orders类

/**
* 使用有参构造注入
*/
public class Orders {

private String name;
private String address;

public Orders(String name,String address) {
this.name = name;
this.address=address;
}
public void orderTest(){
System.out.println(name+":"+address);
}
}

配置bean1.xml文件

<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">

<!--配置User类对象的创建 -->
<!--使用时set方法注入-->
<!-- <bean id="book" class="com.at.spring5.Book">-->
<!-- <property name="bName" value="易筋经"></property>-->
<!-- <property name="author" value="八段锦"></property>-->
<!-- </bean>-->
<!--使用有参构造注入-->
<bean id="orders" class="com.at.spring5.Orders">
<constructor-arg name="name" value="ssh"></constructor-arg>
<constructor-arg name="address" value="ssm"></constructor-arg>
</bean>
</beans>

测试类

public class TestSpring5 {
@Test
public void testAdd(){
//1.加载spring配置文件------》在该步骤创建对象
ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
//2.获取配置创建对象
Orders orders = context.getBean("orders", Orders.class);
orders.orderTest();
}
}

第三种注入方式(不常用,了解即可)
使用名称空间实现注入