Spirng using annotations to construct an IOC container

Source: Internet
Author: User

Use annotations to register beans with the spring container. You need to register <context:component-scan base-package= ""/> in Applicationcontext.xml.

such as: <context:component-scan base-package= "Com.ssh"/>

Spring automatically scans all classes of the COM.SSH package, and registers these classes as beans if scanned for classes that have @component @[email protected].

You can also specify multiple packages in the <context:component-scan base-package= ""/>, such as:

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

1. @Component

@Component
is a common form of all Spring management components,@Component annotations can be placed on the head of a class,@Component deprecated

2. @Controller

@Controller The bean that corresponds to the presentation layer, which is the action, for example:

1 @Controller 2 @Scope ("prototype")3publicclassextends baseaction< User>{4  ... 5 }

After using the @controller annotation to identify the useraction, it means that the useraction is given to the spring container, and a "useraction" action exists in the spring container. This name is based on the Useraction class name. Note: If @controller does not specify its value "@Controller", the default bean name is the first lowercase of the class name for this class, if you specify value "@Controller (value=" useraction ")" or "@ Controller ("Useraction"), use value as the name of the bean.

The useraction also uses the @scope annotation, @Scope ("prototype") that declares the scope of the action as a prototype, and can take advantage of the container's scope= "prototype" to ensure that each request has a separate action to handle To avoid thread-safety issues with action in struts. Spring default scope is a singleton mode (scope= "singleton"), so that only one action object is created, each access is the same action object, the data is unsafe, and the STRUTS2 requires a different action for each access. Scope= "Prototype" ensures that an action object is created when a request is made

3. @ Service

The @Service corresponds to a business layer bean, for example:

1 @Service ("UserService")2public  classimplements  UserService {3  ... .. 4 }

The @Service ("UserService") annotation is to tell spring that when spring creates an instance of Userserviceimpl, the bean's name must be called "UserService", This way, when an action needs to use an instance of Userserviceimpl, it is possible to create a "userservice" from spring and inject it into the action: only one name called "UserService" is declared on the action. Variable to receive the "UserService" injected by spring, the code is as follows:

1 // injected UserService 2 @Resource (name = "UserService") 3 Private userservice UserService;

The following dependency injection explains very clearly what is dependent, what is injected, or what is controlled rollover

Note: The type of the "userservice" variable in the action declaration must be "Userserviceimpl" or its parent class "UserService", otherwise it cannot be injected due to a type inconsistency because the declaration in the action " UserService "Variable uses the @resource annotation to label and indicates its name =" UserService ", which is tantamount to telling spring that my action is to instantiate a" UserService ", You spring quickly help me instantiate, and then give me, when spring sees the @resource annotation on the userservice variable, according to its specified name property, it is known that an instance of Userserviceimpl is needed in the action. At this point, spring will inject its own created Userserviceimpl instance called "UserService" into the "userservice" variable in the action, helping the action complete the instantiation of the UserService. This will not be passed in the action "userservice userservice = new Userserviceimpl ();" This is the most primitive way to instantiate UserService. If there is no spring, then when the action needs to use Userserviceimpl, it must pass "userservice UserService = new Userserviceimpl ();" The initiative to create an instance object, but after using spring, the action to use Userserviceimpl, you do not have to take the initiative to create an instance of Userserviceimpl, the creation of Userserviceimpl instance has been given to spring to do, Spring has created a good Userserviceimpl instance for action,action to get it straight. The action is used immediately after the original active creation of the Userserviceimpl instance and becomes passive waiting for the Userserviceimpl instance to be created by spring before being injected to action,action. This shows that the action on the "Userserviceimpl" class "control" has been "reversed", the original initiative in their own hands, to use the "Userserviceimpl" class instances, their own initiative to new one out immediately can be used, But now I can't take the initiative to go to the new "Userserviceimpl" class instance, newThe power of an instance of the "Userserviceimpl" class has been taken away by spring, and only spring is able to create an instance of the "Userserviceimpl" class, and the action can wait until spring creates "Userserviceimpl" Class, then "begs" spring to give him an instance of the created "Userserviceimpl" class so he can use "Userserviceimpl", which is spring's core thought "Control Reversal", also called"Dependency Injection"Dependency Injection" is also well understood, the action needs to use USERSERVICEIMPL work, then is the dependence on Userserviceimpl, spring to acion need to rely on the Userserviceimpl injection (that is, "give") To the action, this is called "Dependency injection". For action, the action relies on something, asks spring to inject it to him, and for spring, Spring takes the initiative to inject it into the action.

4, @ Repository

@Repository the corresponding data access layer bean, for example:

1  @Repository (value= "Userdao")2public  classextends Basedaoimpl <User> {3     ... .. 4  }

The @Repository (value= "Userdao") note is to tell spring to let spring create a Userdaoimpl instance named "Userdao".

When the service needs to use the Userdaoimpl instance named "Userdao" created by spring, you can use the @resource (name = "Userdao") annotation to tell Spring that Spring injects the created Userdao to the service.

// Inject Userdao, 2 @Resource (name = "Userdao") is required to remove the specified user from the database based on the user ID basedao<user > Userdao;

5. @Scope

@Scope ("singleton") @Scope ("property")

Spirng using annotations to construct an IOC container

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.