@Autowired//The default is injected by type @Qualifier("cusInfoService")//It is generally used as a modification of @Autowired()
@Resource(name="cusInfoService")//The default injection is by name, and selective injection can be performed through the name and type attributes
Generally @Autowired and @Qualifier are used together, and @Resource is used alone.
Of course, if there is no conflict, @Autowired can also be used alone
These three are used according to the actual situation. Usually, @Autowired and @Resource are used more. The bean name does not need to be written, and UserServiceImpl can write @Service("userService") like this. It is the situation that is considered during the design of the entire project. If the architect considers it more carefully, the requirements are stricter, and the access speed after the project is launched is required to be better, usually the speed is considered. At this time, @Autowired is not as useful as @Resource, because @Resource can be searched by name, which is written like @Resource("userService"). The reason for not using @Autowired @Qualifie("userService") is simple, it's a bit long, I don't like it, it increases the workload. Because searching by name is the fastest, just like looking up a database, looking up by Id is the fastest. Because the name here is the same as the ID in the database. At this time, the workload will naturally increase. And if you don't need to use annotations, when using xml files, you need to write an ID when injecting beans, and the id in the xml file is equivalent to the name here.
Usually, the annotation of a bean is wrong, and the following errors will be reported. The most common, No bean named 'user' is defined, this means that the bean named user is not found. In layman's terms, it is the type named user. and its subtypes, the reason for this error is that the type name when injecting is user, but it cannot be found when searching, that is to say, the search type may not have the command user, and the solution is to find this type. go to command as user,
The following error is also common, No qualifying bean of type [com.service.UserService] found for dependency: The reason for this error is that there is no injection of @Service on the type, not only @Service, but also in other layers. Error, here I use Service as an example to illustrate. If it is the DAO layer, @Repository is not added, and the Controller layer is not added @Controller.
Also, if you want to make it simpler, whether it is DAO, Controller, Service three layers, you can use this annotation, @Component, this annotation is common to all beans, but fewer people use it, because of the layered design of MVC Principle, use @Repository, @Service, @Controller, this can distinguish DAO, Service, Controller in MVC principle. easy to identify.
-------------Common Notes-------
– Annotations that define beans
@Controller
@Controller("Bean's name")
Define control layer beans, such as Action
@Service
@Service("Bean's name")
Define business layer beans
@Repository
@Repository("Bean's name")
Defining DAO Layer Beans
@Component
Define Bean, used when it is not easy to classify.
– Autowire Bean (you can choose an annotation)
@Autowired (provided by Srping)
Default matching by type, automatic assembly (provided by Srping), can be written on member properties, or on setter methods
@Autowired(required=true)
Be sure to find a matching Bean, otherwise throw an exception. The default value is true
@Autowired
@Qualifier("bean name")
Assemble beans by name and use them in combination with @Autowired to solve the problem of finding multiple beans by type matching.
Provided by @Resource JSR-250
By default, it is assembled by name, and when a bean with a matching name is not found, it is assembled by type.
Can be written on member properties, or on setter methods
You can specify the name of the injected bean through @Resource(name="beanName"). If the name attribute is not specified, the variable name of the member attribute is used by default, and the name attribute is generally not required.
@Resource(name="beanName") specifies the name attribute, injecting by name but not finding the bean, it will not be assembled by type.
@Inject is provided by JSR-330
Assembly by type, less functionality than @Autowired, no need to use.
– Define the scope and life process of the bean
@Scope(“prototype”)
Values are: singleton,prototype,session,request,session,globalSession
@PostConstruct
Equivalent to init-method, used in the method, executed when the bean is initialized.
@PreDestroy
Equivalent to destroy-method, used in the method, executed when the Bean is destroyed.
– Declarative transactions
@Transactional
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。