飞道的博客

【Spring篇】基于xml的自动装配

275人阅读  评论(0)

🍓个人主页:个人主页

🍒系列专栏:SSM框架

自动装配:
根据指定的策略,在IOC容器中匹配某一个bean,自动为指定的bean中所依赖的类类型或接口类
型属性赋值
①场景模拟
创建类 UserController

    
  1. public class UserController {
  2. private UserService userService;
  3. public void setUserService (UserService userService) {
  4. this.userService = userService;
  5. }
  6. public void saveUser (){
  7. userService.saveUser();
  8. }
  9. }
创建接口 UserService

     
  1. public interface UserService {
  2. void saveUser ();
  3. }
创建类 UserServiceImpl 实现接口 UserService

      
  1. public class UserServiceImpl implements UserService {
  2. private UserDao userDao;
  3. public void setUserDao (UserDao userDao) {
  4. this.userDao = userDao;
  5. }
  6. @Override
  7. public void saveUser (){
  8. userDao.saveUser();
  9. }
  10. }
创建接口 UserDao

       
  1. public interface UserDao {
  2. void saveUser ();
  3. }

创建类 UserDaoImpl 实现接口 UserDao

        
  1. public class UserDaoImpl implements UserDao {
  2. @Override
  3. public void saveUser () {
  4. System.out.println( "保存成功");
  5. }
  6. }

②配置 bean

         
  1. <bean id= "userController" class= "com.atguigu.spring.Controller.UserController">
  2. <property name= "userService" ref= "userService"></property>
  3. </bean>
  4. <bean id= "userService" class= "com.atguigu.spring.Service.impl.UserServiceImpl">
  5. <property name= "userDao" ref= "userDao"></property>
  6. </bean>
  7. <bean id= "userDao" class= "com.atguigu.spring.Dao.impl.UserDaoImpl"></bean>

③测试

          
  1. @Test
  2. public void test2 (){
  3. ConfigurableApplicationContext ioc = new ClassPathXmlApplicationContext( "spring-autowire-xml.xml");
  4. UserController userController = ioc.getBean( "userController", UserController.class);
  5. userController.saveUser();
  6. }

④使用bean标签的autowire属性设置自动装配效果
自动装配方式:byType
byType :根据类型匹配 IOC 容器中的某个兼容类型的 bean ,为属性自动赋值
  • 若在IOC中,没有任何一个兼容类型的bean能够为属性赋值,则该属性不装配,即值为默认值 null
  • 若在IOC中,有多个兼容类型的bean能够为属性赋值,则抛出异常 NoUniqueBeanDefinitionException

           
  1. <bean id= "userController" class= "com.atguigu.spring.Controller.UserController" autowire= "byType">
  2. <!-- <property name= "userService" ref= "userService"></property>-->
  3. </bean>
  4. <bean id= "userService" class= "com.atguigu.spring.Service.impl.UserServiceImpl" autowire= "byType">
  5. <!-- <property name= "userDao" ref= "userDao"></property>-->
  6. </bean>
  7. <bean id= "userDao" class= "com.atguigu.spring.Dao.impl.UserDaoImpl"></bean>

⑤测试

自动装配方式:byName
byName :将自动装配的属性的属性名,作为 bean id IOC 容器中匹配相对应的 bean 进行赋值

            
  1. <bean id= "userController" class= "com.atguigu.spring.Controller.UserController" autowire= "byName">
  2. <!-- <property name= "userService" ref= "userService"></property>-->
  3. </bean>
  4. <bean id= "userService" class= "com.atguigu.spring.Service.impl.UserServiceImpl" autowire= "byName">
  5. <!-- <property name= "userDao" ref= "userDao"></property>-->
  6. </bean>
  7. <bean id= "userDao" class= "com.atguigu.spring.Dao.impl.UserDaoImpl"></bean>

转载:https://blog.csdn.net/Javascript_tsj/article/details/128186976
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场