Use annotations Development
Explanation
After spring4, you want to use annotation form, have to be introduced aop package
in the configuration file which is also dependent on the introduction of a context constraint
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
Bean added to the vessel
Before we are using the label bean bean injection, but the actual development, we will generally use the comment!
What notes in the package configuration scanning
<!--指定注解扫描包-->
<context:component-scan base-package="com.cong.pojo"/>
In the preparation of classes specified package, using annotation @Value injection properties
@Component("user")// 相当于配置文件中 <bean id="user" class="当前注解的类"/>
public class User {
@Value("cong")// 相当于配置文件中 <property name="name" value="cong"/>
private String name;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
test
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) context.getBean("user");
System.out.println(user.toString());
}
Spread
@Value method may also act on the
Back @Component value can not write, the default is the class of lowercase
@Component
public class User {
private String name;
@Value("cong")
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
Derived comment
These annotations us, is replacing the configuration steps in the configuration file which it! More convenient!
@Component three-derived notes
In order to better stratify, Spring can use the other three notes, the same function, which function is currently using are the same.
- @Controller: web layer
- @Service: service layer
- @Repository: dao layer
Write these notes, the equivalent of this class to manage the assembly of Spring!
Automatic assembly notes
There are four notes, talked about before
@Autowired
Automatically according to the type of injection
If a plurality of the same type of bean, automatically assembling the class name lowercase bean,
@Qualifier
It can not be used alone, with @Autowired, the assembly according to the corresponding bean id
@Resource
- @Resource if specified attribute name, attribute byName the press fitting manner lookup;
- Secondly, then the default byName assembled manner;
- If the above are not successful, press byType way automatic assembly.
- Not successful, reported abnormal.
@Nullable
Fields marked this can be null values
Scope
@scope
@scope
- singleton: default, Spring will use the Singleton pattern to create the object. Closing factories, all objects will be destroyed.
- prototype: Multi-pattern. Closing factories, all objects will not be destroyed. Garbage collection will reclaim internal
@Component
@Scope("singleton")
public class User {
@Value("cong")
private String name;
@PostConstruct//指定初始化方法
public void init(){
System.out.println("对象初始化了...");
}
@PreDestroy//指定销毁方法
public void destroy(){
System.out.println("对象销毁了...");
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
Run again, the results
对象初始化了...
User{name='cong'}
对象销毁了...
summary
XML and compare notes
- XML can be applied to any scene, clear structure, easy maintenance
- Notes not provide their own class can not use, develop simple and convenient
xml and Notes Integration and Development : Recommended Best Practices
- xml management Bean
- Notes injected completed property
- Course, you can not scan, the scan is to comment on the class
<context:annotation-config/>
effect:
- Annotation-driven register, so that the notes into force
- Those bean is used to activate the above comments already registered in the spring container, which is displayed to register with Spring
- If no scan packages, it is necessary to manually configure the bean
- If you do not add annotation-driven, the injection is null!