Use annotations instead of Spring configuration files Specific steps
Create a WEB project - import package - introduce a new namespace (constraint) for the main configuration file - enable the use of annotation proxy configuration file - use annotations in the class to complete the configuration
1 guide package
springsource.org.aopalliance-1.0.0.jar
springsource.org.apache.commons.logging-1.1.1.jar
springsource.org.apache.log4j-1.2.15.jar
springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
spring-aop-4.2.4.RELEASE.jar
spring-aspects-4.2.4.RELEASE.jar
spring-beans-4.2.4.RELEASE.jar
spring-context-4.2.4.RELEASE.jar
spring-core-4.2.4.RELEASE.jar
spring-expression-4.2.4.RELEASE.jar
spring-test-4.2.4.RELEASE.jar
2 Introduce a new namespace (constraint)-context constraint to the main configuration file
3 Enable the use of annotation proxy configuration files
<!-- Specifies to scan annotations in all classes under the com.hciot.bean package. Note: When scanning the package, it will scan all descendant packages under the specified report --> <context:component-scan base-package="com.hciot.bean"></context:component-scan>4 Use annotations in the class to complete the configuration
4.1 Register the object with the container
// Equivalent to <bean name="user" class="com.hciot.bean.User"></bean> in the configuration file @Component("user") //@Service("user") //service层 //@Controller("user") //web层 //@Repository("user") //Dao层
Test result User [name=null, age=null, car=null]4.2 Modifying the scope of an object
//Specify the scope of the object
//@Scope(scopeName="prototype")
@Scope(scopeName="singleton")
4.3
value type injection
//Inject the property value into the object @Value("tom") public void setName(String name) { this.name = name; } @Value("28") public void setAge(Integer age) { this.age = age; }
Test result User [name=tom, age=28, car=null]4.4 reference type injection
Inject the car object into the container
package com.hciot.bean; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("car") public class Car { private String name; private String color; @Value("BMW") public void setName(String name) { this.name = name; } @Value("Yellow") public void setColor(String color) { this.color = color; } @Override public String toString() { return "Car [name=" + name + ", color=" + color + "]"; } }
@Autowired // Automatic assembly, but when there are multiple objects of the same type, it will not be possible to choose which object to inject
private Car car;
Test result User [name=tom, age=28, car=Car [name=BMW, color=yellow]]
@Autowired @Qualifier("car2")//Use the Qualifier annotation to tell the spring container which object to autowire private Car car;
Test result User [name=tom, age=28, car=Car [name=Benz, color=yellow]]
@Resource(name="car2") //Manual injection, specify which object to inject private Car car;
4.5 Initialization|Destruction method
@PostConstruct//Called after the object is created
public void init(){
System.out.println("I am the initialization method");
}
I am the initialization method User [name=tom, age=28, car=Car [name=Benz, color=yellow]] Destruction method
appendix
test code
package com.hciot.a_annotation; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.hciot.bean.User; public class Demo { @Test public void fun1(){ //1. Create a container object ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); //ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); //2. Ask the container for the user object User u = (User) ac.getBean("user"); User u2 = (User) ac.getBean("user"); User u3 = (User) ac.getBean("user"); //3. Print the user object System.out.println(u2); ac.close(); //System.out.println(u2==u3); } }User object
package com.hciot.bean; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource; 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; // Equivalent to <bean name="user" class="com.hciot.bean.User"></bean> in the configuration file @Component("user") //@Service("user") //service层 //@Controller("user") //web层 //@Repository("user") //Dao层 //Specify the scope of the object //@Scope(scopeName="prototype") @Scope(scopeName="singleton") public class User { private String name; private Integer age; //@Autowired //@Qualifier("car2")//Use the Qualifier annotation to tell the spring container which object to autowire @Resource(name="car2") private Car car; public String getName() { return name; } public Integer getAge() { return age; } //Inject the property value into the object @Value("tom") public void setName(String name) { this.name = name; } @Value("28") public void setAge(Integer age) { this.age = age; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } @PostConstruct//Called after the object is created public void init(){ System.out.println("I am the initialization method"); } @PreDestroy//Called before the object is destroyed public void destroy(){ System.out.println("Destruction method"); } @Override public String toString() { return "User [name=" + name + ", age=" + age + ", car=" + car + "]"; } }configuration file
<?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 "> <!-- Specifies to scan annotations in all classes under the com.hciot.bean package. Note: When scanning the package, it will scan all descendant packages under the specified report --> <context:component-scan base-package="com.hciot.bean"></context:component-scan> <bean name="car2" class="com.hciot.bean.Car"> <property name="name" value="奔驰"></property> <property name="color" value="黄色"></property> </bean> </beans>