zoukankan      html  css  js  c++  java
  • spring常用注解使用解析

    spring常用注解使用解析

     

    spring没有采用约定优于配置的策略,spring要求显示指定搜索哪些路径下的Java文件。spring将会把合适的java类全部注册成spring Bean。

     
    问题:spring怎么知道把哪些Java类当初bean类处理?
    这就需要使用annotation,spring使用一些特殊的annotation来标注bean类。
     
    @Component:标准一个普通的spring Bean类。
    @Controller:标注一个控制器组件类。
    @Service:标注一个业务逻辑组件类。
    @Repository:标注一个DAO组件类。
     
    Bean实例的名称默认是Bean类的首字母小写,其他部分不变。
     
    在spring未来的版本中,@Controller,@Service,@Repository会携带更多语义。尽量考虑使用@Controller,@Service,@Repository代替通用的@Component。
     
    指定了某些类可作为Spring Bean类使用后,最好还需要让spring搜索指定路径,此时需要在spring配置文件中导入context Schema,并指定一个简单的搜索路径。
    复制代码
    <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">
    <!-- 自动扫描指定包及其子包下的所有Bean类 -->
    <context:component-scan
    base-package="org.crazyit.app.service"/>
    </beans>
    复制代码
    我们可以通过为<context:component-scan>添加<include-filter...>或<exclude-filter...>子元素来指定spring bean类,只要位于指定路径下的java类满足这种规则,即使这些java类没有使用任何annotation标注,spring一样会将他们当初bean类来处理。
     
    <include-filter...>:满足该规则的java类会被当初bean类处理。
    <exclude-filter...>:指定满足该规则的java类不会被当初bean类处理。
     
    这两个子元素有两个属性:
    type:指定过滤器类型。
    expression:指定过滤器所需要的表达式。
     
    spring内建支持如下四种过滤器:
    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方法...
  • 相关阅读:
    import pymongo exceptions.ImportError: No module named pymongo
    单个回调函数中返回多个Request以及Item
    linux下将jpg,jpeg格式转为PDF
    thinkphp中SQLSTATE[42S02]: Base table or view not found: 1146 Table错误解决方法
    Example015实现html中checkbox的全选和反选(2)
    Exameple014实现html中checkbox的全选,反选和全不选(1)
    关于DOM中的model(将元素转成对象进行操作)
    Example013操作样式
    Example012点击修改属性
    Example011表单中修改内容
  • 原文地址:https://www.cnblogs.com/keyi/p/6797450.html
Copyright © 2011-2022 走看看