zoukankan      html  css  js  c++  java
  • Spring_自动组件扫描和 基于注解配置bean

    自动组件扫描 

    启用Spring组件扫描功能。
    使用@Component注释来表示这是类是一个自动扫描组件。 
    package com.tanlei.dao;
    
    
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Repository;
    
    @Component或者是@Repository
    public class CustomerDao {
        
        @Override
        public String toString() {
            // TODO Auto-generated method stub
            return "hello ,this is CustomerDao";
        }
    }
    package com.tanlei.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Service;
    
    import com.tanlei.dao.CustomerDao;
    
    @Component或是@Service
    public class CustomerService {
        
        @Autowired
        CustomerDao customerDao;
        
        @Override
        public String toString() {
            // TODO Auto-generated method stub
            return "CustomerService [customerDAO=" + customerDao + "]";
        }
    }

    将这个“context:component”在bean配置文件,这意味着,在 Spring 中启用自动扫描功能。base-package 是指明存储组件,Spring将扫描该文件夹,并找出Bean(注解为@Component)并注册到 Spring 容器。

    扫描指定的资源:

     resource-pattern="service/*.class"

    <?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-4.0.xsd">
      
        <context:component-scan base-package="com.tanlei.service"></context:component-scan>
       <context:component-scan base-package="com.tanlei.dao"></context:component-scan>
    
    </beans>

    执行

    package com.tanlei.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.tanlei.service.CustomerService;
    
    public class App {
    
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "beans-spring.xml" });
    
            CustomerService cust = (CustomerService) context.getBean("customerService");
            System.out.println(cust);
        }
    
    }

    结果

    自定义自动扫描组件名称

    要创建组件的自定义名称,你可以这样自定义名称:

    @Service("AAA")
    public class CustomerService 
    ...

    现在,可以用'AAA'这个名称进行检索。

    CustomerService cust = (CustomerService)context.getBean("AAA");

    自动组件扫描注释类型 

    在Spring2.5中,有4种类型的组件自动扫描注释类型
    • @Component – 指示自动扫描组件。
    • @Repository – 表示在持久层DAO组件。
    • @Service – 表示在业务层服务组件。
    • @Controller – 表示在表示层控制器组件。

    因此,使用哪一个?其实并不那么重要。参见 @Repository,@Service 或 @Controller 源代码。

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Component
    public @interface Repository {
    
        String value() default "";
    
    }

    你可能会发现,所有的 @Repository, @Service 或 @Controller 被注解为 @Component。因此,我们可以只使用 @Component 对所有组件进行自动扫描?是的,Spring会自动扫描所有组件的 @Component 注解。

    它工作正常,但不是一个好的做法,为便于阅读,应该始终声明@Repository,@ Service 或 @Controller 在指定的层,使你的代码更易于阅读
     
     
     
     
    <
    context:exclude-filter
    >  子节点指定排除哪些指定表达式的组件
    
    <
    
    context:include-filter
    子节点指定包含哪些表达式的组件,该子节点需要use-default-filters="true"配合使用
    >

    1.过滤组件 - 包含 

    <context:component-scan base-package="com.tanlei">
            <context:include-filter type="regex" 
                           expression="com.tanlei.dao.*DAO.*" />
    
            <context:include-filter type="regex" 
                           expression="com.tanlei.service.*Service.*" />
        </context:component-scan>

    2.过滤组件 - 不包含 

    另外,您还可以排除指定组件,以避免 Spring 检测和 Spring 容器注册。不包括在这些文件中标注有 @Service 。
     
    <context:component-scan base-package="com.yiibai.customer"   >
            <context:exclude-filter type="annotation" 
                expression="org.springframework.stereotype.Service" />        
        </context:component-scan>

    不包括那些包含DAO这个词组文件名。

    <context:component-scan base-package="com.yiibai" >
            <context:exclude-filter type="regex" 
                expression="com.yiibai.customer.dao.*DAO.*" />        
        </context:component-scan>

    注意事项

    找不到bean可以使用   @Autowired(required=false)

    IOC有相同类型的bean   @Repository("bean的名字")

                @Qualifier("指定一个名字")

                setter方法

  • 相关阅读:
    Could note find result map com.xxxx.entity.UserAccountDO
    浏览器通过file://访问文件和通过http://访问文件有什么区别
    FreeMarker template error: The following has evaluated to null or missing: ==> blogger.md [in template "admin/about.ftl" at line 44, column 84]
    2018-01-03 --活动观赏鱼的2017总结及2018年展望
    eclipse open call hierarchy无效
    Mysql 查询重复的记录
    oracle用户、权限操作
    Oracle 修改表操作
    Python 魔法方法详解
    Python __dict__和vars()
  • 原文地址:https://www.cnblogs.com/tanlei-sxs/p/10132559.html
Copyright © 2011-2022 走看看