您的位置:首页 > 编程语言 > Java开发

Spring的注入-从配置文件到注解

2014-11-10 22:04 351 查看
在Spring之前 在一个对象中调用另外一个类,需要在其内部实例化这个类,这样的结果是一旦类的数目增加代码的耦合度将变的非常高。为了降低耦合度,我们引入了Spring,Spring可以通过注入来降低代码的耦合度。即把生成对象的控制权交给容器,代码要做的只是使用这些对象进行逻辑或者持久化操作,这就是控制反转(IOC).



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,后面会继续添砖加瓦的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