Spring-Context的注解实现依赖注入功能

使用Spring-Context的注解实现依赖注入功能.

 

Demo要点:

本例子中主要使用Annotation功能来实现对MoviceService的注入。我们将Cinema.java的头部标注为@Component说明该类交由Spring托管。而Cinema.java中的属性MoviceService标注为@Autowired,则Spring在初始化Cinema类时会从Application Context中找到类型为MovieService的Bean,并赋值给Cinema。在Application.java中我们声明了一个类型为MovieService的Bean。并且标注Application.java为@Configuration,这是告诉Spring在Application.java中定义了一个或多个@Bean方法,让Spring容器可以在运行时生成这些Bean。@ComponentScan则会让Spring容器自动扫描当前package下的标有@Component的class,这些class都将由Spring托管。此时 就要注意Application.java需要在一个较为上层的包,不然会报错找不到类。

 

项目结构:

Pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"    
    
    >
    <modelVersion>4.0.0</modelVersion>
    <groupId>MarkDemo</groupId>
    <artifactId>MarkDemo.SpringContext</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build />
    
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.1.RELEASE</version>
        </dependency>
    </dependencies>
    
</project>

 

MovieService.java

package MarkDemo.service;


public interface MovieService {

    String getMovieName();
}

 

DefaultMovieService.java

package MarkDemo.service;

public class DefaultMovieService implements MovieService {

    public String getMovieName() {
        return "INTO THE WILD, HELLO SPRING CONTEXT";
    }
}

 

Cinema.java

package MarkDemo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class Cinema {
    
    @Autowired
    private MovieService movieService;

    public void printMovieName() {
        System.out.println(movieService.getMovieName());
    }

    public MovieService getMovieService() {
        return movieService;
    }

    public void setMovieService(MovieService movieService) {
        this.movieService = movieService;
    }
    
}

 

Application.java

package MarkDemo.service;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
public class Application {

    @Bean
    public MovieService getMovieService() {
        return new DefaultMovieService();
    }

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Application.class);
        Cinema cinema = applicationContext.getBean(Cinema.class);
        cinema.printMovieName();
        applicationContext.close();

    }

}

 

 

参考文章:http://www.2cto.com/kf/201403/284656.html

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。