作用域、延迟加载、数据装配与包扫描的方式创建bean

发布于:2024-05-04 ⋅ 阅读:(25) ⋅ 点赞:(0)

一、作用域

  • 容器内部默认使用的是单例模式
    • 两种模式
      • 单例模式
          <bean id ="SomeService" class="org.example.SomeService" scope="singleton" ></bean> 
          # 备注
            # scope= "singleton" 单例模式,默认
            # scope= "prototype" 单例模式,默认
        
      • 多例模式
        <bean id ="SomeService" class="org.example.SomeService" scope="prototype" ></bean> 
        

二、延迟加载

    <bean id ="SomeService" class="org.example.SomeService" scope="prototype" lazy-init="true"></bean>
    # 备注
      # lazy-init="true" 什么时候用对象,什么时候创建对象

三、类型装配

  • 值类型装配
    • application.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 ="SomeService" class="org.example.SomeService" scope="prototype" lazy-init="true">
                 
                   <constructor-arg name="name" value="xiaoming"></constructor-arg>
          
              </bean>
      
      
``` - 接口类 ``` package org.example;
    import java.util.List;
    
    public interface ISomeService {
        void doSome(); 
    }

  ```
  • 实现类
      package org.example;
    
      import java.util.List;
      
      public class SomeService implements ISomeService{
         
          private String name;
          
          public SomeService (String name)
          {
              this.name = name;
          }
          @Override
          public void doSome() {
              System.out.println("SomeService ----name值:"+name);
          }
      
        
      }
    
    
  • main
      ApplicationContext context =  new ClassPathXmlApplicationContext("applicationContext.xml");
      ISomeService iSomeService =  context.getBean("SomeService",ISomeService.class); 
      iSomeService.doSome();
    
  • 引用类型装配
    • application.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 ="SomeService" class="org.example.SomeService" scope="prototype" lazy-init="true">
                <property name="lists">
                    <list>
                        <value>1</value>
                        <value>2</value>
                        <value>3</value>
                        <value>4</value>
                    </list>
                </property> 
        
            </bean>
        
        </beans>
      
    • 接口类
        package org.example;
      
        import java.util.List;
        
        public interface ISomeService { 
            List<String> getList();
        }
      
      
    • 实现类
      package org.example;
      
        import java.util.List;
        
        public class SomeService implements ISomeService{
            private List<String> lists; 
            public void setLists(List<String> lists) {
                this.lists = lists;
            }
         
        
            @Override
            public List<String> getList() {
                return lists;
            }
        } 
      
    • main
        package org.example;
      
        import org.springframework.context.ApplicationContext;
        import org.springframework.context.support.ClassPathXmlApplicationContext;
        
        public class Main {
            /**
             * main
             * @param args
             */
            public static void main(String[] args) {
                ApplicationContext context =  new ClassPathXmlApplicationContext("applicationContext.xml");
                ISomeService iSomeService =  context.getBean("SomeService",ISomeService.class);
                System.out.println(iSomeService.getList()); 
            }
        }
      

四、包扫描的方式创建bean

  • applicationContextScanning.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.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context.xsd">
       <context:component-scan base-package="com.scanningPackage"/>
   </beans>
  • 接口类
         package com.scanningPackage;
    
         public interface ISomeService {
             void doSome();
         }
    
    
  • 实现类
         package com.scanningPackage;
    
         import org.springframework.stereotype.Service;
         
         @Service
         public class SomeService implements ISomeService{
             @Override
             public void doSome() {
                 System.out.println("SomeService--doSome");
             }
         }
    
    
    • 注解注入
      • @Repository
        • 主要标记在数据访问层对应的实现类
      • @Service
        • 主要标记在业务逻辑层对应的实现类
      • @Controller
        • 主要标记在控制层对应的实现类
      • @Component
        • 不清楚该层数据哪一类
      • @Autowired [spring 框架提供的]
        • 使用该注解完成依赖注入
      • @Resource [java 提供]
        • 使用该注解完成依赖注入
  • main
    package com.scanningPackage;
    
     import org.springframework.context.ApplicationContext;
     import org.springframework.context.support.ClassPathXmlApplicationContext;
     
     public class Test {
         private static ISomeService iSomeService;
         public static void main(String[] agrs) {
             ApplicationContext context =  new ClassPathXmlApplicationContext("applicationContextScanning.xml");
             iSomeService = context.getBean("someService",ISomeService.class);
             iSomeService.doSome();
         }
     }
    

网站公告

今日签到

点亮在社区的每一天
去签到