Spring通过名称获取Bean示例
程序员文章站
2022-07-02 10:54:30
摘要:本文主要演示通过继承自抽象类ApplicationObjectSupport获取Bean实例。 参考文档: "Building a RESTful Web Service" "Spring在代码中获取bean的几种方式" "Spring在代码中获取bean的几种方式" 提到共有以下几种方式: ......
摘要:本文主要演示通过继承自抽象类applicationobjectsupport获取bean实例。
参考文档:
spring在代码中获取bean的几种方式提到共有以下几种方式:
- 方法一:在初始化时保存applicationcontext对象
- 方法二:通过spring提供的utils类获取applicationcontext对象
- 方法三:继承自抽象类applicationobjectsupport
- 方法四:继承自抽象类webapplicationobjectsupport
- 方法五:实现接口applicationcontextaware
- 方法六:通过spring提供的contextloader
完整示例代码请参考:
软件环境:
- 操作系统:macos catalina 10.15.3
- jdk 13.0.2
- spring-boot-starter-parent: 2.2.5.release
- maven: 3.6.3
目录:
文件布局
localhost:rest-service didi$ tree . . ├── pom.xml ├── src │ └── main │ └── java │ └── com │ └── example │ ├── restserviceapplication.java │ ├── controller │ │ ├── greeting.java │ │ └── greetingcontroller.java │ ├── service │ │ ├── bartask.java │ │ ├── footask.java │ │ └── task.java │ └── util │ └── springcontextholder.java
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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <parent> <groupid>com.example</groupid> <artifactid>springcloud-parent</artifactid> <version>1.0-snapshot</version> </parent> <groupid>com.example</groupid> <artifactid>rest-service</artifactid> <version>1.0-snapshot</version> <name>rest-service</name> <description>demo project for spring boot</description> <properties> <java.version>13</java.version> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> <exclusions> <exclusion> <groupid>org.junit.vintage</groupid> <artifactid>junit-vintage-engine</artifactid> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project>
task接口
文件路径:src/main/java/com/example/service/task.java
package com.example.service; public interface task { void execute(); }
task实现类bartask
文件路径:src/main/java/com/example/service/bartask.java
package com.example.service; import org.springframework.stereotype.service; @service("bartask") public class bartask implements task { @override public void execute() { system.out.println("run bartask"); } }
task实现类footask
文件路径:src/main/java/com/example/service/footask.java
package com.example.service; import org.springframework.stereotype.service; @service("footask") public class footask implements task { @override public void execute() { system.out.println("run footask"); } }
spring上下文工具类(继承applicationobjectsupport)
文件路径:src/main/java/com/example/util/springcontextholder.java
package com.example.util; import com.example.service.task; import org.springframework.context.support.applicationobjectsupport; import org.springframework.stereotype.component; @component public class springcontextholder extends applicationobjectsupport { public task gettask(string beanname){ return super.getapplicationcontext().getbean(beanname , task.class); } }
用于测试的greetingcontroller
文件路径:src/main/java/com/example/controller/greetingcontroller.java
package com.example.controller; import java.util.concurrent.atomic.atomiclong; import com.example.service.task; import com.example.util.springcontextholder; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.requestparam; import org.springframework.web.bind.annotation.restcontroller; @restcontroller public class greetingcontroller { private static final string template = "hello, %s!"; private final atomiclong counter = new atomiclong(); @autowired springcontextholder holder; @getmapping("/greeting") public greeting greeting(@requestparam(value = "name", defaultvalue = "world") string name) { task task1 = holder.gettask("footask"); task task2 = holder.gettask("bartask"); task1.execute(); task2.execute(); return new greeting(counter.incrementandget(), string.format(template, name)); } }
其它如src/main/java/com/example/restserviceapplication.java和src/main/java/com/example/controller/greeting.java等文件请参考github
测试
启动该服务后,在浏览器中输入:,会得到如下响应:
{"id":1,"content":"hello, world!"}
在后台日志会看到:
run footask run bartask
推荐阅读
-
Spring Boot - 获取所有的Bean信息
-
获取并打印Spring容器中所有的Bean名称
-
iOS开发之通过银行卡号获取所属银行名称
-
Java获取文件夹下所有文件名称的方法示例
-
解析Java中如何获取Spring中配置的bean
-
获取Spring中ApplicationContext.xml的Bean 博客分类: Spring SpringBeanFactoryApplicationContext
-
android通过google api获取天气信息示例
-
通过spring.factory文件来加载第三方的bean
-
spring5源码解析—Bean的获取流程
-
spring中单例中获取新的bean实例