Spring 依赖注入
Spring框架的核心功能之一就是通过依赖注入的方式来管理Bean之间的依赖关系。
1、属性注入
IService:
1 public interface IUserService { 2 /* 3 * 说话 4 */ 5 void Say(String msg); 6 }
ServiceImp:
1 public class UserServiceImpl implements IUserService { 2 3 private IUserDao iUserDao; 4 5 public void setUserDao(IUserDao dao) { 6 this.iUserDao=dao; 7 } 8 9 /* 10 * 说话 11 */ 12 public void Say(String msg) { 13 this.iUserDao.Say(msg); 14 } 15 }
IDao:
1 public interface IUserDao { 2 /* 3 * 说话 4 */ 5 void Say(String msg); 6 }
DaoImp:
1 public class UserDaoImpl implements IUserDao { 2 /* 3 * 说话 4 */ 5 public void Say(String msg) { 6 System.out.println(msg); 7 } 8 }
Bean:
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://www.springframework.org/schema/beans 4 http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 6 <bean id="userServerId" class="serviceImpl.UserServiceImpl"> 7 <!-- 此处是set属性注入,name值必须和UserServiceImpl中属性名一致 --> 8 <property name="userDao" ref="userDaoId"></property> 9 </bean> 10 </beans>
测试输出:
1 public class MainShow { 2 public static void main(String[] args) { 3 System.out.println("开始执行。。。"); 4 5 ApplicationContext context=new ClassPathXmlApplicationContext("resources/beans.xml"); 6 IUserService idog=(IUserService)context.getBean("userServerId"); 7 idog.Say("汪汪汪。。。"); 8 } 9 }
2、构造注入
IService:
1 public interface IDogService { 2 /* 3 * 叫 4 */ 5 void Call(String msg); 6 }
ServiceImp:
1 public class DogServiceImpl implements IDogService { 2 3 private IDogDao idogdao; 4 5 public DogServiceImpl(DogDaoImpl dogDao) { 6 System.out.println("开始构造注入....."); 7 this.idogdao=dogDao; 8 } 9 10 /* 11 * 叫声 12 */ 13 public void Call(String msg) { 14 idogdao.Call(msg); 15 } 16 }
IDao:
1 public interface IDogDao { 2 /* 3 * 叫 4 */ 5 void Call(String msg); 6 }
DaoImp:
1 public class DogDaoImpl implements IDogDao { 2 3 public void Call(String msg) { 4 System.out.println("汪汪汪:"+msg); 5 } 6 }
Bean:
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://www.springframework.org/schema/beans 4 http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 6 <!-- 构造注入 --> 7 <bean id="dogId" class="serviceImpl.DogServiceImpl"> 8 <constructor-arg ref="dogDao"/> 9 </bean> 10 11 <bean id="dogDao" class="daoImpl.DogDaoImpl"></bean> 12 </beans>
3、内部注入
IService:
1 public interface ICarService { 2 /* 3 * 汽车启动 4 */ 5 void Start(); 6 }
ServiceImp:
1 private ICarDao idao; 2 3 public void setIdao(CarDaoImpl impl) { 4 this.idao = impl; 5 } 6 7 public void Start() { 8 idao.Start(); 9 } 10 }
IDao:
1 public interface ICarDao { 2 /* 3 * 汽车启动 4 */ 5 void Start(); 6 }
DaoImp:
1 public class CarDaoImpl implements ICarDao { 2 3 public void Start() { 4 System.out.println("汽车开始启动。。。。。。"); 5 } 6 }
Bean:
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://www.springframework.org/schema/beans 4 http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 6 <!-- 内部注入 --> 7 <bean id="carId" class="serviceImpl.CarServiceImpl"> 8 <property name="idao"> 9 <bean class="daoImpl.CarDaoImpl"></bean> 10 </property> 11 </bean> 12 </beans>
4、自动装配
在Spring框架,可以用 auto-wiring 功能会自动装配Bean。要启用它,只需要在 <bean> 定义“autowire”属性。
<beans/> 标签可以定义 default-autowire-candidate="false" 属性让它和它包含的所有 bean 都不做为候选bean。
1 <bean id="customer" class="com.yiibai.common.Customer" autowire="byName" />
在Spring中,支持 5 自动装配模式。
- no – 缺省情况下,自动配置是通过“ref”属性手动设定
- byName – 根据属性名称自动装配。如果一个bean的名称和其他bean属性的名称是一样的,将会自装配它。
- byType – 按数据类型自动装配。如果一个bean的数据类型是用其它bean属性的数据类型,兼容并自动装配它。
- constructor – 在构造函数参数的byType方式。
- autodetect – 如果找到默认的构造函数,使用“自动装配用构造”; 否则,使用“按类型自动装配”。
ServiceImp:
1 public class UserServiceImpl implements IUserService { 2 3 private IUserDao userDao; 4 5 public void setUserDao(UserDaoImpl dao) { 6 this.userDao=dao; 7 } 8 9 /* 10 * 说话 11 */ 12 public void Say(String msg) { 13 this.userDao.Say(msg); 14 } 15 }
IDao:
1 public interface IUserDao { 2 /* 3 * 说话 4 */ 5 void Say(String msg); 6 }
DaoImp:
1 public class UserDaoImpl implements IUserDao { 2 /* 3 * 说话 4 */ 5 public void Say(String msg) { 6 System.out.println(msg); 7 } 8 }
通常情况下,进行明确的装配目标 Bean
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://www.springframework.org/schema/beans 4 http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 6 7 <bean id="userServerId" class="serviceImpl.UserServiceImpl"> 8 <!-- 此处是set属性注入,name值必须和UserServiceImpl中属性名一致 --> 9 <property name="userDao" ref="userDaoId"></property> 10 </bean> 11 12 <bean id="userDaoId" class="daoImpl.UserDaoImpl"></bean> 13 </beans>
1、由类型 (Type) 自动装配:
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://www.springframework.org/schema/beans 4 http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 6 <!-- Type自动装配注入 --> 7 <bean id="userServerId" class="serviceImpl.UserServiceImpl" autowire="byType"/> 8 <bean id="userDaoId" class="daoImpl.UserDaoImpl"></bean> 9 </beans>
2、由类型 (Name) 自动装配:
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://www.springframework.org/schema/beans 4 http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 6 <!-- Name 自动装配注入 --> 7 <bean id="userServerId" class="serviceImpl.UserServiceImpl" autowire="byName"/> 8 <bean id="userDao" class="daoImpl.UserDaoImpl"></bean> 9 </beans>
3、构造函数自动装配:
ServiceImp:
1 public class DogServiceImpl implements IDogService { 2 3 private IDogDao idogdao; 4 5 public DogServiceImpl(DogDaoImpl dogDao) { 6 System.out.println("开始构造注入....."); 7 this.idogdao=dogDao; 8 } 9 10 /* 11 * 叫声 12 */ 13 public void Call(String msg) { 14 idogdao.Call(msg); 15 } 16 }
IDao:
1 public interface IDogDao { 2 /* 3 * 叫 4 */ 5 void Call(String msg); 6 }
DaoImp:
1 public class DogDaoImpl implements IDogDao { 2 3 public void Call(String msg) { 4 System.out.println("狗叫:"+msg); 5 } 6 }
通常情况下,使用构造注入Bean,配置如下:
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://www.springframework.org/schema/beans 4 http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 6 <!-- 构造注入 --> 7 <bean id="dogId" class="serviceImpl.DogServiceImpl"> 8 <constructor-arg ref="dogDao"/> 9 </bean> 10 <bean id="dogDao" class="daoImpl.DogDaoImpl"></bean> 11 </beans>
自动装配用构造函数启用后,你可以不设置构造器属性。Spring会找到兼容的数据类型,并自动装配它。
<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"> <!-- Constructor 构造自动装配 --> <bean id="dogId" class="serviceImpl.DogServiceImpl" autowire="constructor"/> <!-- 有无Id都可以 --> <bean id="dogDaos" class="daoImpl.DogDaoImpl"></bean> </beans>