spring 不同注解的使用场景
2016-04-19 17:10
573 查看
spring中各种注解的使用场景。本文摘自stackoverflow.url:http://stackoverflow.com/questions/19414734/understanding-spring-autowired-usage
The
allows you to skip configurations elsewhere of what to inject and just does it for you. Assuming your package is
have to put this tag in your XML (application context file):
This tag will do an auto-scanning. Assuming each class that has to become a bean is annotated with a correct annotation like
simple bean) or
a servlet control) or
and these classes are somewhere under the package
Spring will find all of these and create a bean for each one. This is done in 2 scans of the classes - the first time it just searches for classes that need to become a bean and maps the injections it needs to be doing, and on the second scan it injects the
beans. Of course, you can define your beans in the more traditional XML file or with a @Configuration class
(or any combination of the three).
The
tells Spring where an injection needs to occur. If you put it on a method
understands (by the prefix
the
that a bean needs to be injected. In the second scan, Spring searches for a bean of type
and if it finds such bean, it inject
it to this method. If it finds two such beans you will get an
To avoid the
you can use the
and tell it which of the two beans to inject in the following manner:
Or if you prefer to declare the beans in your XML, it would look something like this:
In the
you need to also add the
tell which of the two color beans to inject:
If you don't want to use two annotations (the
you can use
combine these two:
The
can read some extra data about it in the first comment on this answer) spares you the use of two annotations and instead you only use one.
I'll just add two more comments:
Good practice would be to use
of
it is not Spring-specific and is part of the
Another good practice would be to put the
a constructor instead of a method. If you put it on a constructor, you can validate that the injected beans are not null and fail fast when you try to start the application and avoid a
you need to actually use the bean.
So, to sum it up: The
spares you the need to do the wiring by yourself in the XML file (or any other way) and just finds for you what needs to be injected where, and does that for you.
The
@Autowiredannotation
allows you to skip configurations elsewhere of what to inject and just does it for you. Assuming your package is
com.mycompany.moviesyou
have to put this tag in your XML (application context file):
<context:component-scan base-package="com.mycompany.movies" />
This tag will do an auto-scanning. Assuming each class that has to become a bean is annotated with a correct annotation like
@Component(for
simple bean) or
@Controller(for
a servlet control) or
@Repository(for
DAOclasses)
and these classes are somewhere under the package
com.mycompany.movies,
Spring will find all of these and create a bean for each one. This is done in 2 scans of the classes - the first time it just searches for classes that need to become a bean and maps the injections it needs to be doing, and on the second scan it injects the
beans. Of course, you can define your beans in the more traditional XML file or with a @Configuration class
(or any combination of the three).
The
@Autowiredannotation
tells Spring where an injection needs to occur. If you put it on a method
setMovieFinderit
understands (by the prefix
set+
the
@Autowiredannotation)
that a bean needs to be injected. In the second scan, Spring searches for a bean of type
MovieFinder,
and if it finds such bean, it inject
it to this method. If it finds two such beans you will get an
Exception.
To avoid the
Exception,
you can use the
@Qualifierannotation
and tell it which of the two beans to inject in the following manner:
@Qualifier("redBean") class Red implements Color { // Class code here } @Qualifier("blueBean") class Blue implements Color { // Class code here }
Or if you prefer to declare the beans in your XML, it would look something like this:
<bean id="redBean" class="com.mycompany.movies.Red"/> <bean id="blueBean" class="com.mycompany.movies.Blue"/>
In the
@Autowireddeclaration,
you need to also add the
@Qualifierto
tell which of the two color beans to inject:
@Autowired @Qualifier("redBean") public void setColor(Color color) { this.color = color; }
If you don't want to use two annotations (the
@Autowiredand
@Qualifier)
you can use
@Resourceto
combine these two:
@Resource(name="redBean") public void setColor(Color color) { this.color = color; }
The
@Resource(you
can read some extra data about it in the first comment on this answer) spares you the use of two annotations and instead you only use one.
I'll just add two more comments:
Good practice would be to use
@Injectinstead
of
@Autowiredbecause
it is not Spring-specific and is part of the
JSR-330standard.
Another good practice would be to put the
@Inject/
@Autowiredon
a constructor instead of a method. If you put it on a constructor, you can validate that the injected beans are not null and fail fast when you try to start the application and avoid a
NullPointerExceptionwhen
you need to actually use the bean.
So, to sum it up: The
@Autowiredannotation
spares you the need to do the wiring by yourself in the XML file (or any other way) and just finds for you what needs to be injected where, and does that for you.
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- 模拟Spring的简单实现
- Spring整合WebSocket应用示例(上)
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- springmvc 发送ajax出现中文乱码的解决方法汇总
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- 读取spring配置文件的方法(spring读取资源文件)
- Spring Bean基本管理实例详解
- java实现简单美女拼图游戏
- 详解Java的Spring框架中的事务管理方式
- 解析Java的Spring框架的BeanPostProcessor发布处理器