[英]Spring autowire object with value
Suppose i have class named SomeClass
which takes value in constructor and populates the field. 假设我有一个名为SomeClass
类,它在构造函数中获取值并填充该字段。
String text;
String sometext;
public someClass(String text, String sometext){
this.text = text;
this.sometext = sometext;
}
SomeClass
has a method which creates a new object. SomeClass
有一个创建新对象的方法。 In java when we create a new object we can instantiate with values like 在java中,当我们创建一个新对象时,我们可以使用像这样的值进行实例化
ClassName variable = new ClassName(text, sometext);
and populate the fields in ClassName
like this using constructor 并使用构造函数填充ClassName
的字段
public ClassName(String text, String sometext){
this.text = text;
this.sometext = sometext;
}
But in spring using autowired how can we do that? 但是在春天使用自动装配我们怎么能这样做?
@Autowired
public ClassName(SomeClass someClass){
this.text = someClass.text;
this.sometext = someClass.sometext;
}
This wont work. 这不行。 how spring will know which the instance of SomeClass
Spring如何知道SomeClass
哪个实例
UPDATE: 更新:
I was wrong. 我错了。 i was not thinking in DI way. 我不是在想DI。 instead of autowiring SomeClass
. 而不是自动装配SomeClass
。 i had to autowire ClassName
. 我不得不自动装配ClassName
。
Without DI i created a new object in a method of class ClassName
没有DI我在类ClassName
的方法中创建了一个新对象
When using DI i had to autowire in the method of the class ClassName
当使用DI时,我必须在类ClassName
的方法中自动装配
@Autowired
ClassName className;
I can populate the fields directly making the fields public 我可以直接填充字段,使字段公开
className.text = text;
className.sometext = sometext;
and i can do using javabeans. 我可以使用javabeans。 but how to do via constructor. 但是如何通过构造函数来做。
NOTE: there is nothing wrong with spring config. 注意:spring配置没有任何问题。 base scan is enabled. 基本扫描已启用。
You are right in the sense that Spring will not automatically know of all the classes or create the required beans. 你是正确的,因为Spring不会自动知道所有类或创建所需的bean。 Spring does not work like magic. 春天不像魔术一样有效。 We can use annotations but we still need to tell Spring which beans to create and how to create them. 我们可以使用注释,但我们仍然需要告诉Spring要创建哪些bean以及如何创建它们。 That is where the @Component
, @Service
, @Repository
, @Controller
comes into play. 这就是在@Component
, @Service
, @Repository
, @Controller
进场。 have a look here . 看看这里 。 When you want SomeClass
to be a dependency in ClassName
, you have to explicitly let Spring know that a bean of SomeClass
needs to be created before it can be injected as a dependency in ClassName
, and you do that by annotating SomeClass
with the correct annotation. 当您希望SomeClass
成为ClassName
的依赖项时,您必须明确地让Spring知道SomeClass
的bean需要先创建才能作为ClassName
的依赖项注入,并且您可以通过使用正确的注释注释SomeClass
来实现。 Also you need to do a <context:component-scan base-package="com.your.package"/>
for Spring to resolve all the annotations correctly before it can autowire dependencies. 此外,您需要为Spring执行<context:component-scan base-package="com.your.package"/>
,以便在它可以自动装配依赖项之前正确解析所有注释。
Moreover, you have to realise the fact that if your SomeClass
constructor arguments depend on dynamic values, you won't be able to create the bean right away, and you would not be able to inject it as a dependency in ClassName
using @Autowired
, since spring needs to know those values during deploy time. 此外,您必须意识到如果您的SomeClass
构造函数参数依赖于动态值,您将无法立即创建bean,并且您将无法使用@Autowired
将其作为ClassName
的依赖项注入,因为Spring需要在部署期间知道这些值。 If that is the case, I'd prefer using getter setter methods instead in ClassName
for an instance of SomeClass
. 如果是这种情况,我更喜欢在ClassName
使用getter setter方法作为SomeClass
的实例。
Note: This is answer is taking into consideration about annotations in Spring. 注意:答案是考虑到Spring中的注释。 You can also do the same by defining Spring Beans in XML. 您也可以通过在XML中定义Spring Beans来做同样的事情。
You can not use new with Spring, everything has to be created in the spring context. 你不能在Spring中使用new,所有东西都必须在spring环境中创建。 In your example your spring config would be 在你的例子中,你的弹簧配置是
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean class="SomeClass">
<constructor-arg index="0" name="text" value="some value" />
<constructor-arg index="1" name="sometext" value="some other value" />
</bean>
</beans>
Your code woud be 你的代码应该是
@Autowired
private SomeClass someClass;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.