spring初体验笔记,spring的使用

//创建容器,
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
WelcomeService ws=(WelcomeService)ac.getBean("ws");
ws.sayHello();

在创建容器的时候参考beans.xml文件将对象组装好,不需要手动new一个对象了,

以下是beans.xml文件的内容,已经给定message的value值

<?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="ws" class="com.tian.springdemo.service.WelcomeService">
<property name="message" value="hello spring"/>
</bean>
</beans>

spring将bean的信息放到xml文件中,通过xml配置内容,在spring中直接实例化,通过getbean方法获得bean

猜你喜欢

转载自www.cnblogs.com/hbigdata/p/10886679.html