Spring Framework项目创建

再resource目录下创建applicationContext.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">
       <bean id="video" class="com.example.demo01.domain.Video">
       <property name="id" value="25"/>
      <property name="title" value="hello world"/>
</bean>
</beans>

再创建入口类
public class App {
public static void main(String[] args) {
//引用配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取配置文件中id为"video"的bean
Video video = (Video) applicationContext.getBean("video");

System.out.println(video.getTitle());
}
}

其中 Video类中只有简单的id、title及其getter,setter方法

最后再运行入口类,成功打印hello world!
posted @ 2021-11-22 23:12  智慧搬运工  阅读(96)  评论(0)    收藏  举报