一、Spring中三种配置方法的混合使用
项目如下:
pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.liaoxiang</groupId>
<artifactId>spring_2-5_config_anno_xml</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>
</project>
spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userDao" class="com.liaoxiang.dao.UserDao"></bean>
</beans>
//@Configuration
@ComponentScan("com.liaoxiang")
@ImportResource("classpath:spring.xml")
public class AppConfig {
}
public class UserDao {
public void find(){
System.out.println("查询成功!");
}
}
@Service
public class UserService {
@Autowired
private UserDao userDao;
public void findUser(){
userDao.find();
}
}
public class UserController {
public static void main(String[] args) {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
UserService userService = (UserService)ac.getBean("userService");
userService.findUser();
}
}
执行main方法
二、Spring中的自动装配
项目结构还是如上,修改UserService类
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void findUser(){
userDao.find();
}
}
使用自动装配:default-autowire="byType"
<?xml version="1.0" encoding="UTF-8"?>
<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="byType">
<bean id="userDao" class="com.liaoxiang.dao.UserDao"></bean>
<bean id="userService" class="com.liaoxiang.service.UserService"></bean>
</beans>
成功执行main方法,在配置userService的bean标签时,并没有指定注入的userDao对象,Spring框架帮我们进行了自动装配
当UserService中使用的是UserDao的接口的话,且这个接口有多个实现类的时候,spring.xml使用default-autowire="byType"
<?xml version="1.0" encoding="UTF-8"?>
<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="byType">
<bean id="userDao1" class="com.liaoxiang.dao.impl.UserDaoImpl1"></bean>
<bean id="userDao2" class="com.liaoxiang.dao.impl.UserDaoImpl2"></bean>
<bean id="userService" class="com.liaoxiang.service.UserService"></bean>
</beans>
执行方法,错误如下:
此时容器中有两个UserDao 的实现类对象,按照类型注入的话,都是UserDao的实现类类型,spring框架就不知道该注入哪一个。
此时可以修改default-autowire="byType"
为default-autowire="byName"
,按照bean的名称来注入,注入是依赖set方法的,bean的名称是UserService中set方法去掉set后字母小写(setUserDao1 —> userDao1)
<?xml version="1.0" encoding="UTF-8"?>
<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="byName">
<bean id="userDao1" class="com.liaoxiang.dao.impl.UserDaoImpl1"></bean>
<bean id="userDao2" class="com.liaoxiang.dao.impl.UserDaoImpl2"></bean>
<bean id="userService" class="com.liaoxiang.service.UserService"></bean>
</beans>
public class UserService {
private UserDao userDao;
public void setUserDao1(UserDao userDao) {
this.userDao = userDao;
}
public void findUser(){
userDao.find();
}
}
修改为方法名为:setUserDao1
后执行
修改为setUserDao2
后执行
当default-autowire="byType"
的时候,也可以在单独的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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byType">
<bean id="userDao1" class="com.liaoxiang.dao.impl.UserDaoImpl1"></bean>
<bean id="userDao2" class="com.liaoxiang.dao.impl.UserDaoImpl2"></bean>
<bean id="userService" class="com.liaoxiang.service.UserService" autowire="byName"></bean>
</beans>
指定全局使用byType
的方式注入,UserService
中的依赖属性使用bybName
的方式注入,修改方法名为setUserDao1
和setUserDao2
运行依然成功