Java -- Spring annotation and spring Annotation

Source: Internet
Author: User

Java -- Spring annotation and spring Annotation

Common Spring annotations
Construct an IoC Container Using annotations
Use annotations to register beans with Spring containers. You must register <context: component-scan base-package = "pagkage1 [, pagkage2 ,..., PagkageN] "/>.
For example, specify a package in base-package.

<context:component-scan base-package="cn.gacl.java"/>

Indicates cn. gacl. in a java package and its sub-package, if a Class header contains a specific annotation [@ Component/@ Repository/@ Service/@ Controller ], the object is registered as a Bean in the Spring container. You can also specify multiple packages in <context: component-scan base-package = ""/>, for example:

<context:component-scan base-package="cn.gacl.dao.impl,cn.gacl.service.impl,cn.gacl.action"/>

Multiple packages are separated by commas.


1. @ Component
@ Component is a common form of Spring management components. @ Component annotations can be placed on class headers. @ Component is not recommended.
2. @ Controller
@ Controller corresponds to the Bean of the presentation layer, that is, Action, for example:

@Controller@Scope("prototype")public class UserAction extends BaseAction<User>{  ……}

After the @ Controller annotation is used to identify UserAction, it indicates that the UserAction should be handed over to the Spring container for management. There will be an action named "userAction" in the Spring container, this name is obtained based on the UserAction class name. NOTE: If @ Controller does not specify its value [@ Controller], the default bean name is the first letter of the class name in lowercase, if you specify value [@ Controller (value = "UserAction")] or [@ Controller ("UserAction")], value is used as the bean name.
Here, the UserAction also uses the @ Scope annotation. @ Scope ("prototype") indicates that the Action range is declared as a prototype, the scope = "prototype" of the container can be used to ensure that each request has a separate Action for processing, avoiding the thread security issue of the Action in struts. By default, scope in spring is a singleton mode (scope = "singleton"). In this way, only one Action object is created, and each access is the same Action object, causing insecure data, struts2 requires that each access correspond to different actions. scope = "prototype" ensures that an Action object is created when a request is made.
3. @ Service
@ Service corresponds to the business layer Bean, for example:

@Service("userService")public class UserServiceImpl implements UserService {  ………}

The @ Service ("userService") annotation tells Spring that when Spring wants to create an instance of UserServiceImpl, the bean name must be "userService". In this way, when Action needs to use an instance of UserServiceImpl, the "userService" created by Spring can be injected to the Action. In the Action, you only need to declare a variable named "userService" to receive the "userService" injected by Spring, the Code is as follows:

// Inject userService @ Resource (name = "userService") private UserService userService;

Note: The type of the "userService" variable declared in the Action must be "UserServiceImpl" or its parent class "UserService". Otherwise, injection cannot be performed due to inconsistent types, because the declared "userService" variable in the Action uses the @ Resource annotation to mark and specifies its name = "userService", this is equivalent to telling Spring, let's say that I want to instantiate a "userService" for the Action. You can instantiate it for me in Spring and give it to me. When Spring sees the @ Resource Annotation on the userService variable, according to the name attribute specified by this parameter, an instance of UserServiceImpl must be used in the Action, at this time, Spring will inject the UserServiceImpl instance named "userService" into the "userService" variable in the Action to help the Action complete use RService instantiation, so that in the Action, you do not need to instantiate UserService in the original way "userService = new UserServiceImpl. If there is no Spring, when the Action needs to use UserServiceImpl, you must use "UserService userService = new UserServiceImpl ();" to create an instance object, but after using Spring, to use UserServiceImpl for an Action, you do not need to create the UserServiceImpl instance. The UserServiceImpl instance has been created by Spring. Spring will give the created UserServiceImpl instance to the Action, the Action can be used directly. The Action can be used immediately after an active UserServiceImpl instance is created. Instead, the Action can be used only when the UserServiceImpl instance is created by Spring and then injected to the Action. This indicates that the "control" of the Action on the "UserServiceImpl" class has been "Reversed". In the past, the initiative was in the hands of the user, and the user needs to use the "UserServiceImpl" class instance, you can use it immediately after you take the initiative to create a new instance, but now you cannot take the initiative to go to the new "UserServiceImpl" class instance, the power of the new "UserServiceImpl" class instance has been removed by Spring. Only Spring can be used for the new "UserServiceImpl" class instance, action can only wait for Spring to create the "UserServiceImpl" class instance, and then "Beg" Spring to give it the created "UserServiceImpl" class instance, in this way, he can use "UserServiceImpl", which is the core idea of Spring, "control inversion", also known as "dependency injection". "dependency injection" is also well understood, and actions need to work with UserServiceImpl, then it is for UserSe RviceImpl generates dependencies. Spring injects the UserServiceImpl required by Acion to the Action, which is called "dependency injection ". For an Action, when an Action depends on something, Spring is requested to inject it to it. For Spring, Spring actively injects the Action to it.
4. @ Repository
@ Repository corresponds to the data access layer Bean, for example:

@Repository(value="userDao")public class UserDaoImpl extends BaseDaoImpl<User> {  ………}

@ Repository (value = "userDao") annotation tells Spring to create a UserDaoImpl instance named "userDao.
When the Service needs to use the UserDaoImpl instance created by Spring, the @ Resource (name = "userDao") annotation can be used to notify Spring, spring injects the created userDao into the Service.

// Inject userDao. @ Resource (name = "userDao") private BaseDao <User> userDao is used to retrieve a specified User from the database based on the User ID;

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.