Spring基础

一.简单案例   打印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();
    }
posted @ 2017-08-20 08:49  执念哈i  阅读(124)  评论(0编辑  收藏  举报