using annotations to construct an IOC container
to use annotations to Spring Container Registration Bean . Need to Register <context:component-scan base-package= "pagkage1[in Applicationcontext.xml, Pagkage2,..., Pagkagen] "/>.
such as: Specify a package in Base-package
1 <context:component-scan base-package= "Cn.gacl.java"/>
Indicates the Cn.gacl.java package and its child packages, if a class has a specific annotation "@Component/@Repository/@Service/@Controller" on its head, This 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:
1 <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 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 @Controller2 @Scope ("prototype") 3 public class Useraction extends baseaction<user>{4 ... 5}
Use @Controller annotation identifies useraction , which means to put useraction Give Spring Container Management, in Spring there will be a name in the container named "Useraction" of Action , this name is based on Useraction The class name to fetch. Note: If @Controller does not specify its value @Controller, defaults to Bean first letter of class name for this class is lowercase , If you specify value @Controller (value= "useraction") " or " @Controller ("Useraction"), Use the Value as Bean 's name .
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, avoiding The thread safety of the action in struts. Spring default scope is a singleton mode (scope= "singleton"), This will only create an action object, each access is the same action object, the data is unsafe,struts2 is to ask every time you visit all correspond to different Action , scope= "Prototype" can guarantee when there's a request all Create a Action Object
3. @ Service
The @Service corresponds to a business layer bean, for example:
1 @Service ("UserService") 2 public class Userserviceimpl implements UserService {3 ... 6?
The @Service ("UserService") annotation is to tell spring that when spring creates an instance of Userserviceimpl, the bean's name must be called "userservice" so that when the action needs to use the Userserviceimpl instance, you can create a good "userservice" from spring and inject it into the action: the action only needs to declare a variable named "UserService" to receive the " UserService "can be, the specific code is as follows:
1//injected UserService2 @Resource (name = "UserService") 3 private UserService userservice;
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 new "UserServiceImpl "class instance, new" Userserviceimpl "class instance power has been taken away by spring, only spring can be new" Userserviceimpl "class instances, and action can only wait for spring to create a good" Userserviceimpl "The instance of the class, and then" begged "spring to give him the example of creating a good" Userserviceimpl "class so that he could use" Userserviceimpl ", which is spring's core idea"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.
When to use @resource in spring, when to use @service when you need to define a class as a bean, then the class name in this class uses @service ("XXX"), which is equivalent to saying that the class is defined as a bean,bean called XXX; When you need to define a property in a class, and the property is an existing bean, to assign a value to the property or to inject it with @resource (name= "xxx") on that property, it is equivalent to injecting a bean named XXX for that property.
4, @ Repository
@Repository the corresponding data access layer bean, for example:
1 @Repository (value= "Userdao") 2 public class Userdaoimpl extends 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.
1//injection Userdao, the user ID from the database to remove the specified user needs to use 2 @Resource (name = "Userdao") 3 private basedao<user> Userdao;
Common notes for Spring Learning (Goto)