欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring5入门

程序员文章站 2022-03-05 10:25:17
...

使用Spring DI实现的入门案例:松耦合、可扩展的消息渲染程序

项目目录结构

Spring5入门

代码(基于XML配置的实现)

0、pom引入

<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>com.jay</groupId>
  <artifactId>chap02</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
  	<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-context</artifactId>
	    <version>5.1.6.RELEASE</version>
	</dependency>
  </dependencies>
</project>

1、接口

package com.jay.decoupled;

public interface MessageProvider {
	String getMessage();
}


public interface MessageRender {
	void render();

	/* MessageProvider是MessageRender的依赖项 */
	void setMessageProvider(MessageProvider provider);
	MessageProvider getMessageProvider();
}

2、实现类

public class HelloWorldMessageProvider implements MessageProvider{
	@Override
	public String getMessage() {
		return "Hello World!!!";
	}
}

public class HelloWorldMessageRender implements MessageRender{
	
	public MessageProvider messageProvider;
	@Override
	public void render() {
		if (messageProvider==null) {
			throw new RuntimeException("you must set the property of messageProvider of class"+HelloWorldMessageRender.class.getName());
		}
		System.out.println(messageProvider.getMessage());
	}
	@Override
	public void setMessageProvider(MessageProvider provider) {
		messageProvider = provider ;
	}
	@Override
	public MessageProvider getMessageProvider() {
		return this.messageProvider;
	}
}

3、入口

public class HelloWorldSpringDI {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/app-context.xml");
		MessageRender render = ctx.getBean("render",MessageRender.class);
		render.render();
	}
}

4、配置文件

<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 定义Bean -->
	<bean id="provider" class="com.jay.decoupled.HelloWorldMessageProvider"></bean>
	<!-- p:messageProvider-ref="provider" 告诉spring,bean的属性messageProvider应该与另一个bean一起注入 -->
	<bean id="render" class="com.jay.decoupled.HelloWorldMessageRender" p:messageProvider-ref="provider"></bean>
</beans>

代码(基于注解的实现)

1、配置类

/*表明这是一个配置类*/
@Configuration
public class HelloWorldConfiguration {
	@Bean(name="provider")
	public MessageProvider	provider() {
		return new HelloWorldMessageProvider();
	}
	@Bean(name="render")
	public MessageRender render() {
		HelloWorldMessageRender messageRender = new HelloWorldMessageRender();
		messageRender.setMessageProvider(provider());
		return messageRender;
	}
}

2、入口

public class HelloWorldSpringDIAnnotation {
	public static void main(String[] args) {
		ApplicationContext ctx = new AnnotationConfigApplicationContext(HelloWorldConfiguration.class);
		MessageRender render = ctx.getBean("render",MessageRender.class);
		render.render();
	}
}

结果输出:

Spring5入门