组团学

Spring 注解驱动设计模式

阅读 (6557739)

1、Spring @Enable模块驱动概述

Spring Framework3.1 是一个其有里程碑意义的发行版本,从此版本开始,Spring Framework开始支持"@Enable模块驱动"。所谓"模块"是指具备相同领域的功能组件集合,例如Web MVC模块、AspectJ模块等。

2、理解@Enable模块驱动

@Enable模块驱动在后续的Spring Framework、Spring Boot和Spring Cloud中一以贯之,这种模块化的Annotation均以@Enable作为前缀,例如:@EnableWebFlux、@EnableWebMVC等。@Enable模块驱动的意义在于简化装配步骤,实现"按需装配",同时屏蔽组件集合装配的细节。

查看@EnableWebFlux注解

@Retention(RetentionPolicy.RUNTIME) //元注解,表示注解不仅保存在class文件,并且jvm加载class文件之后,仍然存在 @Target({ElementType.TYPE}) //表示此注解的标识范围为接口、类、枚举 @Documented //表示该注解会被javadoc工具记录 @Import({DelegatingWebFluxConfiguration.class}) //通过快速导入的方式实现把实例加入spring的IOC容器中 public @interface EnableWebFlux { } @Configuration( proxyBeanMethods = false ) public class DelegatingWebFluxConfiguration extends WebFluxConfigurationSupport {

3、 自定义@Enable模块驱动

3.1、创建maven工程

pom.xml内容如下:

<?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.tyschool</groupId> <artifactId>enable-driver</artifactId> <version>1.0-SNAPSHOT</version> <properties> <!-- Spring 3.x 最新发布版本 --> <spring.version>3.2.18.RELEASE</spring.version> <java.version>1.9</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> </plugins> </build> </project>

3.2、实现Configuration类

@Configuration public class HelloConfig { /*** * 创建名为"helloWorld"String类型的Bean * @return */ @Bean public String helloWorld(){ return "Hello,World"; } }

3.3、实现"@Enable模块驱动"注解

@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Import(HelloConfig.class) public @interface EnableHello { }

3.4、 标注@EnableHello到引到类

@EnableHello @Configuration public class EnableHelloBootStrap { public static void main(String[] args) { //构建Annotation配置驱动Spring上下文 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); //注册当前引导类到Spring上下文 context.register(EnableHelloBootStrap.class); //启动上下文 context.refresh(); //获取名称为"helloWorld"的Bean对象 String helloWorld = context.getBean("helloWorld", String.class); //输出 System.out.println(helloWorld); //关闭上下文 context.close(); }

4、Spring Web自动装配概述

Spring Framwork 3.1.0.RELEASE中新引入的WebApplicationInitializer构建在Servlet3.0 之上,应用在Servlet3.0+的环境也可以采用编程手段实现。

WebApplicationInitializer属于Spring MVC提供的接口,确保Web ApplicationInitializer自定义实现能够被任何Servlet3.0容器侦测并自动地初始化。如果实现WebApplicationInitializer接口较为困难,也可使用简化实现方案,即AbstractDispatcherServletInitializer。

AbstractAnnotaionConfigDispatcherServletInitializer是AbstactDispatcherServletInitializer的子类。前者实现属于Spring Java代码配置驱动,后者实现是Spring XML配置驱动。

1588673820165.png

5、自定义Web自动装配

5.1、在pom.xml文件中添加web依赖

<?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> <packaging>war</packaging> <groupId>com.tyschool</groupId> <artifactId>enable-driver</artifactId> <version>1.0-SNAPSHOT</version> <properties> <!-- Spring 3.x 最新发布版本 --> <spring.version>3.2.18.RELEASE</spring.version> <java.version>1.9</java.version> </properties> <dependencies> <!-- Servlet 3.0 API --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.1</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring Web MVC 依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <plugins> <!-- Maven war 插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <!-- 忽略错误,当web.xml不存在时 --> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> <!-- Tomcat Maven 插件用于构建可执行 war --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.1</version> <executions> <execution> <id>tomcat-run</id> <goals> <!-- 最终打包成可执行的jar包 --> <goal>exec-war-only</goal> </goals> <phase>package</phase> <configuration> <!-- ServletContext 路径 --> <path>/</path> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>

5.2、 新增@Controller

@Controller //标识为controller public class HelloWorldController { @RequestMapping("/hello") @ResponseBody //返回json数据格式 public String helloWorld(){ return "Hello,World!"; } }

5.3、新增Spring Web MVC配置

@EnableWebMvc //开启WebMvc @Configuration //标识为配置类 @ComponentScan(basePackageClasses = SpringWebMvcConfiguration.class)//扫描SpringWebMvcConfiguration所在的包及子包 public class SpringWebMvcConfiguration { }

5.4、实现AbstractAnnotationConfigDispatcherServletInitializer

package com.tyschool.initializer; import com.tyschool.config.SpringWebMvcConfiguration; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class SpringWebMvcServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { protected Class<?>[] getRootConfigClasses() { return new Class[0]; } //DispatcherServlet配置Bean protected Class<?>[] getServletConfigClasses() { return of(SpringWebMvcConfiguration.class); } //DispathcerServlet URL Pattern映射 protected String[] getServletMappings() { return of("/*"); } private static <T> T [] of(T... values) { return values; } }

5.5、打包并运行

java -jar enable-driver-1.0-SNAPSHOT-war-exec.jar
需要 登录 才可以提问哦