Spring的注入-从配置文件到注解
2014-11-10 22:04
351 查看
在Spring之前 在一个对象中调用另外一个类,需要在其内部实例化这个类,这样的结果是一旦类的数目增加代码的耦合度将变的非常高。为了降低耦合度,我们引入了Spring,Spring可以通过注入来降低代码的耦合度。即把生成对象的控制权交给容器,代码要做的只是使用这些对象进行逻辑或者持久化操作,这就是控制反转(IOC).
在Java文件中,需要添加一个setter函数
同样的在相应的Java文件中要加上一个构造器
其中default-autowire有如下四种类型的自动装配。
byName
byType
constructor
autodetect
在Java代码中庸@Autowired注解,应用中必须只能有一个Bean适合装配到@Autowired 注解所标注的属性或参数中,就像上面的脑图中的方式@Autowired+@Quality或者@inject+@Name
Java文件中要怎么编写才能让容器发现,有三种方式可以选择
默认情况下类被自动发现并注册bean的条件是使用@Componment,@Repository,@Service,@Controller或者使用自定义的注解
在配置文件中修改上面的行为。即过滤器包括include-filter,exclude-filter
use-default-filters = "false" 禁用自动发现与注册
Java文件编写实例:
默认情况下bean的名字是类名的第一个字母小写的名字,当然也可以自己定义bean的id。
后记。可能本文只记录了个这个过程中的大概,很多细节没有补上,比如bean的scope,以及init-mehod和destory-method,后面会继续添砖加瓦的。
1.配置文件阶段。
1.1 手动注入
虽然解决了在代码中显示生成对象的操作,但是我们仍然要为每个bean配置它所依赖的其他bean。1.1.1 设值注入
<bean id="injectionService" class="com.xyf.InjectionServiceImpl"> <property name="injectionDAO" ref="injectionDao"></property> </bean> <bean id="injectionDao" class="com.xyf.InjectionDaoImpl"></bean>
在Java文件中,需要添加一个setter函数
//设值注入 public void setInjectionDAO(InjectionDAO injectionDAO) { this.injectionDAO = injectionDAO; }
1.1.2 构造器注入
<bean id="injectionService" class="com.xyf.InjectionServiceImpl"> <constructor-arg ref="injectionDao"></constructor-arg> </bean> <bean id="injectionDao" class="com.xyf.InjectionDaoImpl"></bean>
同样的在相应的Java文件中要加上一个构造器
//构造器注入 public InjectionServiceImpl(InjectionDAO injectionDAO1) { this.injectionDAO = injectionDAO1; }
1.2自动装配
1.2.1 在配置文件中自动装配,参见如下的配置文件
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-autowire="constructor"> </beans>
其中default-autowire有如下四种类型的自动装配。
byName
byType
constructor
autodetect
1.2.2 在Spring3.0中可以使用注解来代替编写XML的方式来进行自动装配。(@AutoWired)
为了能够检测到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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <context:annotation-config/> </beans>
在Java代码中庸@Autowired注解,应用中必须只能有一个Bean适合装配到@Autowired 注解所标注的属性或参数中,就像上面的脑图中的方式@Autowired+@Quality或者@inject+@Name
1.3自动检测
Java文件中要怎么编写才能让容器发现,有三种方式可以选择默认情况下类被自动发现并注册bean的条件是使用@Componment,@Repository,@Service,@Controller或者使用自定义的注解
在配置文件中修改上面的行为。即过滤器包括include-filter,exclude-filter
use-default-filters = "false" 禁用自动发现与注册
Java文件编写实例:
@Component public class BeanInterfaceImpl implements Beaninterface { } @Component public class BeanInvoker { @Autowired public List<Beaninterface> list; @Autowired public Map<String,Beaninterface> map; //数据操作 ...
默认情况下bean的名字是类名的第一个字母小写的名字,当然也可以自己定义bean的id。
后记。可能本文只记录了个这个过程中的大概,很多细节没有补上,比如bean的scope,以及init-mehod和destory-method,后面会继续添砖加瓦的。
相关文章推荐
- Spring中的注入注解Resource/Autowired以及配置文件中default-autowire参数详解
- Spring AOP注解通过@Autowired,@Resource(J2EE提供),@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法
- Spring autoWire resource Spring配置文件问题 自定义注解 并注入spring
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- Spring 全注解配置 bean 和 调用 (7) properties文件注入
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- Spring 3.0 学习-DI 依赖注入_创建Spring 配置-使用一个或多个XML 文件作为配置文件,使用自动注入(byName),在代码中使用注解代替自动注入,使用自动扫描代替xml中bea
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法
- spring注解注入properties配置文件
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解
- [java][spring]取得通过配置文件和注解注入的bean、取得当前数据库连接、取得Appli
- [java][spring]取得通过配置文件和注解注入的bean、取得当前数据库连接、取得ApplicationContext的方法
- [java][spring]取得通过配置文件和注解注入的bean、取得当前数据库连接、取得ApplicationContext的方法
- Spring继承注入与加载多个配置文件