1. To configure the annotations under which package to scan in Applicationcontext.xml
<!-- specifies that annotations in all classes under the Cn.itcast.bean report are scanned . - < base-package= "Cn.itcast.bean"></context:component-scan >
The above configuration instructions only scan the Cn.itcast.bean package and the annotations in the child package
2. Note Description (Annotations on entity classes)
PackageCn.itcast.bean;Importjavax.annotation.PostConstruct;ImportJavax.annotation.PreDestroy;ImportJavax.annotation.Resource;Importjavax.xml.ws.RespectBinding;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.beans.factory.annotation.Qualifier;ImportOrg.springframework.beans.factory.annotation.Value;ImportOrg.springframework.context.annotation.Scope;Importorg.springframework.stereotype.Component;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.stereotype.Repository;ImportOrg.springframework.stereotype.Service;//<bean name= "user" class= "cn.itcast.bean.User"/>//@Component ("user")//@Service ("user")//Service Layer//@Controller ("user")//Web Tier@Repository ("User")//DAO Layer//specify the scope of the object@Scope (scopename= "Singleton") Public classUser {PrivateString name; @Value ("18") PrivateInteger age; //@Autowired//Automatic Assembly//problem: If you match multiple types of objects that are consistent. You will not be able to choose which object to inject specifically. //@Qualifier ("Car2")//use @qualifier annotations to tell the spring container which name of the object is automatically assembled@Resource (Name= "Car")//manual injection, specifying which name is injected into the object Privatecar Car; PublicCar Getcar () {returncar; } Public voidSetcar (car car) { This. Car =car; } PublicString GetName () {returnname; } @Value ("Tom") Public voidsetName (String name) { This. Name =name; } PublicInteger getage () {returnAge ; } Public voidsetage (Integer age) { This. Age =Age ; } @PostConstruct//called after the object is created. Init-method Public voidinit () {System.out.println ("I am the initialization Method!"); } @PreDestroy//called before it is destroyed. Destory-method Public voiddestory () {System.out.println ("I am the Destruction Method!"); } @Override PublicString toString () {return"User [name=" + name + ", age=" + Age + ", car=" + Car + "]"; } }
@Component: This annotation is generally placed on the entity class equivalent to the configuration in Applicationcontext.xml <bean name= "user" class= "Cn.itcast.bean.User"/>
@Service: General annotations in the Service layer
@Controller: General annotations on the Web tier
@Repository: General annotations on the DAO layer
@Scope (scopename= "singleton"): Specifies the scope of the object, prototype (multiple cases) and Singleton (Singleton)
These are annotations in the Java EE class, the name is to distinguish between the layer, and there is no specific function, each layer can write the same annotations, but it is not recommended to do so.
3. Note Description (attributes or annotations on methods)
Public classUser {PrivateString name; @Value ("18") PrivateInteger age; //@Autowired//Automatic Assembly//problem: If you match multiple types of objects that are consistent. You will not be able to choose which object to inject specifically. //@Qualifier ("Car2")//use @qualifier annotations to tell the spring container which name of the object is automatically assembled@Resource (Name= "Car")//manual injection, specifying which name is injected into the object Privatecar Car; PublicCar Getcar () {returncar; } Public voidSetcar (car car) { This. Car =car; } PublicString GetName () {returnname; } @Value ("Tom") Public voidsetName (String name) { This. Name =name; } PublicInteger getage () {returnAge ; } Public voidsetage (Integer age) { This. Age =Age ; } @PostConstruct//called after the object is created. Init-method Public voidinit () {System.out.println ("I am the initialization Method!"); } @PreDestroy//called before it is destroyed. Destory-method Public voiddestory () {System.out.println ("I am the Destruction Method!"); } @Override PublicString toString () {return"User [name=" + name + ", age=" + Age + ", car=" + Car + "]"; } }
@Value: Annotations on attributes (underlying attributes, not object properties) are intended to assign an initial value
@Resource (name= "Car"): object property Annotations, manual injection, specifying which name to inject
@Autowired: Automatic assembly, problem: If you match more than one type of object. You will not be able to choose which object to inject specifically.
@Qualifier ("Car2"): Use @qualifier annotations to tell the spring container which name of the object is automatically assembled
@PostConstruct: Called after the object is created. Init-method
@PreDestroy: Called before destroying. Destory-method
Using Annotations in Spring