spring可以通过编写XML来配置Bean,也可以通过使用spring的注解来装配Bean。
1、自动装配与自动检测:
自动装配:让spring自动识别如何装配bean的依赖关系,减少对<property>元素的使用。
自动检测:让spring自动识别哪些类需要配置成spring Bean,减少对<bean>元素的使用。
2、自动装配的4种类型:
byName:把与bean名字(ID)相同的其他Bean自动装配到Bean的对应属性中。
byType:把与bean 类型相同的其他Bean自动装配到Bean的对应属性中。
constructor:把与Bean的构造器入参具有相同类型的其他Bean装配到对应属性中。
autodetect:首先尝试constructor进行自动装配,然后再尝试byType.
假设spring配有一个Bean:
<bean id="userService" class="com.test.UserServiceImpl">
</bean>
对应于类 com.test.UserService:
public class UserServiceImpl{
private UserDao userDao; //属性
...
}
若配置的为byName,则spring会将Bean的id 为userDao 的Bean自动装配到userService bean的userDao的属性中。
若配置的为byType,则spring会将Bean的class类型为UserDao的Bean自动装配到userService bean的userDao的属性中。
若配置的为constructor,则类com.test.UserService中要有构造函数,如:
public class UserServiceImpl{
private UserDao userDao; //属性
//构造函数
pulic UserServiceImpl(UserDao userDao){
this.userDao = userDao;
}
}
那么spring会将类型为UserDao的bean装配到属性userDao中。
写个基于注解自动装配,maven工程如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOYAAAIfCAIAAADrCjcUAAAgAElEQVR4nO2dfXAT573v9U//z8ztTA9/3Elf0ombTJPiYIc4xOGcew6mp3dKbjs+Q2ZC4pJWN6+NoUmAYIIRGAw4jQiOcW+DY9I4MRgIJScGElIDvtjQkwRjSPG1i23ZvMjCAlm2rJVFuvePlVbPPi+7q7fd51n/PvNMRlrt8+wq/rC7kp7v/lwhABAKlwwAQgHKAoKRS2ULljfkcDQAoGJK2YLlDY+t3yvL8mPr9+p4CcoCFmBW2YLlDc/vPKI8kGX5/IE11NVyvHcAQGBW2SWv71WOtaqXPftXk6uxxxjwlrhc7vYMdrHd7cLJaBzAGZhVdu+JvxUsbzj4fy8pXo6eXPf/Dq94e0OtLMtLXt+rrsYcYsBbUlJSkp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J3MnZiamJiga9gVlecEiZcfDMwXLG4pe2F30wu7x8IzyoGB5w3h4Rl9ZRJPkU3wirLoa+gLrsQpWVMywLyjLCxYpe/2mpBxiC5Y3vHngv9THY6FYLpQlQ16grGOxQtntq0oUZd888F8/f31fwfKGn7++TxH3+k1p+6oSRj/0mhN9QlwYUMLftMcD3hLtqsQCdl9QlhssUnYkkFBWMVV9MBIwUNbtpn360vn4ZUJZRHn1HwIoKwxWKLtx5YKRgHTo9OCZvhtf+yYOnR782jdxpu/GodODIwFp48oFjH4QggUoWKTskD865JeQ/6YaKAukhRXKVlWW6jdGP1AWoACTDwHBAGUBwQBlAcEAZQHBAGUBwfj/sMmsCoXK8jcAAAAASUVORK5CYII=" alt="" />
UserServiceImpl类:
import org.springframework.beans.factory.annotation.Autowired;
import com.test.dao.UserDao;
import com.test.domain.User;
public class UserServiceImpl implements UserService {
@Autowired //自动装配Bean,使用注解
private UserDao userDao;
public void save(User user) {
userDao.Save(user);
System.out.println("saved!");
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
自动装配Bean是通过设置自动装配的类型来为Bean的属性装配依赖。所以在beans.xml中只指定Bean而不需要显式指定属性<property>.
beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-autowire="byName">
<context:annotation-config /> <!--由于使用了注解,因此需要配置 -->
<bean id="userDao" class="com.test.dao.UserDaoImpl"></bean>
<bean id="userService" class="com.test.service.UserServiceImpl"></bean>
<!-- <context:component-scan base-package="com.test"></context:component-scan> -->
</beans>
3、自动检测Bean
首先修改beans.xml,使用<context:component-scan>替代<bean>
<context:annotation-config />
<!-- <bean id="userDao" class="com.test.dao.UserDaoImpl"></bean> -->
<!-- <bean id="userService" class="com.test.service.UserServiceImpl"></bean> -->
<context:component-scan base-package="com.test"></context:component-scan>
修改UserDaoImpl,同样修改UserServiceImpl.
@Component //spring会自动检测
//@Qualifier("userDao")
public class UserDaoImpl implements UserDao {
public void Save(User user) {
System.out.println("userName:"+user.getUserName());
System.out.println("password:"+user.getPassword());
}
}
4、限定依赖
当有足够多的Bean满足装配条件,@Autowired注解没办法选择哪一个Bean才是它真正需要的,因此可以配合使用@Qualifier注解
如,除了UserDaoImpl实现接口UserDao,现在还有UserDaoImpl2实现,为UserDaoImpl2使用@Qualifier注解限定。
import com.test.domain.User;
@Qualifier("userDaoOther") //限定
public class UserDaoImpl1 implements UserDao{
public void Save(User user) {
System.out.println("userName1:"+user.getUserName());
System.out.println("password1"+user.getPassword());
}
}
在UserServiceImpl中,若需要装配UserDaoImpl2,除了使用@Autowired自动装配,还需要使用@Qualifier("userDaoOther")限定依赖
@Component("userService")
public class UserServiceImpl implements UserService {
@Autowired
@Qualifier("userDaoOther") //限定依赖,最后依赖的Bean为UserDaoImpl2.
private UserDao userDao;
public void save(User user) {
userDao.Save(user);
System.out.println("saved!");
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}