为什么我们不能在Spring bean中自动装配静态实例变量.我知道还有其他方法可以达到这个目的但只是想知道为什么我们不能以这种方式做到这一点.
例如
@Autowired
public static Test test;
Run Code Online (Sandbox Code Playgroud) 我有必须@Autowired在静态方法中使用的服务.我知道这是错的,但我不能改变当前的设计,因为它需要大量的工作,所以我需要一些简单的黑客.我不能改为randomMethod()非静态,我需要使用这个自动装配的bean.任何线索怎么做?
@Service
public class Foo {
public int doStuff() {
return 1;
}
}
public class Boo {
@Autowired
Foo foo;
public static void randomMethod() {
foo.doStuff();
}
}
Run Code Online (Sandbox Code Playgroud) 有了这堂课
@Component
public class Sample {
@Value("${my.name}")
public static String name;
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试Sample.name,它总是'空'.所以我尝试了这个.
public class Sample {
public static String name;
@PostConstruct
public void init(){
name = privateName;
}
@Value("${my.name}")
private String privateName;
public String getPrivateName() {
return privateName;
}
public void setPrivateName(String privateName) {
this.privateName = privateName;
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码有效.Sample.name设置得当.这是好方法吗?如果没有,有什么更好的方法吗?怎么做?
哪个更正确?
这个(方法上有@Autowired注释)?
@Controller
public class MyController
{
private MyDao myDao;
@Autowired
public MyController(MyDao myDao)
{
this.myDao = myDao;
}
Run Code Online (Sandbox Code Playgroud)
这个(在属性上有@Autowired注释)?
@Controller
public class MyController
{
@Autowired
private MyDao myDao;
public MyController(MyDao myDao)
{
this.myDao = myDao;
}
Run Code Online (Sandbox Code Playgroud)
@Autowired注释应该去哪里?
spring annotations dependency-injection spring-mvc autowired
我在我的网络应用程序中使用延迟加载与hibernate.
我想在服务器响应的解析阶段从数据库加载一些对象
Run Code Online (Sandbox Code Playgroud)@Component public class DesignSerializer extends JsonSerializer<Design> { @Autowired IDesignService designService; <-- is null}
这是完全可以理解的,因为DesignSerializer每个对象都使用"new"运算符进行实例化.
我确信有一种方法可以在创建时将bean注入该序列化程序,我只是不知道如何.
你们能帮助我或指出我正确的方向吗?
我正在尝试创建一个扩展函数,其实现使用 Spring bean。通过在包的顶层定义扩展函数似乎不可能做到这一点。我试过这个:
@Component
class Converter {
companion object {
@Autowired
lateinit var transformer: Transformer
fun Class1.convert(): Class2 {
return Class2 (this, transformer.transform(someStringProperty))
}
}
}
Run Code Online (Sandbox Code Playgroud)
wheretransform是一个将某个字符串转换为另一个字符串的函数,并且Class1是某个具有someStringProperty属性的类。我希望其他类可以import pkg.Converter.Companion.convert然后能够使用x.convert()wherex是类型的对象Class1。
语法有效,x.convert()在其他类中使用编译正常。但它在运行时导致异常:kotlin.UninitializedPropertyAccessException: lateinit property transformer has not been initialized
看起来 Spring 没有自动装配变量,因为它位于伴随对象中,而不是实际的组件对象中。
注释伴随对象@Component不起作用。
我不认为Class1.convert直接在内部移动Converter会起作用,因为然后使用x.convert()将需要Converter对象的实例,并且我不知道如何使用扩展函数语法来做到这一点。
有什么办法可以做到这一点,还是必须放弃扩展函数语法?
@Autowire静态字段的替代方法是什么?
我经历了很多问题并理解为什么@Autowired不能与静态字段一起使用。这个问题的公认答案提到
您必须编写自己的逻辑来执行此操作,因为不能使用 @Autowired。
由于我是依赖注入的新手,我不知道编写我们自己的逻辑来执行此操作必须遵循哪些步骤。
我得到的是它new不能用于创建对象,因为它会将 2 个类紧密耦合,而 DI 旨在成为它的解决方案。@autowired在同一问题的第二个答案中也不建议使用setter。那么在不使用的情况下实现相同效果的替代方法是@Autowire什么?
我是春天的新手。我使用 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) 我的xml看起来像这样:
<bean name="subscriberStore" class="java.util.HashSet" scope="singleton"/>
Run Code Online (Sandbox Code Playgroud)
我有以下代码:
@Value("#{subscriberStore}")
private static HashSet<Subscriber> subscriberStore;
Run Code Online (Sandbox Code Playgroud)
但是,在上面的类中,当我在subscriberStore上调用方法时,我得到一个空指针异常.我尝试过使用@Autowired而@Resource不是@Value ..上面但它没有任何区别.
任何人都有任何想法为什么订户商店没有初始化?
谢谢!