Tag: Ram mode uses target Hiberna without a package resource member
Here we will elaborate on the use of annotation-annotations to assemble beans.
Because if you learn to annotate, you never want to manually configure the XML file, let's look at the charm of annotation.
Let's take a look at the previous Bean annotation Assembly example:
Package Com.eco.service;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.service;import Com.eco.dao.userdao;import Com.eco.model.User; @Servicepublic class UserService { //@Autowired private Userdao Userdao; public void Setuserdao (Userdao userdao) { This.userdao = Userdao; } public void Useradd (User newuser) { userdao.adduser (newuser); }}
Package Com.eco.daoimp;import Org.hibernate.session;import Org.hibernate.transaction;import Org.springframework.stereotype.repository;import Com.eco.hibernate.hibernatesessionfactory;import Com.eco.dao.userdao;import Com.eco.model.User; @Repositorypublic class Usertodo1 implements Userdao {public void AddUser (user user) { session session = Hibernatesessionfactory.getsession (); Transaction Transaction = Session.begintransaction (); Session.save (user); Transaction.commit (); Session.close (); System.out.println ("Todo1 Create the User");} }
Since this is just a comment, the comments inside the method are omitted in order for the code to look more clearly.
1. Component annotations
@controller Controller (injection service)
@service service (injected DAO)
@repository DAO (Implementing DAO Access)
@component (generally referred to as components, we can use this annotation when the component is poorly categorized.) )
In fact, at the beginning of the annotation design, was defined four component annotations, each component annotation declares the function of the component, but for some reason, these designs
Thought is not really realized, so that the above four component annotations are the same, no difference;
So when you can't figure out whether the class that is currently being annotated is the DAO layer, service layer, or controller layer, it is recommended to use @component annotations;
Or all component classes are annotated with @component, otherwise, if the DAO layer class you use the @service annotation, although the code can run normally, but the Ming-eye
People know that you are not professional, even the DAO layer and service layer can not be confused.
2.Autowired annotations
member variables, setter methods, construction methods (interface injection for dependency injection, setter injection, constructor injection) using @autowired annotations will allow
Spring to do one thing: go to all @component annotation classes and find the same type as mine, and assemble into the bean, it is bytype by default, so
If you annotate @component for multiple classes of the same type, Spring says: I've found a lot of the same type as you, so what to do.
And then the gorgeous to error.
So we recommend that you give it a name when you use a component annotation in @component ("Eco"), and if you don't, spring defaults to the component
Name, which is the first letter of the component class name lowercase.
So in @autowired (name= "Eco"), this explicitly declares that the injected component name is Eco, so there is no error, but you do not intentionally inject a type that does not
Match, then it's boring.
3.Resource annotations
@Resource is a more powerful annotation than the @autowired annotation and is easier to understand;
@Autowired (name= "Eco") [email protected] (name= "Eco"), the two annotation functions are the same, can be implemented here to inject a named
The Class (Resource) of the ECO.
As a result, several annotations can be implemented to implement the functionality of the XML file to manually assemble the bean.
Components use @component annotations, which require injection of dependencies (resources) using @resource annotations.
Source: http://www.cnblogs.com/eco-just/p/7852230.html
(GO) Java Spring (IOC) annotation Assembly Bean