Spring3系列7-自动扫描组件或Bean

1、      Spring Auto Scanning Components —— 自动扫描组件    

    1.      Declares Components Manually——手动配置component

    2.      Auto Components Scanning——自动扫描组件

    3.      Custom auto scan component name——自定义扫描组件名称

    4.      Auto Components Scan Antotation Types——自动扫描组件的注释类型

2、      Spring Filter Components In Auto Scanning —— 在自动扫描中过滤组件

    1.      Filter Component——include

    2.      Filter Component——exclude

 

 

1、      Spring Auto Scanning Components —— 自动扫描组件

一般你能够在xml配置文件中,声明一个bean或者component,而后Spring容器会检查和注册你的bean或component。实际上,Spring支持自动扫描bean或component,你能够没必要再在xml文件中繁琐的声明bean,Spring会自动扫描、检查你指定包的bean或component。java

如下列举一个简单的Spring Project,包含了Customer、Service、DAO层,让咱们来看一下手动配置和自动扫描的不一样。spring

1.      Declares Components Manually——手动配置component

先看一下正常手动配置一个beanexpress

DAO层,CustomerDAO.java以下:ide

 
package com.lei.customer.dao;
 
public class CustomerDAO 
{
    @Override
    public String toString() {
        return "Hello , This is CustomerDAO";
    }    
}
 

 

Service层,CustomerService.java以下:this

 
package com.lei.customer.services;
 
import com.lei.customer.dao.CustomerDAO;
 
public class CustomerService 
{
    CustomerDAO customerDAO;
 
    public void setCustomerDAO(CustomerDAO customerDAO) {
        this.customerDAO = customerDAO;
    }
 
    @Override
    public String toString() {
        return "CustomerService [customerDAO=" + customerDAO + "]";
    }
 
}
 

 

 

配置文件,Spring-Customer.xml文件以下:code

 
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
    <bean id="customerService" class="com.lei.customer.services.CustomerService">
        <property name="customerDAO" ref="customerDAO" />
    </bean>
 
    <bean id="customerDAO" class="com.lei.customer.dao.CustomerDAO" />
 
</beans>
 

 

运行以下代码,App.java以下:component

 
package com.lei.common;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.lei.customer.services.CustomerService;
 
public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext context = 
          new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});
 
        CustomerService cust = (CustomerService)context.getBean("customerService");
        System.out.println(cust);
 
    }
}
 

 

输出结果:CustomerService [customerDAO=Hello , This is CustomerDAO]

 

2.      Auto Components Scanning——自动扫描组件

如今,看一下怎样运用Spring的自动扫描。xml

用注释@Component来表示这个Class是一个自动扫描组件。blog

Customer.java以下:get

 
package com.lei.customer.dao;
 
import org.springframework.stereotype.Component;
 
@Component
public class CustomerDAO 
{
    @Override
    public String toString() {
        return "Hello , This is CustomerDAO";
    }    
}
 

 

CustomerService.java以下:

 
package com.lei.customer.services;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.lei.customer.dao.CustomerDAO;
 
@Component
public class CustomerService 
{
    @Autowired
    CustomerDAO customerDAO;
 
    @Override
    public String toString() {
        return "CustomerService [customerDAO=" + customerDAO + "]";
    }
}
 

 

 

配置文件Spring-customer.xml以下

 

 
<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-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 
    <context:component-scan base-package="com.lei.customer" />
 
</beans>
 

 

 

 

注意:

以上xml文件中,加入了“context:component-scan”标签,这样就将Spring的自动扫描特性引入,base-package表示你的组件的存放位置,Spring将扫描对应文件夹下的bean(用@Component注释过的),将这些bean注册到容器中。

       最后运行结果与手动配置的结果一致。

3.      Custom auto scan component name——自定义扫描组件名称

上例中,默认状况下,Spring将把组件Class的第一个字母变成小写,来做为自动扫描组件的名称,例如将“CustomerService”转变为“customerservice”,你能够用“customerService”这个名字调用组件,以下:

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

 

