spring context 例子

本文详细介绍了Spring框架的配置方式,包括各种命名空间的使用、组件扫描、消息转换器配置、缓存管理、事务管理等内容,并展示了如何通过AOP实现日志记录等功能。
<?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:c="http://www.springframework.org/schema/c"
  xmlns:cache="http://www.springframework.org/schema/cache"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  xmlns:jee="http://www.springframework.org/schema/jee"
  xmlns:lang="http://www.springframework.org/schema/lang"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:task="http://www.springframework.org/schema/task"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:util="http://www.springframework.org/schema/util"
  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.xsd
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
    http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">


  <context:component-scan base-package="com.lee" />
  <bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor" />
  <mvc:annotation-driven>
    <mvc:message-converters>
      <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="objectMapper" ref="myJacksonObjectMapper" />
      </bean>
    </mvc:message-converters>
  </mvc:annotation-driven>
 
  <bean id="keyGenerator" class="com.lee.CacheKeyGenerator"/>
  <cache:annotation-driven cache-manager="cacheManager" key-generator="keyGenerator"/>
  
  <tx:annotation-driven transaction-manager="transactionManager" />
  
  <aop:config>
    <aop:aspect id="logAspect" ref="logAspect">
      <aop:pointcut expression="execution(* com.lee.service.impl.*.*(..))" id="service"/>
      <aop:around pointcut-ref="service" method="doAround"/>
      
      <aop:pointcut expression="execution(* com.lee.controller.*.*(..))" id="controller"/>
      <aop:around pointcut-ref="controller" method="doAround"/>
    </aop:aspect>
  </aop:config>
  
  <bean id="logAspect" class="com.fil.ap.api.framework.interceptor.LoggingInterceptor"/>
  
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="myDataSource" />
  </bean>


  <bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/MYDataSource" />
    <property name="resourceRef" value="true" />
  </bean>


  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="myDaffeDataSource" />
    <property name="mapperLocations" value="classpath*:mappers/**/*.xml" />
  </bean>


  <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory" />
  </bean>


  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="annotationClass" value="org.springframework.stereotype.Repository"/>
    <property name="basePackage" value="com.fil.ap.api.hk.retail.mapper" />
    <!-- need below line when there're more than one data sources -->
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  </bean>


  <bean id="myJacksonObjectMapper" class="com.lee.MyJacksonObjectMapper" />  

  <mvc:interceptors>  
    <mvc:interceptor>
      <mvc:mapping path="/*"/>
      <bean id="myInterceptor1" class="com.lee.MyInterceptor1"/>
    </mvc:interceptor>
    <mvc:interceptor>
      <mvc:mapping path="/*"/>
      <bean id="myInterceptor2" class="com.lee.MyInterceptor2"/>
    </mvc:interceptor>
  </mvc:interceptors>

</beans>



----------------------------------------------分割线----------------------------------------------
<!--ehcache-->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache" />
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="classpath:ehcache.xml" p:shared="true" />  

<think>好的,用户想了解Spring Framework中Context的依赖注入原理和用法。首先,我需要回忆一下Spring的核心机制,特别是依赖注入(DI)和控制反转(IoC)的概念。根据提供的引用内容,Spring通过IoC容器管理Bean的创建和依赖关系,这有助于降低耦合。 用户提到的引用[2]指出,Spring使用配置方式(XML、注解、Java配置类)定义Bean及其依赖,容器启动时自动创建和注入。这应该是依赖注入的基本原理。接下来,我需要整理出依赖注入的三种主要方式:构造函数注入、Setter注入和字段注入,如引用[3]所述。 关于Context的作用,引用[5]提到了AnnotationConfigApplicationContext,说明Spring Context是容器的核心,负责Bean的生命周期管理。需要解释Context如何加载配置,初始化Bean,并处理依赖关系。 用法部分,用户可能需要具体的配置示例。比如XML配置中的<bean>标签,注解如@Autowired和@Component,以及Java配置类中使用@Bean注解。这些内容在引用[2]和[5]中都有涉及。 另外,用户的问题可能隐含对实际应用场景或常见问题的兴趣,比如如何解决循环依赖,或者配置方式的选择。不过根据引用内容,可能暂时不需要深入这些细节,但可以简要提及优化方法,如引用[2]提到的各种配置方式的选择。 最后,生成相关问题时,需要考虑用户可能进一步想了解的内容,比如不同注入方式的优缺点,或者Context的高级功能。需要确保问题覆盖原理、实现、应用和常见问题。 需要检查是否有遗漏点,比如Bean的作用域、生命周期回调等,但根据现有引用,可能主要聚焦在依赖注入本身。确保回答结构清晰,分点说明原理和用法,并附上示例代码,如Java配置类的例子。 可能还需要注意术语的一致性,比如“依赖注入”和“控制反转”的关系,引用[4]提到依赖注入是实现控制反转的方式之一,这点需要明确区分。 总结下来,回答的结构应该是:原理部分(IoC容器、配置方式、注入方式),用法部分(配置示例),以及相关问题。需要引用正确的引用标记,比如引用[2]和[3]用于解释原理,引用[5]用于Java配置示例。</think>### Spring Framework Context依赖注入原理与用法 #### 一、依赖注入原理 1. **控制反转(IoC)与依赖注入(DI)** Spring通过IoC容器管理对象的创建和依赖关系,实现控制反转。依赖注入是IoC的具体实现方式之一,将对象间的依赖关系交由容器自动处理[^2][^3]。 2. **核心机制** - **Bean定义**:通过XML、注解(如`@Component`)或Java配置类(`@Configuration`)定义Bean及其依赖关系。 - **容器初始化**:Spring Context(如`AnnotationConfigApplicationContext`)启动时解析配置,创建Bean实例并注入依赖[^5][^2]。 - **依赖注入方式**: - **构造函数注入**:通过构造方法传递依赖。 - **Setter注入**:通过Setter方法赋值。 - **字段注入**:直接通过注解(如`@Autowired`)注入字段。 3. **容器工作流程** ```java // 示例:基于Java配置类的容器初始化 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); MyService service = context.getBean(MyService.class); ``` 容器会执行`BeanFactoryPostProcessor`调整Bean定义,再通过反射实例化Bean并注入依赖[^1][^3]。 --- #### 二、依赖注入用法 1. **XML配置** 传统方式,使用`<bean>`标签定义Bean及其依赖: ```xml <bean id="userService" class="com.example.UserService"> <constructor-arg ref="userRepository"/> </bean> ``` 2. **注解驱动** - **组件扫描**:`@ComponentScan`自动发现带`@Component`、`@Service`等注解的类。 - **注入注解**:`@Autowired`实现自动装配,`@Qualifier`指定具体Bean。 ```java @Service public class UserService { @Autowired private UserRepository userRepository; } ``` 3. **Java配置类** 使用`@Configuration`和`@Bean`显式定义Bean: ```java @Configuration public class AppConfig { @Bean public UserRepository userRepository() { return new UserRepositoryImpl(); } } ``` --- #### 三、优势与典型场景 - **优势**:降低耦合度、提升可测试性、简化配置[^4][^2]。 - **应用场景**: 1. Web应用中的服务层与数据层解耦。 2. 单元测试中通过Mock依赖隔离组件。 3. 动态切换依赖实现(如不同环境的数据源)。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值