<context:annotation-config/>和 <context:component-scan base-package=""/>的区别
时间: 2023-05-11 20:06:19 浏览: 85
<context:annotation-config/>是用来启用注解的支持,它会扫描 Spring 容器中的所有 Bean,查找其中使用了注解的类,并将这些注解解析成相应的 Bean 定义。而<context:component-scan base-package=""/>则是用来扫描指定包下的所有类,查找其中使用了特定注解的类,并将其注册为 Bean。因此,它们的作用略有不同,但都是用来帮助 Spring 容器管理 Bean 的。
相关问题
这是 applicationContext.xml 文件:<?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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- 使用 annotation --> <context:annotation-config /> <context:component-scan base-package="commvc.sinosoft.lis"></context:component-scan> <bean id="requestFactory" class="org.springframework.http.client.SimpleClientHttpRequestFactory"> <property name="connectTimeout" value="5000"/> <property name="readTimeout" value="10000"/> </bean> <!-- 使用 annotation 自动注册bean,并检查@Controller, @Service, @Repository注解已被注入 --> <context:component-scan base-package="com.sinosoft.lisinterface.*" /> <bean id="SpringConfigTool" class="com.sinosoft.lisinterface.common.config.ApplicationContextUtils"/> </beans>
根据提供的 applicationContext.xml 文件,它是一个典型的Spring Framework配置文件。该文件定义了Spring应用程序上下文中的bean和其它配置。
在这个文件中,主要包括以下内容:
1. 命名空间声明:通过 `xmlns` 和 `xmlns:xsi` 声明了几个命名空间,用于引入Spring框架的XML命名空间和XML Schema定义。
2. Schema Location:通过 `xsi:schemaLocation` 属性指定了各个命名空间对应的XML Schema定义的位置。
3. 注解配置:通过 `<context:annotation-config />` 配置开启对注解的支持,这样可以在代码中使用注解来进行依赖注入等操作。
4. 组件扫描:通过 `<context:component-scan>` 配置指定要扫描的包,以自动注册带有 `@Controller`、`@Service`、`@Repository` 注解的类为Spring的bean。
5. bean定义:通过 `<bean>` 元素定义了一个名为 `requestFactory` 的bean,它是 `org.springframework.http.client.SimpleClientHttpRequestFactory` 类的实例,并设置了一些属性。
6. 其他配置:还包括了一个 `SpringConfigTool` 的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" xmlns:util="http://www.springframework.org/schema/util" xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 扫描com.xie下的所有spring的注解--> <context:component-scan base-package="com.xie" /> <!-- 把其他的java等的注解也加入到spring容器管理--> <context:annotation-config/> <!-- 配置文件读取--> <util:properties id="dbConfig" location="classpath:Config.properties"/> <!-- 配置数据源--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="#{dbConfig.driver}"/> <property name="url" value="#{dbConfig.url}"/> <property name="username" value="#{dbConfig.username}"/> <property name="password" value="#{dbConfig.password}"/> </bean> <!-- 创建sessionfacto--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="dataSource" ref="dataSource"/> </bean> <!-- mapper由spring接管--> <!-- 扫描出mapper接口--> <mybatis:scan base-package="com.xie.mapper"/> <!-- 使用注解来完成aop--> <aop:aspectj-autoproxy/> <!-- 使用注释来控制事务--> <tx:annotation-driven/> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 引入数据源--> <property name="dataSource" ref="dataSource"/> </bean> </beans>
这是一个 Spring 配置文件,它包含了以下内容:
1. 声明了 Spring 核心容器的命名空间和 schema 位置;
2. 通过 component-scan 和 annotation-config 扫描和加载指定包下的 Spring 注解和其他注解;
3. 通过 util:properties 声明了一个 id 为 dbConfig 的 properties 文件,用于配置数据源的相关信息;
4. 配置了一个基于 Apache Commons DBCP 的数据源;
5. 配置了一个 MyBatis 的 SessionFactory,指定了 MyBatis 的配置文件和数据源;
6. 配置了 MyBatis 的 Mapper 接口;
7. 使用 aspectj-autoproxy 开启 AOP;
8. 使用 annotation-driven 开启声明式事务,并配置了事务管理器。