Spring:HelloWorld:修订间差异
imported>Soleverlee |
imported>Soleverlee |
||
第18行: | 第18行: | ||
</bean> | </bean> | ||
</beans> | </beans> | ||
</ | </source> | ||
代码: | 代码: | ||
<source lang="java"> | <source lang="java"> |
2016年11月18日 (五) 03:23的最新版本
maven依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring}</version>
</dependency>
基于xml的配置
<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-4.3.xsd">
<bean id="helloService" class="com.riguz.activity.HelloServiceImpl" />
<bean id="app" class="com.riguz.activity.App">
<property name="service" ref="helloService"></property>
</bean>
</beans>
代码:
public class App {
private HelloService service;
public void setService(HelloService service) {
this.service = service;
}
public void sayHello() {
String msg = this.service.sayHello("Riguz");
System.out.println(msg);
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-beans.xml");
App app = context.getBean(App.class);
app.sayHello();
}
}