将Spring 配置成如下这样. 就表示 Spring 将扫描所有的com.yourhz下面的Bean.
那么Bean应当如何写才能被他扫描到呢。我们看如下一个例子。
在类名上面加如下一句,则表示这是一个受制于Spring 的 Bean
@Controller
如果写成这样的,估计表示Bean的句柄名称为 core.upload
@Controller("core.upload")
写成如下形式也行。
@Service注释表示定义一个bean,自动根据bean的类名实例化一个首写字母为小写的bean,例如Chinese实例化为chinese,American实例化为american,如果需要自己改名字则:@Service("你自己改的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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<!-- use annotation auto regist beans -->
<context:component-scan base-package="com.yourhz" />
</beans>
那么Bean应当如何写才能被他扫描到呢。我们看如下一个例子。
在类名上面加如下一句,则表示这是一个受制于Spring 的 Bean
@Controller
如果写成这样的,估计表示Bean的句柄名称为 core.upload
@Controller("core.upload")
写成如下形式也行。
@Service注释表示定义一个bean,自动根据bean的类名实例化一个首写字母为小写的bean,例如Chinese实例化为chinese,American实例化为american,如果需要自己改名字则:@Service("你自己改的bean名")。
本文介绍如何使用Spring的context:component-scan进行Bean的自动扫描与注册,并通过@Controller与@Service注解示例说明如何定义和命名Bean。
134

被折叠的 条评论
为什么被折叠?



