来源 :http://www.jroller.com/habuma/date/20070516
spring 2.0 配置文件
<?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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.1.xsd">
<context:component-scan
base-package="com.springinaction.chapter01.knight" />
</beans>
[b]<context:component-scan>[/b] is a new configuration element in the "context" namespace (which, BTW, is also new in Spring 2.1). This modest little element tells Spring to scan the "com.springinaction.chapter01.knight" package (as specified by the base-package attribute) and any subpackage looking for classes that might be annotated with @Component (new in Spring 2.1), @Repository (new in Spring 2.0), or @Aspect (provided by @AspectJ). If it finds any such class, it automatically will register the class as a bean in the Spring application context. So, although <context:component-scan> takes up so very little space in the XML configuration, it could be responsible for configuring dozens, hundreds, or even thousands of beans in Spring.
[b]@Component[/b] tells Spring that this class should be automatically registered in the Spring context. By default, the class name is used as the bean's ID, but here I've explicitly specified that the bean should have an ID of "knight".
Next, notice that the setQuest() method is annotated with [b]@Autowired[/b]. This tells Spring that this setter shold automatically be wired with a bean reference. The autowiring is "byType", so there will need to be a Quest bean in the Spring context. There's not one there now, but I'll add one soon.
package com.springinaction.chapter01.knight;
import org.springframework.stereotype.Component;
spring 2.0 配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="quest"
class="com.springinaction.chapter01.knight.HolyGrailQuest"/>
<bean id="knight"
class="com.springinaction.chapter01.knight.KnightOfTheRoundTable">
<constructor-arg value="Bedivere" />
<property name="quest" ref="quest" />
</bean>
<bean id="minstrel"
class="com.springinaction.chapter01.knight.Minstrel"/>
<aop:config>
<aop:aspect ref="minstrel">
<aop:pointcut
id="questPointcut"
expression="execution(* *.embarkOnQuest(..)) and target(bean)" />
<aop:before
method="singBefore"
pointcut-ref="questPointcut"
arg-names="bean" />
<aop:after-returning
method="singAfter"
pointcut-ref="questPointcut"
arg-names="bean" />
</aop:aspect>
</aop:config>
</beans>
<?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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.1.xsd">
<context:component-scan
base-package="com.springinaction.chapter01.knight" />
</beans>
[b]<context:component-scan>[/b] is a new configuration element in the "context" namespace (which, BTW, is also new in Spring 2.1). This modest little element tells Spring to scan the "com.springinaction.chapter01.knight" package (as specified by the base-package attribute) and any subpackage looking for classes that might be annotated with @Component (new in Spring 2.1), @Repository (new in Spring 2.0), or @Aspect (provided by @AspectJ). If it finds any such class, it automatically will register the class as a bean in the Spring application context. So, although <context:component-scan> takes up so very little space in the XML configuration, it could be responsible for configuring dozens, hundreds, or even thousands of beans in Spring.
[b]@Component[/b] tells Spring that this class should be automatically registered in the Spring context. By default, the class name is used as the bean's ID, but here I've explicitly specified that the bean should have an ID of "knight".
@Autowired
private Quest quest;
Next, notice that the setQuest() method is annotated with [b]@Autowired[/b]. This tells Spring that this setter shold automatically be wired with a bean reference. The autowiring is "byType", so there will need to be a Quest bean in the Spring context. There's not one there now, but I'll add one soon.
package com.springinaction.chapter01.knight;
import org.springframework.stereotype.Component;
@Component
public class HolyGrailQuest implements Quest {
public void embark() {
System.out.println("Embarking on the quest for the Holy Grail!");
}
}
@Resource(name="grailQuest")
public void setQuest(Quest quest) {
this.quest = quest;
}