zoukankan      html  css  js  c++  java
  • 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()方法来得到问候服务器的引用。

  • 相关阅读:
    同步gitlab与github
    配置hosts快速访问GitHub
    Linux下Julia安装
    LATEX图片位置
    IPOPT安装
    sqlplus传入shell变量
    users表空间满导致应用无法连接
    坏块修复 ORA-00701
    Oracle中INITRANS和MAXTRANS参数(转)
    DBMS_ROWID包的使用
  • 原文地址:https://www.cnblogs.com/hezijian/p/3479558.html
Copyright © 2011-2022 走看看