请处理这个基本问题。
我曾经使用 @Autowired 注释,其中可以使用该类的键/值格式为属性(类变量)赋予一个值。
<bean id="class" class="a.b.c.Class" lazy-init="true">
<property name="var1" value="${var1}" />
</bean>
Run Code Online (Sandbox Code Playgroud)
我将 Class 更改为带有不需要自动装配的组件扫描选项的 @Component 。我现在如何添加属性变量?
另外,我不想在编写 Junit 时遇到麻烦。
谢谢,
我们知道,@Autowired只能在spring容器管理的实例中使用,如果你新建一个实例,其中的@Autowired成员不会生效。
但我认为在某些情况下,新实例无法避免。
比如一个RunnableTask。其中包含由 spring 管理的 DAOService。因为任务是手动新建的。所以我不能在ThreadTask中使用DAOService。
所以我想知道如何在Spring Boot 中获取 ApplicationContext ,这样我就可以通过context.getBean().
我知道在 main() 中我可以自动装配 ApplicationContext。但是我不能在任何地方都将上下文作为参数传递!
我想在任何地方获取 ApplicationContext。
任何帮助将不胜感激。
我正在运行一个 Spring Boot 应用程序,我已经在我的 App 配置类中进行了配置:
@Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
pool.setCorePoolSize(5);
pool.setMaxPoolSize(10);
pool.setWaitForTasksToCompleteOnShutdown(true);
return pool;
}
Run Code Online (Sandbox Code Playgroud)
我用 TaskExecutor 这样创建我的线程:
@Configuration
public class ProducerConsumer {
@Inject
TaskExecutor taskExecutor;
Producer producer = new Producer(sharedQueue);
Consumer consumer = new Consumer(sharedQueue);
taskExecutor.execute(producer);
taskExecutor.execute(consumer);
Run Code Online (Sandbox Code Playgroud)
生产者和消费者,两个类都实现了 Runnable。我的线程按预期工作,但是当我尝试将 Bean 注入或自动装配到消费者或生产者中时,它为空。
@Component
public class Consumer implements Runnable {
@Autowired
SomeController someController;
public Consumer (BlockingQueue<String> sharedQueue) {
this.sharedQueue = sharedQueue;
}
@Override
public void run() {
while (true) {
synchronized (sharedQueue) {
//someController …Run Code Online (Sandbox Code Playgroud) spring multithreading dependency-injection autowired spring-boot
我有两个项目 A 和 B。两者都是用 Maven 构建的,项目 A 有一个对项目 B 的 Maven 依赖。这两个项目都有一个带有 @Configuration 注释的类,我在其中定义了 @Beans。
我在项目 A 中有来自两个项目的 bean。如果我在同一项目中定义的 bean 的项目 A 中使用 @Autowired 注释,则自动装配工作。但是,如果我在项目 B 的 bean 的项目 A 中使用 @Autowired 批注,则会出现异常。
这是什么意思?如何在项目 B 中定义的项目 A 中自动装配 bean?
我知道该错误是不言自明的,但是当我将其余模板的设置从构造函数删除为 @Autowired @Qualifier("myRestTemplate") private RestTemplate restTemplate 时,它起作用了。
只是想知道如果同一个类具有我要自动装配的 bean 定义,我该如何在构造函数中执行此操作?
org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名称为“xxx”的bean时出错:请求的bean当前正在创建中:是否存在无法解析的循环引用?
@Component
public class xxx {
private RestTemplate restTemplate;
@Autowired
public xxx(@Qualifier("myRestTemplate") RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@Bean(name="myRestTemplate")
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用自动装配 (@Autowired) 在 JUnit 测试类中注入依赖项,但面临 NullPointerException。我想知道 JUnit 测试类中是否可以进行自动装配。否则应该如何将bean注入到测试类中。我的代码如下 -
主类/客户端- 自动装配按预期工作。
package com.example.demo;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import com.example.demo.services.IMessage;
import com.example.demo.services.SayWelcomeService;
@SpringBootApplication
@ComponentScan("com.example.demo.services")
public class AutowireWithMultipleImplementationsApplication {
@Autowired
IMessage sayHelloService;
@Autowired
SayWelcomeService sayWelcome;
@Autowired
IMessage masterService;
public static void main(String[] args) {
SpringApplication.run(AutowireWithMultipleImplementationsApplication.class, args);
}
@PostConstruct
public void init() {
String message;
message=masterService.message("George");
System.out.println("message: \n" + message);
message=sayWelcome.message("george");
System.out.println("message: " + message);
}
}
Run Code Online (Sandbox Code Playgroud)
服务接口和实现类
接口IMessage
package com.example.demo.services;
public interface IMessage { …Run Code Online (Sandbox Code Playgroud) junit spring dependency-injection inversion-of-control autowired
我已经扩展CrudRepository<ClassName, Id>了用户定义的接口,但是在尝试使用注入时,@Autowired我收到以下错误:
创建名称为“helloController”的 bean:通过字段“danCorePrivateRepository”表达的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“com.sgcorp.repository.DanCorePrivateRepository”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。
HelloController.java
@RestController
@RequestMapping("/hello")
public class HelloController {
@Autowired
private DanCorePrivateRepository danCorePrivateRepository;
@RequestMapping(value = "/service", method= RequestMethod.GET)
public String selectService(){
String result = "<html>";
result += "<div>"+danCorePrivateRepository.findAll()+"</div>";
return result+ "</html>";
}
}
Run Code Online (Sandbox Code Playgroud)
DanCorePrivateRepository.java(用户定义接口)
public interface DanCorePrivateRepository extends CrudRepository<DanaModel, String> {
}
Run Code Online (Sandbox Code Playgroud)
请建议为什么它不正确@Autowired?
注意:在其他一些项目中它正在运行。
我是春天的新手。我使用 Java 开发使用带证书的 RESTful 服务的服务
这是我的配置类:
package configuration;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.ResourceUtils;
import org.springframework.web.client.RestTemplate;
import javax.net.ssl.SSLContext;
import java.util.function.Supplier;
@Configuration
public class RestClientCertConfig {
private char[] allPassword = "allpassword".toCharArray();
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) throws Exception {
SSLContext sslContext = SSLContextBuilder
.create()
.loadKeyMaterial(ResourceUtils.getFile("classpath:keystore.jks"), allPassword, allPassword)
.loadTrustMaterial(ResourceUtils.getFile("classpath:truststore.jks"), allPassword)
.build();
HttpClient client = HttpClients.custom()
.setSSLContext(sslContext)
.build();
return builder
.requestFactory((Supplier<ClientHttpRequestFactory>)new HttpComponentsClientHttpRequestFactory(client))
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我使用 Restful EndPoint 的类:
import org.springframework.beans.factory.annotation.Autowired; …Run Code Online (Sandbox Code Playgroud) 我有一个配置类,我可以像这样将 Map 创建为 Bean。
@Bean
public Map<String, Filter> filters() {
Map<String, Filter> map = new HashMap<>();
map.put("RECOMMENDATION", new RecommendationFilter());
map.put("UPCOMING", new UpcomingFilter());
return map;
}
Run Code Online (Sandbox Code Playgroud)
并在另一个服务中自动装配此地图:
@Autowired
private Map<String, Filter> filterMap;
Run Code Online (Sandbox Code Playgroud)
在调试模式下,我可以通过类名称看到映射有另一个键:
但不是“推荐”和“即将到来”,它是如何在配置中设置的。而且谷歌搜索没有帮助。
我知道构造函数在自动连接变量之前调用。但是,不幸的是,我想读取构造函数中的 application.properties 值?
@Component
public class DESedeEncryption {
private static final String key = "TEST_KEY";
public DESedeEncryption() {
system.out.println(key);
}
}
DESedeEncryption encrypted = new DESedeEncryption();
Run Code Online (Sandbox Code Playgroud)
对于上面的类,在我的项目中使用 new 操作符创建了对象共有 108 个位置。现在,我想从 application.properties 中读取该键值。但是,我需要使用 @Autowired 注释更改所有 108 个位置。但是,有些地方在实体类文件中使用“new”运算符编写了对象创建。所以,我不能在实体类中自动连接对象。
有人,请帮我解决这个问题。
autowired ×10
spring ×9
java ×6
spring-boot ×4
components ×1
crud ×1
dictionary ×1
javabeans ×1
junit ×1