第一步:添加架包
第二步:写一个简单的实列
package com.java.test; /** * @author nidegui * @create 2019-06-22 10:58 */ public class Helloworld { public void say(){ System.out.println("nihao spring"); } }
第三步:用bean
<?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="helloWorld" class="com.java.test.Helloworld"></bean> </beans>
第四步:测试
package com.java.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author nidegui * @create 2019-06-22 11:00 */ public class Test { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); //获取bean Helloworld helloWorld = (Helloworld) ac.getBean("helloWorld"); //获取bean的名字 helloWorld.say(); } }