1. 导入依赖
直接导入springboot,会将spring所需要所有依赖一并导入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
2. bean.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
</beans>
3. bean创建对象
<bean id="hello" class="com.yang.pojo.Hello">
<property name="str" value="helloWorld"/>
</bean>
4. 测试
public class HelloTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Hello hello = context.getBean("hello", Hello.class);
System.out.println(hello.getStr());
}
}