Spring 基本配置
1.写一个 applicationContext.xml, schema取自: ${spring}/docs/spring-framework-reference/htmlsingle/index.html
<?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"> <!-- 配置 spring 的 bean --> <bean id="greetingService" class="service.GreetingService"> <property name="greeting"> <value>hello world</value> </property> </bean> </beans>
2.一个 POJO
public class GreetingService { /* 属性 */ private String greeting; public String getGreeting() { return greeting; } public void setGreeting(String greeting) { this.greeting = greeting; } }
3.使用
// 初始化 应用 上下文
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取 Bean
GreetingService gService = (GreetingService) ac.getBean("greetingService");
// 使用
System.out.println(gService.getGreeting());