簡體   English   中英

沒有自動裝配的Spring組件掃描?

[英]Spring component-scan without autowiring?

在彈簧上不使用自動布線而使用元件掃描是否有意義?

我喜歡在控制器的MVC環境上進行組件掃描的想法,我甚至想避免在xml上聲明所有DAO,服務...。

但是,我不是自動裝配的忠實擁護者,因此,是否仍然可以使用xml配置文件手動注入所需的bean,還是只是廢話?

更新:使用@Autowired而不是通過XML進行顯式聲明的優勢是什么(請不要提供“較少的xml配置”作為優勢)

我會

  • @Component添加到實現類中,以便為每個實現類創建一個bean
  • 不要@Autowired / @Resource與其他類中與這些實現相對應的成員聲明相關聯,以使Spring無法對其進行autowire
  • 使用<bean>節點和<property>元素在XML中定義需要這些組件bean的bean,這些元素通過setter注入,或者通過<constructor-arg>通過構造函數注入。

例:

接口:

package com.krovi.compscan;

public interface MyInterface
{
    void method();
}

聲明為Spring DI框架的組件的實現,以為此創建一個bean:

package com.krovi.compscan;

import org.springframework.stereotype.Component;

@Component
public class MyImpl implements MyInterface
{
    public void method()
    {
        System.out.println("Method definition in the implementation");
    }
}

一個典型的客戶端,它將使用基於組件的Bean,但通過配置文件,構造函數顯式注入。

package com.krovi.compscan;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyClient
{
    private MyInterface myInterface;

    public MyClient(MyInterface i)
    {
        this.myInterface = i;
    }

    public void clientMethod()
    {
        myInterface.method();
    }

    // Main method to test.    
    public static void main(
        String[] args)
    {
        ClassPathXmlApplicationContext context =
            new ClassPathXmlApplicationContext("classpath:META-INF/compscan.xml");
        MyClient client =
            context.getBean(MyClient.class);
        client.clientMethod();
    }
}

另一個典型的客戶端,它將使用基於組件的Bean,但使用setter通過配置文件顯式注入:

package com.krovi.compscan;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyAnotherClient
{
    private MyInterface impl;

    protected MyAnotherClient()
    {

    }

    public MyInterface getImpl()
    {
        return impl;
    }

    public void setImpl(
        MyInterface impl)
    {
        this.impl = impl;
    }

    public void clientMethod()
    {
        impl.method();
    }

    public static void main(
        String[] args)
    {
        ClassPathXmlApplicationContext context =
            new ClassPathXmlApplicationContext("classpath:META-INF/compscan.xml");
        MyAnotherClient client =
            context.getBean(MyAnotherClient.class);
        client.clientMethod();
    }
}

Bean配置文件:

<?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-3.0.xsd">

    <!-- Component scan to create beans for @Component classes -->
    <context:component-scan base-package="com.krovi.compscan" />

    <!-- Explicit bean declaration for clients to get the 
         @Component classes injected 
    -->
    <bean id="myClient" class="com.krovi.compscan.MyClient">
        <constructor-arg ref="myImpl" />
    </bean>
    <bean id="anotherClient" class="com.krovi.compscan.MyAnotherClient">
        <property name="impl" ref="myImpl" />
    </bean>
</beans>

Xml聲明優先於注釋,因此您不能同時使用兩者。

但是,如果您不想使用DI,為什么還要通過注釋聲明組件? 這是沒有意義的。

它是可修復的,並且可以根據您的喜好來聲明組件“ Spring bean”:

  1. 使用組件掃描

  2. 使用XML編寫

您還可以定義組件之間的關系:

  1. 使用@Autowired

  2. 在XML中使用或

這兩個動作是不同的,就像初始化bean和在另一個bean的屬性中設置bean的引用一樣,將“ component-scan”與“ @Autowired”一起使用的唯一要點是@Autowired正在使用類型和“ component-scan”不會強制生成的Bean的ID或名稱,因此最好將兩者一起使用,但是如果要使用scan而沒有自動裝配,則必須注意不要使用兩個具有相同類名的組件,還要注意輸入中的任何錯字寫下參考名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM