1. 程式人生 > >Spring基礎

Spring基礎

style set 分享 -- service sys info inf eas

一.簡單案例 打印hello spring

1.導包

 <!--beans-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.3.RELEASE</version>
        </dependency>
        <!--context-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.2.RELEASE</version>
        </dependency>

技術分享

2.創建HappyService類

技術分享

public class HappyService {

    private String info;

    public void work(){
        System.out.println("Hello "+info);
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }
}

3.創建applicationContext.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.xsd
     

    <!--IOC-->
    <bean id="HappyService" class
="cn.bdqn.service.HappyService"> <!--DI依賴註入--> <property name="info" value="Spring"></property> </bean> </beans>

4.測試類

 @Test
    public void test01(){
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        HappyService service=(HappyService)ac.getBean("HappyService");
        service.work();
    }

Spring基礎