spring-1
beans.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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hello" class="com.kuang.pojo.Hello">
<property name="str" value="hello"/>
</bean>
</beans>
调用:
ApplicationContext context= new ClassPathXmlApplicationContext("beans.xml");
Hello hello = (Hello) context.getBean("hello");
ApplicationContext为一个上下文类,可以通过ClassPathXmlApplicationContext()获取一个上下文对象(就是一个xml的操作对象,参数为对应的xml文件)
context.getbean()方法可以获取xml文件中指定id名的类
<bean id="hello" class="com.kuang.pojo.Hello">
property可以进行无参构造的赋值
<property name="str" value="hello"/>
ref的作用是应用已经bean定义的类