about annotations

本文介绍了Spring框架中配置文件的使用,包括bean的定义、依赖注入和组件扫描的功能。通过示例展示了如何使用@Component注解自动注册bean,并利用<aop:aspect>元素实现面向切面编程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

来源 :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"
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值