Spring:HelloWorld

来自WHY42
imported>Soleverlee2016年11月18日 (五) 03:23的版本 (以“=maven依赖= <source lang="xml"> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring}</version...”为内容创建页面)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

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>  
</xml>
代码:
<source lang="java">
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();
    }
}

基于注解的配置