独立 Spring 应用程序
最后修改于 2020 年 7 月 13 日
在本教程中,我们将创建两个简单的 Java Spring 独立应用程序。我们将使用 NetBeans 来构建这些应用程序。
Spring 是一个流行的 Java 应用程序框架。它为企业应用程序编程提供了各种库和工具。它也是一个非常好的集成系统,有助于将各种企业组件粘合在一起。
Spring ApplicationContext 是提供应用程序配置的核心接口。ClassPathXmlApplicationContext 是 ApplicationContext 的一个实现,它从类路径中的 XML 文件加载配置定义。AnnotationConfigApplicationContext 创建一个从给定注解类派生 Bean 定义的新应用程序上下文。
<?xml version="1.0" encoding="UTF-8"?>
<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.zetcode</groupId>
<artifactId>SpringStandaloneEx2</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring-version>4.3.0.RELEASE</spring-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
</dependencies>
</project>
我们对这两个应用程序都使用此 Maven 构建文件。它包含必要的 Spring 依赖项。
使用 ClassPathXmlApplicationContext 的 Spring 应用程序
我们在 NetBeans IDE 中创建一个新的 Maven Java SE 应用程序。
在项目中,有四个文件:Message.java、Application.java、my-beans.xml 和 pom.xml。
package com.zetcode.bean;
public class Message {
private String message;
public void setMessage(String message){
this.message = message;
}
public String getMessage(){
return message;
}
}
Message 是我们应用程序中使用的简单 Java Bean。
<?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="mymessage" class="com.zetcode.bean.Message">
<property name="message" value="Hello there!"/>
</bean>
</beans>
我们将 Message 类变成一个 Spring Bean;它现在由 Spring 容器管理。我们还为 message 属性提供了一个值。my-beans.xml 位于 src/main/resources 子目录中。
package com.zetcode.main;
import com.zetcode.bean.Message;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Application {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("my-beans.xml");
Message obj = (Message) context.getBean("mymessage");
String msg = obj.getMessage();
System.out.println(msg);
}
}
Application 设置 Spring 应用程序。
ApplicationContext context =
new ClassPathXmlApplicationContext("my-beans.xml");
我们从 my-beans.xml 文件创建 ApplicationContext。
Message obj = (Message) context.getBean("mymessage");
我们从应用程序上下文中检索 Message Bean。
String msg = obj.getMessage(); System.out.println(msg);
我们调用 Bean 的 getMessage 方法并将消息打印到控制台。
Hello there!
这是应用程序的输出。
使用 AnnotationConfigApplicationContext 的 Spring 应用程序
在第二个示例中,我们将使用 AnnotationConfigApplicationContext 来创建 Spring ApplicationContext。
package com.zetcode.bean;
import org.springframework.stereotype.Component;
@Component
public class Message {
private String message = "Hello there!";
public void setMessage(String message){
this.message = message;
}
public String getMessage(){
return message;
}
}
Message Bean 用 @Component 注解进行装饰。Spring 会自动检测此类。
package com.zetcode.main;
import com.zetcode.bean.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan(basePackages = "com.zetcode")
public class Application {
public static void main(String[] args) {
ApplicationContext context
= new AnnotationConfigApplicationContext(Application.class);
Application p = context.getBean(Application.class);
p.start();
}
@Autowired
private Message message;
private void start() {
System.out.println("Message: " + message.getMessage());
}
}
这是主要的 Application 类。
@ComponentScan(basePackages = "com.zetcode")
使用 @ComponentScan 注解,我们告诉 Spring 在哪里查找组件。
ApplicationContext context
= new AnnotationConfigApplicationContext(Application.class);
ApplicationContext 是从注解创建的。
@Autowired
private Message message;
private void start() {
System.out.println("Message: " + message.getMessage());
}
使用 @Autowired 注解,Message Bean 被注入到 message 变量中。
在本教程中,我们创建了两个独立的 Spring 应用程序。第一个使用了 XML 文件,第二个依赖于注解。