Spring common annotation usage analysis

Spring does not adopt a strategy of convention over configuration, and spring requires that Java files in the specified search paths be displayed. Spring will register all appropriate java classes as spring beans.

 
Question: How does spring know which Java classes to process as bean classes?
This requires the use of annotations, and spring uses some special annotations to annotate bean classes.
 
@Component: Standard a normal spring Bean class.
@Controller: Annotate a controller component class.
@Service: Annotate a business logic component class.
@Repository: Annotate a DAO component class.
 
By default, the name of the bean instance is the first letter of the bean class in lowercase, and other parts remain unchanged.
 
In future versions of spring, @Controller, @Service, @Repository will carry more semantics. Try to consider using @Controller, @Service, @Repository instead of generic @Component.
 
After specifying that some classes can be used as Spring Bean classes, it is better to let spring search for the specified path. In this case, you need to import the context schema in the spring configuration file and specify a simple search path.
<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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- Automatically scan all bean classes under the specified package and its subpackages-->
<context:component-scan
base-package="org.crazyit.app.service"/>
</beans>
 
We can specify spring bean classes by adding <include-filter...> or <exclude-filter...> sub-elements to <context:component-scan> , as long as the java classes under the specified path satisfy this rule, Even if these java classes do not use any annotation annotations, spring will handle them as bean classes.
 
<include-filter...> : Java classes that satisfy this rule will be processed by the original bean class.
<exclude-filter...> : Specifies that java classes that meet this rule will not be processed by the original bean class.
 
These two child elements have two attributes:
type: Specifies the filter type.
expression: Specifies the expression required by the filter.
 
Spring has built-in support for the following four filters:
annotation:该过滤器要指定一个annotation名,如lee.AnnotationTest。
assignable:类名过滤器,该过滤器直接指定一个java类。
regex:正则表达式过滤器,该过滤器指定一个正则表达式,匹配该正则表达式的java类将满足该过滤规则,如org\.example\.default.*。
aspectj:如org.example..*service+。
<!-- 自动扫描指定包及其子包下的所有Bean类 -->
<context:component-scan
base-package="org.crazyit.app.service">
<!-- 只将以Chinese、Axe结尾的类当成Spring容器中的Bean -->
<context:include-filter type="regex"
expression=".*Chinese"/>
<context:include-filter type="regex"
expression=".*Axe"/>
</context:component-scan>
  @Resource位于java.annotation包下,来自于java EE规范的一个annotation。使用该annotation为目标bean指定协作者Bean。
@Resource详细用法见经典javaEE企业应用实战。
@Resource有一个name属性,在默认情况下,spring将这个值解释为需要被注入的Bean实例的名字。
@Controller
public class demo {
@Resource(name="user")
private User user;
@Resource(name="user")
public void setUser(User user) {
this.user = user;
}
public User getUser() {
return user;
}
}
 
@Resource也可以直接修饰Filed,
如果@Resource修饰Field,这时候连该属性的setter方法就不需要了。
 
使用@Resource可以省略name属性。
修饰方法时,省略name属性,则该name值是该setter方法去掉前面的set字符串,首字母小写后得到的子串。
修饰Field时,省略name属性,则该name与该Field同名。
 
指定Bean实例的作用域。
@Scope:注解也可以指定Bean实例的作用域。
@Controller("demo")
@Scope("prototype")
public class demo {
 
}
  作用范围就那4中填写,不知道的等我博客,最近在回顾spring知识。
 
@PostConstruct和@PreDestory位于java.annotation包下。
在spring中用于定制spring容器中bean的生命周期行为。
@PostConstruct修饰的方法是bean的初始化之前的方法。
@PreDestory修饰的方法是bean销毁之前的方法。
 
 
深刻理解该类使用了@PostConstruct修饰init方法,那么spring就会在该bean的依赖关系注入完成之后回调该方法。
 
@Component
public class SteelAxe
{
public SteelAxe()
{
System.out.println("创建SteelAxe类对象实例...");
}
}
 
 
demo1:
@Component
public class Chinese
{
// 执行Field注入
@Resource(name="steelAxe")
private SteelAxe steeAxe;
 
public Chinese() {
    super();
System.out.println("创建Chinese类对象实例...");
}
@PostConstruct
public void init()
{
System.out.println("正在执行初始化的init方法...");
}
@PreDestroy
public void close()
{
System.out.println("正在执行销毁之前的close方法...");
}
}
 
// 创建Spring容器
AbstractApplicationContext ctx = new
ClassPathXmlApplicationContext("beans.xml");
// 注册关闭钩子
ctx.registerShutdownHook();
 
打印:
创建Chinese类对象实例...
创建SteelAxe类对象实例...
正在执行初始化的init方法...
正在执行销毁之前的close方法...
 
如果注释掉chinese的依赖注入,那么结果如下:
 
@Component
public class Chinese
{
// 执行Field注入
//@Resource(name="steelAxe")
//private SteelAxe steeAxe;    
public Chinese() {
super();
System.out.println("创建Chinese类对象实例...");
}
@PostConstruct
public void init()
{
System.out.println("正在执行初始化的init方法...");
}
@PreDestroy
public void close()
{
System.out.println("正在执行销毁之前的close方法...");
}
}
  打印:
创建Chinese类对象实例...
正在执行初始化的init方法...
创建SteelAxe类对象实例...
正在执行销毁之前的close方法...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326497960&siteId=291194637