Spring - 依赖注入:简单的HelloWorld例子

控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心。 控制反转还有一个名字叫做依赖注入(Dependency Injection)。简称DI。          from baidu

一:依赖注入的方式 
  constructor-arg:通过构造函数注入。 
  property:通过setxx方法注入。 

1.GreetingService接口

1 package com.hezijian.spring;
2 
3 public interface GreetingService {
4     void sayGreeting();
5 }

2.GreetingServiceImpl负责实现接口

 1 package com.hezijian.spring;
 2 
 3 public class GreetingServiceImpl implements GreetingService{
 4     private String greeting;
 5     public GreetingServiceImpl(){}
 6     public GreetingServiceImpl(String greeting){
 7         this.greeting = greeting;
 8     }
 9     public void sayGreeting() {
10         System.out.println(greeting);
11     }
12     public void setGreeting(String greeting){
13         this.greeting = greeting;
14     }
15     
16 }

3.Spring配置文件hello.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">  
    <bean id="greetingService" 
        class="com.hezijian.spring.GreetingServiceImpl">
        <property name="greeting">
            <value>Buenos Dias!</value>
        </property>        
        <!--
            这里的<property>元素表示设置属性值。使用<property>,相当于我们告诉Spring容器通过调用Bean的setGreeting方法来设置其属性值。
            相当于以下代码所做的工作
            GreetingServiceImpl greetingService = new GreetingServiceImpl();
            greetingService.setGreeting(“Buenos Dias!”);
         -->
    </bean>
    
</beans>

Spring的配置文件是一个XML文件。

<beans>是这个简单XML文件的根元素,它也是任何Spring配置文件的根元素。

<bean>元素用来在Spring容器中定义一个类以及它的配置信息。这里,属性id表示greetingService Been的名字,class 属性表示Bean的全路径名。

另附:

xmlns ——是XML NameSpace的缩写,因为XML文件的标签名称都是自定义的,自己写的和其他人定义的标签很有可能会重复命名,而功能却不一样,所以需要加上一个namespace来区分这个xml文件和其他的xml文件,类似于java中的package。

xmlns:xsi ——是指xml文件遵守xml规范,xsi全名:xml schema instance,是指具体用到的schema资源文件里定义的元素所准守的规范。即/spring-beans-2.0.xsd这个文件里定义的元素遵守什么标准。

xsi:schemaLocation——是指,本文档里的xml元素所遵守的规范,schemaLocation 属性用来引用(schema)模式文档,解析器可以在需要的情况下使用这个文档对 XML 实例文档进行校验。它的值(URI)是成对出现的,第一个值表示命名空间,第二个值则表示描述该命名空间的模式文档的具体位置,两个值之间以空格分隔。

 

4.主类

package com.hezijian.spring;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class Hello {
    public static void main(String args[]) throws Exception{
        BeanFactory factory = 
                new XmlBeanFactory(new FileSystemResource("conf/hello.xml"));
        GreetingService greetingService = (GreetingService) factory.getBean("greetingService");
        //这里运用了向上转型
        greetingService.sayGreeting();
    }
}

 

BeanFactoty就是Spring的容器。

将XML文件载入容器后,main()方法调用BeanFactory的getBean()方法来得到问候服务器的引用。

转载于:https://www.cnblogs.com/hezijian/p/3479558.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值