Spring framework study notes 3: Use annotations instead of configuration files, spring Study Notes

Source: Internet
Author: User

Spring framework study notes 3: Use annotations instead of configuration files, spring Study Notes

1. Import context constraint: spring-context-4.2.xsd

 

2. Open the xml configuration file in design mode, right-click edit namespaces, and click add to add

 

 

After completion, it should be like this:

 

In the configuration file, write as follows:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">    <context:component-scan base-package="bean"></context:component-scan></beans>

 

Use annotations in the class as follows:

Package bean; import javax. annotation. postConstruct; import javax. annotation. preDestroy; import javax. annotation. resource; import org. junit. validator. publicClassValidator; import org. springframework. beans. factory. annotation. autowired; import org. springframework. beans. factory. annotation. qualifier; import org. springframework. beans. factory. annotation. value; import org. springframework. context. annotation. scope; import org. springframework. stereotype. component; import org. springframework. stereotype. controller; import org. springframework. stereotype. repository; import org. springframework. stereotype. service; // the content of the configuration file <bean name = "user" class = "bean. user "/> // @ Component (" user ") // The four types are essentially the same. To facilitate understanding, we recommend that you use the following three types of // @ Service (" user ") // use the service layer // @ Controller ("user") // use the web layer @ Repository ("user ") // use the dao layer // specify the Scope of action of the object @ Scope (scopeName = "singleton") public class User {@ Value ("Tom") // assign a Value to private String name; private Integer age; // @ Autowired // object assignment, automatic assembly // problem: if multiple objects of the same type are involved, @ Resource (name = "car") cannot be distinguished ") // this method can explicitly specify (recommended) private Car; public car getCar () {return Car;} public void setCar (car Car) {this. car = car;} public String getName () {return name;} public void setName (String name) {this. name = name;} public Integer getAge () {return age;} @ Value ("20") // you can assign a Value in the set method. The effect is the same, but does not destroy encapsulation public void setAge (Integer age) {this. age = age ;}@ Override public String toString () {return "User [name =" + name + ", age =" + age + ", car = "+ car +"] ";}@ PostConstruct // initialization method when the init-mothod public void init () {System. out. println ("initialization") ;}@ PreDestroy // destroy method public void destory () {System. out. println ("Destroy ");}}

 

Car class:

package bean;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;@Component("car")public class Car {    @Value("car2")    private String  name;    private String color;        public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getColor() {        return color;    }    public void setColor(String color) {        this.color = color;    }    @Override    public String toString() {        return "Car [name=" + name + ", color=" + color + "]";    }        }

 

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.