Spring's common annotations and implementation of "Dependency injection"

Source: Internet
Author: User

1. Spring annotations

@Controller declaring the Action component
@Service declaring the Service component @Service ("Mymovielister")
@Repository declaring DAO Components
@Component refer to components when they are not well categorized.
@RequestMapping ("/menu") Request mapping
@Resource for injection, (provided by the Java EE) by default assembly by name, @Resource (name= "Beanname")

@Resource has two important attributes: Name and type, and spring resolves the name property of the @resource annotation to the Bean's
Name. (That is, the Bean's ID name), and the type attribute resolves to the bean. So if you use the Name property, use the ByName auto-injection policy, and use the
The type attribute is used with the Bytype auto-injection policy. If neither name nor the type attribute is specified, the ByName auto-injection policy is used through the reflection mechanism.
For example:
@Resource (name= "Basedao")
Private Basedao Basedao;


@Autowired used for injection, (Srping provided) is assembled by type by default and can be used without the Set,get method

@Autowired is assembled by type by default, you must require that the dependent object must exist by default, and if you want to allow null values, you can set its Required property to False, for example: @Autowired (Required=false),
If we want to use the name assembly can be used in conjunction with @qualifier annotations
As follows:
@Autowired ()
@Qualifier ("Basedao")
Private Basedao Basedao;


@Transactional (Rollbackfor={exception.class}) transaction Management
@ResponseBody
@Scope ("prototype") to set the scope of the bean


2. Use annotations to register beans with the spring container.

You need to register <context:component-scan base-package= "Pagkage1[,pagkage2,..., PagkageN]"/> in Applicationcontext.xml.
such as: Specify a package in Base-package

@1, <context:component-scan base-package= "Com.cmos.ngcard"/>
Indicates that in the Cn.gacl.java package and its child packages, if a class has a specific annotation "@Component/@Repository/@Service/@Controller" on its head, the object is registered as a bean into the spring container. You can also specify multiple packages in the <context:component-scan base-package= ""/>, such as:

@2, <context:component-scan base-package= "Com.cmos.ngcard.dao.impl,com.cmos.ngcard.service.impl, Com.cmos.ngcard.action "/>
Multiple packages are separated by commas.

1), @Component
@Component is a common form of all spring-managed 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:

@Controller
@Scope ("prototype")
public class Useraction extends baseaction<user>{}

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:

@Service ("UserService")
public class Userserviceimpl implements UserService {}
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:

Inject UserService
@Resource (name = "UserService")
Private UserService UserService;


Note: The type of the "userservice" variable in the action declaration must be "Userserviceimpl" or its parent class "UserService". Otherwise, because the type is inconsistent and cannot be injected, because the declared "userservice" variable in the action uses the @resource annotation to label and indicates its name = "UserService", this is tantamount to telling Spring that Say my action 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 can know, An instance of Userserviceimpl is required in action, and spring will inject its own instance of the Userserviceimpl named "UserService" into the action "UserService" Variable, which helps the action complete the instantiation of the userservice so that it does not pass "userservice UserService = new Userserviceimpl () in the action;" 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 instance of the "Userserviceimpl" class, the power of the instance of the new "Userserviceimpl" class has been taken away by spring, only spring can be the instance of the new "Userserviceimpl" class , and the action only waits for spring to create an instance of the "Userserviceimpl" class, and then "begs" spring to give him an instance of the created "Userserviceimpl" class so that he can use "Userserviceimpl", This is the spring core thought "inversion of control," also known as "Dependency Injection", "dependency Injection" is also well understood, action needs to use USERSERVICEIMPL work, then is to Userserviceimpl to produce a dependency, Spring injects the Userserviceimpl that acion needs to rely on (that is, "give") to the action, which 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:

@Repository (value= "Userdao")
public class Userdaoimpl extends basedaoimpl<user> {}
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, which is required to remove the specified user from the database based on the user ID
@Resource (name = "Userdao")
Private basedao<user> Userdao;

5). When there are multiple implementation classes in an interface, the instance binding method
There are two kinds of binding methods
@1, @Qualifier annotations
Through this indicator, it shows which implementation class is what we need.
Http://www.cnblogs.com/smileLuckBoy/p/5801678.html

@2, @Primary
Use @primary to tell spring to choose which specific implementation to prioritize when hesitating.
Http://www.cnblogs.com/myhappylife/p/5647098.html

@3, introducing configuration files
1. @PropertySource
Introducing the. Properties configuration file
such as @propertysource (value= "Rop.appSecret.properties")

2. @ImportResource
Introduction of XML and other configuration files
such as @importresource (locations={"Spring-rop-conf.xml", "Dubbo-consumer.xml", "Dubbo-provider.xml"})





Spring's common annotations and implementation of "Dependency injection"

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.