对象是由Spring容器创建和装配的
Spring通过Xml配置bean(相当于之前的 类 对象名 = new 类)
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloSpring" class="com.cyy.Pojo.HelloSpring">
<property name="str" value="HelloSpring"/>
</bean>
<--
ref引用Spring中创建好的对象
value是直接给对象赋值
-->
</beans>
调用并生成JAVABean
//获取Spring对象的上下文
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
HelloSpring helloSpring = (HelloSpring) context.getBean("helloSpring");
System.out.println(helloSpring.toString());
原文:https://www.cnblogs.com/8023cyy/p/14887709.html