没有自动装配的Spring组件扫描? [英] Spring component-scan without autowiring?
问题描述
在弹簧上不使用自动装配而无需进行组件扫描是否有意义?
Does it makes sense to use component-scan WITHOUT using autowiring on spring?
我喜欢在控制器的MVC环境上进行组件扫描的想法,我甚至想避免在xml上声明所有DAO,服务....
I like the idea of having component scan on an MVC environment for controllers, hell I even like to avoid declaring all the DAOs, Services ... on the xml.
但是,我不是自动装配的忠实拥护者,因此,仍然可以使用xml配置文件手动注入所需的bean,还是只是废话?
However I am not a big fan of autowiring, so, would it still be possible to manually inject the desired beans using the xml config file or is it just a non-sense?
更新:使用@Autowired代替通过XML进行显式声明有什么好处(请不要提供较少的xml配置"作为优势)
UPDATE : What would be the advantage of using @Autowired instead of explicit declaration via XML (please do not provide "less xml configuration" as an advantage)
推荐答案
我会
- 将
@Component
添加到实现类中,以便为每个实现类创建一个bean - 不要将
@Autowired
/@Resource
与其他类中与这些实现相对应的成员声明相关联,以使Spring不能autowire
它们. - 在XML中使用
<bean>
节点和<property>
元素通过setter注入它们,或者通过<constructor-arg>
通过构造函数注入它们,定义需要这些组件bean的bean.
- add
@Component
to the implementation classes so that a bean will get created for each of them - not associate
@Autowired
/@Resource
with the member declarations corresponding to these implementations in other classes so that Spring cannotautowire
them. - define the beans where these component beans are needed, in XML using the
<bean>
node and<property>
elements to inject them through setters or<constructor-arg>
to inject them through constructors.
示例:
接口:
package com.krovi.compscan;
public interface MyInterface
{
void method();
}
声明为Spring DI框架的组件的实现,以为此创建一个bean:
Implementation declared as component for Spring DI framework to create a bean for this:
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,但通过构造文件通过构造文件显式注入.
A typical client who would use the component-based bean but injected explicitly through configuration file, through constructors.
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通过配置文件显式注入:
Another typical client who would use the component-based bean but injected explicitly through configuration file using setters:
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>
这篇关于没有自动装配的Spring组件扫描?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!