项目管理工具:maven
denpendecy:spring-context
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>
编写bean的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-2.5.xsd">
<bean id="sysoAction" class="spring.sysoAction">
<property name="ss" ref="syso"></property>
</bean>
<bean id="syso" class="spring.syso"></bean>
<!-- more bean definitions go here -->
</beans>
syso类
package spring;
public class syso {
public void test(){
System.out.println("test");
}
}
sysoAction类
package spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class sysoAction {
syso ss;
public syso getSs() {
return ss;
}
public void setSs(syso ss) {
this.ss = ss;
}
public void test(){
ss.test();
}
public static void main(String [] args){
ApplicationContext ctx=new ClassPathXmlApplicationContext("SpringBeans.xml");
sysoAction s=(sysoAction) ctx.getBean("sysoAction", sysoAction.class);
s.test();
}
}
执行成功后会输出 test