Skip to content

Latest commit

 

History

History
57 lines (40 loc) · 1.37 KB

File metadata and controls

57 lines (40 loc) · 1.37 KB

3.11.2 @Named:相当于@Component

 @javax.inject.Named或 javax.annotation.ManagedBean可以使用下面的方式来替代@Component:

import javax.inject.Inject;
import javax.inject.Named;

@Named("movieListener") // @ManagedBean("movieListener") could be used as well
public class SimpleMovieLister {

	private MovieFinder movieFinder;

	@Inject
	public void setMovieFinder(MovieFinder movieFinder) {
		this.movieFinder = movieFinder;
	}

	// ...
}

@Component通常不指定组件名字。@Named也能这么用:

import javax.inject.Inject;
import javax.inject.Named;

@Named
public class SimpleMovieLister {

	private MovieFinder movieFinder;

	@Inject
	public void setMovieFinder(MovieFinder movieFinder) {
		this.movieFinder = movieFinder;
	}

	// ...
}

当使用@Named@ManagedBean时,可以使用与使用Spring注解完全相同的方式使用component-scanning组件扫描:

@Configuration
@ComponentScan(basePackages = "org.example")
public class AppConfig  {
   	...
}
[Note]
@Component相反,JSR-330@Named和JSR-250ManagedBean注解是不可组合使用的。 请使用Spring的stereotype model来构建自定义组件注解。