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

本文内容纲要: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();

}

}

没有annotation支持之前则需要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" xmlns:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="movieService" class="MarkDemo.service.DefaultMovieService"></bean>

<bean id="cinema" class="MarkDemo.service.Cinema">

<property name="movieService" ref="movieService"></property>

</bean>

</beans>

application.java的main方法

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();

//不使用annotation,使用配置文件。

System.out.println(ClassLoader.getSystemResource(""));

@SuppressWarnings("resource")

ApplicationContext appContext= new ClassPathXmlApplicationContext("classpath:/MarkDemo/service/applicationContext.xml");

Cinema cinema1=appContext.getBean("cinema",MarkDemo.service.Cinema.class);

cinema1.printMovieName();

}

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

本文内容总结:Spring-Context的注解实现依赖注入功能

原文链接:https://www.cnblogs.com/wancy86/p/spring-context.html

以上是 Spring-Context的注解实现依赖注入功能 的全部内容, 来源链接: utcz.com/z/362633.html

回到顶部