Spring Bean 自动装配
spring自动装配可以简化spring配置。
<bean> 元素的 autowire 属性。autowire 属性有五个值
自动装配的5中类型
名称 | 说明 |
---|---|
byName | 根据 Property 的 name 自动装配 ,如果一个 Bean 的 name 和另一个 Bean 中的 Property 的 name 相同,则自动装配这个 Bean 到 Property 中。注意:set方法名 |
byType | 根据 Property 的数据类型(Type)自动装配,如果一个 Bean 的数据类型兼容另一个 Bean 中 Property 的数据类型,则自动装配。 |
constructor | 根据构造方法的参数的数据类型,进行 byType 模式的自动装配。 |
autodetect | 如果发现默认的构造方法,则用 constructor 模式,否则用 byType 模式 |
no | 默认情况下,不使用自动装配,Bean 依赖必须通过 ref 元素定义。 |
ByName自动装配
Java代码
public class User {
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User name=" + name ;
}
}
public class UserDaoImpl implements UserDao{
@Override
public User getUser() {
return new User("自动装配ByName");
}
}
public class UserServiceImpl implements UserService{
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public User getUser() {
return userDao.getUser();
}
}
public class TestSpringAutowire {
public static void main(String[] args) {
ApplicationContext act= new ClassPathXmlApplicationContext("/config/spring.xml");
UserServiceImpl userService=(UserServiceImpl)act.getBean("userService");
User user = userService.getUser();
System.out.println(user.getName());
}
}
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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id ="userDao" class="cn.lingyiwin.dao.impl.UserDaoImpl"/>
<bean id ="userService" class="cn.lingyiwin.service.impl.UserServiceImpl" autowire="byName"/>
</beans>
<!--或-->
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd" default-autowire="byName">
<bean id ="userDao" class="cn.lingyiwin.dao.impl.UserDaoImpl"/>
<bean id ="userService" class="cn.lingyiwin.service.impl.UserServiceImpl"/>
</beans>
执行结果:
注意:ByName自动装配 service 中的userDao 是通过set方法设置的,xml中userDao要和sevice中属性名称一样。
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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="cn.lingyiwin.dao.impl.UserDaoImpl"/>
<bean id ="userService" class="cn.lingyiwin.service.impl.UserServiceImpl" autowire="byType"/>
</beans>
注意:通过类型装配,需要确保类型只有一个,如果有多个类型,就报错了,举例如下:
UserDao增加一个实现类:
public class UserDaoMySqlImpl implements UserDao{
@Override
public User getUser() {
return null;
}
}
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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="cn.lingyiwin.dao.impl.UserDaoImpl"/>
<bean class="cn.lingyiwin.dao.impl.UserDaoMySqlImpl"/>
<bean id ="userService" class="cn.lingyiwin.service.impl.UserServiceImpl" autowire="byType"/>
</beans>
执行结果:
警告: Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService' defined in class path resource [config/spring.xml]: Unsatisfied dependency expressed through bean property 'userDao': : No qualifying bean of type [cn.lingyiwin.dao.UserDao] is defined: expected single matching bean but found 2: cn.lingyiwin.dao.impl.UserDaoImpl#0,cn.lingyiwin.dao.impl.UserDaoMySqlImpl#0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [cn.lingyiwin.dao.UserDao] is defined: expected single matching bean but found 2: cn.lingyiwin.dao.impl.UserDaoImpl#0,cn.lingyiwin.dao.impl.UserDaoMySqlImpl#0
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1303)
constructor 装配
修改java代码:
public class UserServiceImpl implements UserService{
private UserDao userDao;
public UserServiceImpl(UserDao userDao) {
this.userDao = userDao;
}
@Override
public User getUser() {
return userDao.getUser();
}
}
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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="cn.lingyiwin.dao.impl.UserDaoImpl"/>
<bean id ="userService" class="cn.lingyiwin.service.impl.UserServiceImpl" autowire="constructor"/>
</beans>
执行结果: