• [spring]Bean注入——在XML中配置


    Bean注入的方式有两种:

    一、在XML中配置

    • 属性注入
    • 构造函数注入
    • 工厂方法注入

    二、使用注解的方式注入@Autowired,@Resource,@Required

    本文首先讲解在XML中配置的注入方式

    1.属性注入 

    属性注入即通过setXxx()方法注入Bean的属性值或依赖对象,由于属性注入方式具有可选择性和灵活性高的优点,因此属性注入是实际应用中最常采用的注入方式。 

    属性注入要求Bean提供一个默认的构造函数,并为需要注入的属性提供对应的Setter方法。Spring先调用Bean的默认构造函数实例化Bean对象,然后通过反射的方式调用Setter方法注入属性值。 

    java代码:

    public class LogonService implements BeanNameAware{
        private UserDao userDao;
        
        public void setUserDao(UserDao userDao) {
            this.userDao = userDao;
        }
    
        public UserDao getUserDao() {
            return userDao;
        }    
    }

    bean.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-3.0.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-3.0.xsd"
           default-autowire="byName"
             >
       <bean id="userDao" class="com.baobaotao.anno.UserDao"/>
       <bean id="propertyId" class="com.baobaotao.anno.LogonService">
           //ref中的"userDao"为前一行的bean id
           <property name="userDao" ref="userDao"></property>
       </bean>
    </beans>

    2.构造方法注入(使用构造函数注入的前提是Bean必须提供带参数的构造函数)

    java代码:

    public class LogonService implements BeanNameAware{
    
        public LogonService(){}
        //使用构造函数注入的前提是Bean必须提供带参数的构造函数
        public LogonService(LogDao logDao, UserDao userDao) {
            this.logDao = logDao;
            this.userDao = userDao;
        }
    
        private LogDao logDao;
    
        private UserDao userDao;
    
        public void setUserDao(UserDao userDao) {
            this.userDao = userDao;
        }
    
        public void setLogDao(LogDao logDao) {
            this.logDao = logDao;
        }
        
        public LogDao getLogDao() {
            return logDao;
        }
        public UserDao getUserDao() {
            return userDao;
        }
        
    }

    bean.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-3.0.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-3.0.xsd"
           default-autowire="byName">
    
        <bean id="logDao" class="com.baobaotao.anno.LogDao"/>
        <bean id="userDao" class="com.baobaotao.anno.UserDao"/>
       <bean class="com.baobaotao.anno.LogonService">
          <constructor-arg  ref="logDao"></constructor-arg>
          <constructor-arg ref="userDao"></constructor-arg>
       </bean>
    
    </beans>

    3.工厂方法注入

    factory-bean:用于实例化工厂类;

    factory-method:用于调用工厂类方法;

    java代码:

    public class CarFactory {
       //非静态方法
       public Car createCar(){
           Car car = new Car();
           car.setBrand("BMW");
           return car;
       }
       
       //静态方法
       public static Car createStaticCar(){
           Car car = new Car();
           return car;
       }
    }

    bean.xml配置:

    对于非静态方法createCar(必须实例化工厂类(factory-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" 
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
        <!-- 工厂方法-->
        <bean id="carFactory" class="com.baobaotao.ditype.CarFactory" />
        <bean id="car5" factory-bean="carFactory" factory-method="createCar">
        </bean>
    
    
    </beans>

    对于静态方法createStaticCar(无须创建工厂类实例的情况下就可以调用工厂类方法)的注入方式:

    <?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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
        <bean id="car6" class="com.baobaotao.ditype.CarFactory"
            factory-method="createStaticCar"></bean>
    
    </beans>

     

  • 相关阅读:
    Jmeter分布测试
    SQL命令
    Linux执行命令时遇到的些问题
    Linux常用命令总结
    Jenkins与SVN持续集成
    在linux上创建slave节点
    内网域名配置方法
    Java中重写与重载的区别
    MongoDB基本使用
    MongoDB安装
  • 原文地址:https://www.cnblogs.com/vickylinj/p/9475106.html
Copyright © 2020-2023  润新知