用Spring的三种配置风格实现依赖注入

题外话

不知不觉毕业出来工作快三年了…Spring用了这么久,好好整理一下,查漏补缺。会的不会的用的好的不会用的都做下记录。

关于依赖注入

IoC(Inversion of Control ,控制反转) 是一种解耦的设计思想,实现IOC的最常见方式叫依赖注入(Dependency Injection,DI),还有一种叫依赖查找(Dependency Lookup,DL)。DI是Spring IoC的一种实现方式。Spring的官网说明文档直接提到:

IoC is also known as dependency injection (DI).

在程序开发过程中,面向抽象编程能让代码更具有灵活性。面向抽象编程会产生类的依赖,spring IoC相当于一个管理对象的容器,帮我们优雅地完成了类之间的依赖。
此外,spring有三种配置元数据方式,这里也顺便记一下。官网关于三种配置风格的说明:

  • Annotation-based configuration:

Spring 2.5 introduced support for annotation-based configuration metadata.
spring2.5引入了对基于注释的配置元数据的支持。

  • Java-based configuration:

Starting with Spring 3.0, many features provided by the Spring JavaConfig project became part of the core Spring Framework. Thus, you can define beans external to your application classes by using Java rather than XML files.
从Spring 3.0开始,很多Spring JavaConfig项目提供的特性成为了Spring核心框架的一部分。因此,不用XML文件形式的话,你可以使用Java来定义应用程序类外部的bean。

  • XML-based
    官方文档在“The IoC Container”章节大部分例子都用到XML,就没多赘述了。

注入的方法

spring有两种注入方法:构造方法、set方法。最近听一个视频课的老师提到,Spring3还有一种接口注入,不过因为配置不人性化,现在的spring已经没有了。

构造方法注入

XML-based方式配置

定义一个接口IndexDao

public interface IndexDao {
    public void test();
}

定义一个IndexDaoImpl类实现IndexDao接口

public class IndexDaoImpl implements IndexDao{
    public void test() {
        System.out.println("test");
    }
}

定义一个IndexService类,依赖IndexDao

public class IndexService {
    private IndexDao dao;
    public IndexService(IndexDaoImpl dao) {
        this.dao = dao;
    }
    public void service() {
        dao.test();
    }
}

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="service" class="com.tangyuan.springIOCdao.IndexService">
        <constructor-arg ref="dao"></constructor-arg>
    </bean>
    <bean id="dao" class="com.tangyuan.springIOCdao.IndexDaoImpl" >
    </bean>
</beans>

用ClassPathXmlApplicationContext简单测一下

import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext classPathXmlApplicationContext
                = new ClassPathXmlApplicationContext("classpath:spring.xml");
        IndexService service = (IndexService) classPathXmlApplicationContext.
                getBean(IndexService.class);
        service.service();
    }
}

Java-based方式配置

The @Bean annotation is used to indicate that a method instantiates, configures, and initializes a new object to be managed by the Spring IoC container. For those familiar with Spring’s <beans/> XML configuration, the @Bean annotation plays the same role as the <bean/> element.
@Bean注释用于指示方法实例化、配置和初始化要由Spring IoC容器管理的新对象。对于熟悉XML<beans/>标签配置的人来说,@Bean注释的作用与<bean/> 元素相同。

不需要XML文件了,加个配置类,起到的效果和XML文件一样:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SpringConfig {
    @Bean
    public IndexService indexService() {
        return new IndexService(new IndexDaoImpl());
    }
}

测试一下:

public class test {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationConfigApplicationContext
                = new AnnotationConfigApplicationContext(SpringConfig.class);
        IndexService service = annotationConfigApplicationContext.getBean(IndexService.class);;
        service.service();
    }
}

Annotation-based方式配置

如果要用XML文件配置的话,配置文件要写<context:component-scan>

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <!--这个是用来支持注解的,以前必须要写,现在不需要了,直接写context:component-scan的就行-->
    <!--<context:annotation-config></context:annotation-config>-->
    <context:component-scan base-package="com"></context:component-scan>
</beans>

在IndexDaoImpl和IndexService加上注解:

import org.springframework.stereotype.Repository;
@Repository
public class IndexDaoImpl implements IndexDao{
    public void test() {
        System.out.println("test");
    }
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class IndexService {
    @Autowired
    private IndexDao dao;
    public IndexService(IndexDaoImpl dao) {
        this.dao = dao;
    }
    public void service() {
        dao.test();
    }
}

如果是用Java-based方式配置的话,SpringConfig写上@ComponentScan注解执行扫描:

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.ComponentScan;
@Configurable
@ComponentScan(value = "com.tangyuan.springIOCdao")
public class SpringConfig {
    public IndexService indexService() {
    }
}

set方法注入

不同配置下使用set方法注入跟上面差不多,这里只对set方法注入做个记录~

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class IndexService {
    @Autowired
    private IndexDao dao;
    public void setDao(IndexDao dao) {
        this.dao = dao;
    }
    public void service() {
        dao.test();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值