我正在学习春天3,我似乎没有把握背后的功能<context:annotation-config>和<context:component-scan>.
根据我的阅读,他们似乎处理不同的注释(@ Required,@ Autowired etc vs @Component,@ Repository,@ Service等),但也从我读过的内容中注册了相同的bean后处理器类.
为了让我更加困惑,有一个@Required属性@Autowired.
有人可以对这些标签有所了解吗?什么是相似的,什么是不同的,一个被另一个取代,它们相互完成,我需要其中一个,两者都有吗?
我正在阅读spring 3.0.x参考文档以了解Spring Autowired注释:
我无法理解下面的例子.我们是否需要在XML中执行某些操作才能使用它?
例1
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
例2
public class MovieRecommender {
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public void prepare(MovieCatalog movieCatalog,
CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
如何通过自动装配两个类来实现相同的接口并使用相同的类?
例:
class Red implements Color
class Blue implements Color
class myMainClass{
@Autowired
private Color color;
draw(){
color.design();
}
}
Run Code Online (Sandbox Code Playgroud)
将调用哪种设计方法?如何确保调用Red类的设计方法而不是Blue?
我将旧的xml/java配置转换为纯java配置.在xml中,我使用参数注入配置文件,如下所示:
<bean class="com.project.SpringRestConfiguration">
<property name="parameters" ref="parameters" />
</bean>
@Configuration
public class SpringRestConfiguration {
private Parameters parameters;
public void setParameters(Parameters parameters) {
this.parameters = parameters;
}
// @Bean definitions
...
}
Run Code Online (Sandbox Code Playgroud)
是否可以在javaconfig中注入参数?(无需使用自动装配!)
@Configuration
@Import(SpringRestConfiguration.class)
Run Code Online (Sandbox Code Playgroud)
编辑:使用@Import,我看不到有任何机会将参数注入SpringRestConfiguration
我有一个xml bean文件
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<bean id="helloWorld" class="com.a.b.HelloWorld">
<property name="attr1" value="Attr1 from XML"></property>
</bean>
<bean id="helloWorld2" class="com.a.b.HelloWorld2">
<property name="attr2" value="Attr2 from XML"></property>
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
我使用像这样的构造函数自动装配
public class HelloWorld2{
private String attr2;
public void setAttr2(String message){
this.attr2 = message;
}
public void getAttr2(){
System.out.println("getAttr2 == " + attr2);
}
}
public class HelloWorld{
private String attr1;
private HelloWorld2 helloWorld2;
public HelloWorld(){
}
@Autowired
public HelloWorld(HelloWorld2 helloWorld2){
System.out.println("hhh");
this.helloWorld2 = helloWorld2;
}
public …Run Code Online (Sandbox Code Playgroud)