你能够像下边这样,建立自定义的组件名称:

 

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

 

如今,能够调用本身定义的组件了,以下:

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

 

4.      Auto Components Scan Antotation Types——自动扫描组件的注释类型

有4种注释类型,分别是:

@Component      ——表示一个自动扫描component

@Repository              ——表示持久化层的DAO component

@Service             ——表示业务逻辑层的Service component

@Controller        ——表示表示层的Controller component

 

以上4种,在应用时,咱们应该用哪种?让咱们先看一下@Repository、@Service、@Controller的源代码

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

 

你还能够看一下@Service、@Controller的源代码,发现它们都用@Component注释过了,因此,在项目中,咱们能够将全部自动扫描组件都用@Component注释,Spring将会扫描全部用@Component注释过得组件。

       实际上,@Repository、@Service、@Controller三种注释是为了增强代码的阅读性而创造的,你能够在不一样的应用层中,用不一样的注释,就像下边这样。

DAO层:

 
package com.lei.customer.dao;
 
import org.springframework.stereotype.Repository;
 
@Repository
public class CustomerDAO 
{
    @Override
    public String toString() {
        return "Hello , This is CustomerDAO";
    }    
}
 

 

 

Service层:

 
package com.lei.customer.services;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.lei.customer.dao.CustomerDAO;
 
@Service
public class CustomerService 
{
    @Autowired
    CustomerDAO customerDAO;
 
    @Override
    public String toString() {
        return "CustomerService [customerDAO=" + customerDAO + "]";
    }
 
}
 

 

 

2、      Spring Filter Components In Auto Scanning —— 在自动扫描中过滤组件

1.      Filter Component——include

下例演示了用“filter”自动扫描注册组件,这些组件只要匹配定义的“regex”的命名规则,Clasee前就不须要用@Component进行注释。

DAO层,CustomerDAO.java以下:

 
package com.lei.customer.dao;
 
public class CustomerDAO 
{
    @Override
    public String toString() {
        return "Hello , This is CustomerDAO";
    }    
}
 

 

Service层,CustomerService.java以下:

 
package com.lei.customer.services;
 
import org.springframework.beans.factory.annotation.Autowired;
import com.lei.customer.dao.CustomerDAO;
 
public class CustomerService 
{
    @Autowired
    CustomerDAO customerDAO;
 
    @Override
    public String toString() {
        return "CustomerService [customerDAO=" + customerDAO + "]";
    }
 
}
 

 

Spring Filtering,xml配置以下:

 
<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-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 
    <context:component-scan base-package="com.lei" >
 
        <context:include-filter type="regex" 
                       expression="com.lei.customer.dao.*DAO.*" />
 
        <context:include-filter type="regex" 
                       expression="com.lei.customer.services.*Service.*" />
 
    </context:component-scan>
 
</beans>
 

 

注意:

以上xml文件中,全部文件名字,只要包含DAO和Service(*DAO.*,*Service.*)关键字的,都将被检查注册到Spring容器中。

 

运行如下代码:

 
package com.lei.common;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.lei.customer.services.CustomerService;
 
public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext context = 
        new ClassPathXmlApplicationContext(new String[] {"Spring-AutoScan.xml"});
 
        CustomerService cust = (CustomerService)context.getBean("customerService");
        System.out.println(cust);
 
    }
}
 

 

运行结果:CustomerService [customerDAO=Hello , This is CustomerDAO]

 

2.      Filter Component——exclude

你也能够用exclude,制定组件避免被Spring发现并被注册到容器中。

如下配置排除用@Service注释过的组件

<context:component-scan base-package="com.lei.customer" >
        <context:exclude-filter type="annotation" 
            expression="org.springframework.stereotype.Service" />        
</context:component-scan>

 

如下配置排除包含“DAO”关键字的组件

<context:component-scan base-package="com.lei" >
        <context:exclude-filter type="regex" 
            expression="com.lei.customer.dao.*DAO.*" />        
</context:component-scan>
相关文章
相关标签/搜索