Spring对IoC的实现


一、set注入

  • set注入,基于set方法实现的,底层会通过反射机制调用属性对应的set方法然后给属性赋值。这种方式要求属性必须对外提供set方法。

1.Spring的第一个程序

  • pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.gdb</groupId>
        <artifactId>spring6-dependency</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <properties>
            <maven.compiler.source>17</maven.compiler.source>
            <maven.compiler.target>17</maven.compiler.target>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
        <dependencies>
        	<!-- spring的核心依赖,context是一个需要core、aop、jcl、beans等核心依赖 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>6.1.4</version>
            </dependency>
            <!-- junit的依赖,主要用来测试 -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.13.2</version>
                <scope>test</scope>
            </dependency>
            <!-- lombok的依赖,用来生成set、get、构造方法等 -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.30</version>
            </dependency>
            <!--log4j2的依赖,主要用来记录日志的-->
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>2.23.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-slf4j2-impl</artifactId>
                <version>2.23.0</version>
            </dependency>
        </dependencies>
    </project>
    
  • 添加log4j2的日志文件

    • 在类的根路径下提供log4j2.xml配置文件(文件名固定为:log4j2.xml,文件必须放到类根路径下。)
    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
    
        <loggers>
            <!--
                level指定日志级别,从低到高的优先级:
                    ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF
            -->
            <root level="DEBUG">
                <appender-ref ref="spring6log"/>
            </root>
        </loggers>
    
        <appenders>
            <!--输出日志信息到控制台-->
            <console name="spring6log" target="SYSTEM_OUT">
                <!--控制日志输出的格式-->
                <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss SSS} [%t] %-3level %logger{1024} - %msg%n"/>
            </console>
        </appenders>
    </configuration>
    
  • ArticleDao

    package com.gdb.spring6.dependency.dao;
    
    public interface ArticleDao {
        void insert();
    }
    
  • MySQLForArticleDao

    package com.gdb.spring6.dependency.dao.impl;
    
    import com.gdb.spring6.dependency.dao.ArticleDao;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class MySQLForArticleDao implements ArticleDao {
        private static final Logger logger = LoggerFactory.getLogger(MySQLForArticleDao.class);
    
        public void insert() {
            logger.info("MySQL 数据库插入了一条数据.....");
        }
    }
    
  • OracleForArticleDao

    package com.gdb.spring6.dependency.dao.impl;
    
    import com.gdb.spring6.dependency.dao.ArticleDao;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class OracleForArticleDao implements ArticleDao {
        private static final Logger logger = LoggerFactory.getLogger(OracleForArticleDao.class);
    
        public void insert() {
            logger.info("Oracle 数据库插入了一条数据.....");
        }
    }
    
  • ArticleService

    package com.gdb.spring6.dependency.service;
    
    import com.gdb.spring6.dependency.dao.ArticleDao;
    import lombok.Setter;
    
    @Setter
    public class ArticleService {
        private ArticleDao articleDao;
    
        public void insert(){
            articleDao.insert();
        }
    }
    
  • spring.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">
        <!--
            spring是通过调用类的无参数构造方法来创建对象的,所以要想让spring给你创建对象,必须保证无参数构造方法是存在的。
            spring的配置文件可以有多个,在ClassPathXmlApplicationContext构造方法的参数上传递文件路径即可。(变长参数)
            ApplicationContext的超级父接口BeanFactory。
        -->
        <bean id="MySQLForArticleDao" class="com.gdb.spring6.dependency.dao.impl.MySQLForArticleDao"/>
        <!--
            ● id属性:代表对象的唯一标识。可以看做一个人的身份证号。
            ● class属性:用来指定要创建的java对象的类名,这个类名必须是全限定类名(带包名)。
        -->
    
        <bean id="OracleForArticleDao" class="com.gdb.spring6.dependency.dao.impl.OracleForArticleDao"/>
    
        <bean id="ArticleService" class="com.gdb.spring6.dependency.service.ArticleService">
            <property name="articleDao" ref="MySQLForArticleDao"/>
        </bean>
    </beans>
    
  • 测试程序

    package com.gdb.spring6.dependecy.test;
    
    import com.gdb.spring6.dependency.service.ArticleService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
        @org.junit.Test
        public void TestDao(){
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
            ArticleService articleService = applicationContext.getBean("ArticleService", ArticleService.class);
            articleService.insert();
        }
    }
    

2.注入内部bean和注入外部bean

  • 外部Bean的特点:bean定义到外面,在property标签中使用ref属性进行注入。通常这种方式是常用。

    • 在上面的第一个程序中使用的就是注入外部bean。
  • 内部Bean的方式:在bean标签中嵌套bean标签。

    <bean id="ArticleService" class="com.gdb.spring6.dependency.service.ArticleService">
        <property name="articleDao">
            <bean id="OracleForArticleDao" class="com.gdb.spring6.dependency.dao.impl.OracleForArticleDao"/>
        </property>
    </bean>
    

3.注入简单数据类型

  • Spring中的简单类型
  • BeanUtils类源码:
    public class BeanUtils{
        
        //.......
        
        /**
    	 * Check if the given type represents a "simple" property: a simple value
    	 * type or an array of simple value types.
    	 * <p>See {@link #isSimpleValueType(Class)} for the definition of <em>simple
    	 * value type</em>.
    	 * <p>Used to determine properties to check for a "simple" dependency-check.
    	 * @param type the type to check
    	 * @return whether the given type represents a "simple" property
    	 * @see org.springframework.beans.factory.support.RootBeanDefinition#DEPENDENCY_CHECK_SIMPLE
    	 * @see org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#checkDependencies
    	 * @see #isSimpleValueType(Class)
    	 */
    	public static boolean isSimpleProperty(Class<?> type) {
    		Assert.notNull(type, "'type' must not be null");
    		return isSimpleValueType(type) || (type.isArray() && isSimpleValueType(type.getComponentType()));
    	}
    
    	/**
    	 * Check if the given type represents a "simple" value type: a primitive or
    	 * primitive wrapper, an enum, a String or other CharSequence, a Number, a
    	 * Date, a Temporal, a URI, a URL, a Locale, or a Class.
    	 * <p>{@code Void} and {@code void} are not considered simple value types.
    	 * @param type the type to check
    	 * @return whether the given type represents a "simple" value type
    	 * @see #isSimpleProperty(Class)
    	 */
    	public static boolean isSimpleValueType(Class<?> type) {
    		return (Void.class != type && void.class != type &&
    				(ClassUtils.isPrimitiveOrWrapper(type) ||
    				Enum.class.isAssignableFrom(type) ||
    				CharSequence.class.isAssignableFrom(type) ||
    				Number.class.isAssignableFrom(type) ||
    				Date.class.isAssignableFrom(type) ||
    				Temporal.class.isAssignableFrom(type) ||
    				URI.class == type ||
    				URL.class == type ||
    				Locale.class == type ||
    				Class.class == type));
    	}
        //........
    }
    
  • 通过源码分析得知,简单类型包括:
    • 基本数据类型
    • 基本数据类型对应的包装类
    • String或其他的CharSequence子类
    • Number子类
    • Date子类(虽然是简单类型,但是一般都是当作引用类型进行注入)
    • Enum子类
    • URI
    • URL
    • Temporal子类
    • Locale
    • Class
    • 另外还包括以上简单值类型对应的数组类型。
    • 需要注意的是:
      • 如果把Date当做简单类型的话,日期字符串格式不能随便写。格式必须符合Date的toString()方法格式。显然这就比较鸡肋了。如果我们提供一个这样的日期字符串:2010-10-11,在这里是无法赋值给Date类型的属性的。
      • spring6之后,当注入的是URL,那么这个url字符串是会进行有效性检测的。如果是一个存在的url,那就没问题。如果不存在则报错。
  • 需要特别注意:如果给简单类型赋值,使用value属性或value标签。而不是ref。
<?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="dataSource" class="com.gdb.spring6.beans.MyDataSource">
    	<!--如果像这种int类型的属性,我们称为简单类型,这种简单类型在注入的时候要使用value属性,不能使用ref-->
        <!--<property name="age" value="20"/>-->
        <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/spring"/>
        <property name="username" value="root"/>
        <property name="password">
        	<value>123456</value>
        </property>	
    </bean>
</beans>

3.级联属性赋值

<?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="clazzBean" class="com.gdb.spring6.beans.Clazz"/>

    <bean id="student" class="com.gdb.spring6.beans.Student">
        <property name="name" value="张三"/>

        <!--要点1:以下两行配置的顺序不能颠倒-->
        <property name="clazz" ref="clazzBean"/>
        <!--要点2:clazz属性必须有getter方法-->
        <property name="clazz.name" value="高三一班"/>
    </bean>
</beans>

4.注入数组

  • 如果数组中是简单类型,使用value标签。
  • 如果数组中是非简单类型,使用ref标签。

(1)注入简单类型数组

<?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="person" class="com.dgb.spring6.beans.Person">
        <property name="favariteFoods">
            <array>
                <value>鸡排</value>
                <value>汉堡</value>
                <value>鹅肝</value>
            </array>
        </property>
    </bean>
</beans>

(2)注入引用数据类型数组

<?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="goods1" class="com.gdb.spring6.beans.Goods">
        <property name="name" value="西瓜"/>
    </bean>

    <bean id="goods2" class="com.gdb.spring6.beans.Goods">
        <property name="name" value="苹果"/>
    </bean>

    <bean id="order" class="com.gdb.spring6.beans.Order">
        <property name="goods">
            <array>
                <!--这里使用ref标签即可-->
                <ref bean="goods1"/>
                <ref bean="goods2"/>
            </array>
        </property>
    </bean>
</beans>

5.List和Set集合注入

(1)List

  • 注入List集合的时候使用list标签,如果List集合中是简单类型使用value标签,反之使用ref标签。
<?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="peopleBean" class="com.gdb.spring6.beans.People">
        <property name="names">
            <list>
                <value>铁锤</value>
                <value>张三</value>
                <value>张三</value>
                <value>张三</value>
                <value></value>
            </list>
        </property>
    </bean>
</beans>

(2)Set

  • 注入Set集合的时候使用标签,set集合中元素是简单类型的使用value标签,反之使用ref标签。
<?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="peopleBean" class="com.gdb.spring6.beans.People">
        <property name="phones">
            <set>
                <!--非简单类型可以使用ref,简单类型使用value-->
                <value>110</value>
                <value>110</value>
                <value>120</value>
                <value>120</value>
                <value>119</value>
                <value>119</value>
            </set>
        </property>
    </bean>
</beans>

6.Map和Properties注入

(1)Map

  • 使用标签
  • 如果key是简单类型,使用 key 属性,反之使用 key-ref 属性。
  • 如果value是简单类型,使用 value 属性,反之使用 value-ref 属性。
<?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="peopleBean" class="com.gdb.spring6.beans.People">
        <property name="addrs">
            <map>
                <!--如果key不是简单类型,使用 key-ref 属性-->
                <!--如果value不是简单类型,使用 value-ref 属性-->
                <entry key="1" value="北京大兴区"/>
                <entry key="2" value="上海浦东区"/>
                <entry key="3" value="深圳宝安区"/>
            </map>
        </property>
    </bean>
</beans>

(2)Properties

  • 使用标签嵌套标签完成。
    <?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="peopleBean" class="com.gdb.spring6.beans.People">
            <property name="properties">
                <props>
                    <prop key="driver">com.mysql.cj.jdbc.Driver</prop>
                    <prop key="url">jdbc:mysql://localhost:3306/spring</prop>
                    <prop key="username">root</prop>
                    <prop key="password">123456</prop>
                </props>
            </property>
        </bean>
    </beans>
    

7.注入null和空字符串

(1)空字符串

  • 注入空字符串使用: 或者 value=“”
<?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="vipBean" class="com.gdb.spring6.beans.Vip">
        <!--空串的第一种方式-->
        <!--<property name="email" value=""/>-->
        <!--空串的第二种方式-->
        <property name="email">
            <value/>
        </property>
    </bean>
</beans>

(2)null

  • 注入null使用: 或者 不为该属性赋值
<?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="vipBean" class="com.gdb.spring6.beans.Vip">
        <property name="email">
            <null/>
        </property>
    </bean>
</beans>

8.注入特殊符号

  • XML中有5个特殊字符,分别是:<、>、'、"、&。以上5个特殊符号在XML中会被特殊对待,会被当做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="mathBean" class="com.gdb.spring6.beans.Math">
            <property name="result" value="2 &lt; 3"/>
        </bean>
    </beans>
    
    • 第二种:将含有特殊符号的字符串放到:<![CDATA[]]> 当中。因为放在CDATA区中的数据不会被XML文件解析器解析。 注意:使用CDATA时,不能使用value属性,只能使用value标签。
    <?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="mathBean" class="com.powernode.spring6.beans.Math">
            <property name="result">
                <!--只能使用value标签-->
                <value><![CDATA[2 < 3]]></value>
            </property>
        </bean>
    
    </beans>
    

二、基于p命名空间的注入

  • 目的:简化set注入的配置。
  • 使用p命名空间注入的前提条件包括两个:
    • 第一:在XML头部信息中添加p命名空间的配置信息xmlns:p=“http://www.springframework.org/schema/p”
    • 第二:p命名空间注入是基于setter方法的,所以需要对应的属性提供setter方法。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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="customerBean" class="com.gdb.spring6.beans.Customer" p:name="zhangsan" p:age="20"/>
</beans>

三、构造注入

  • 核心原理:通过调用构造方法来给属性赋值。
<bean id="orderDaoBean" class="com.gdb.spring6.dao.OrderDao"/>
<bean id="orderServiceBean" class="com.gdb.spring6.service.OrderService">
  <!--index="0"表示构造方法的第一个参数,将orderDaoBean对象传递给构造方法的第一个参数。-->
  <constructor-arg index="0" ref="orderDaoBean"/>
</bean>
<bean id="orderDaoBean" class="com.gdb.spring6.dao.OrderDao"/>

<bean id="orderServiceBean" class="com.gdb.spring6.service.OrderService">
  <!--第一个参数下标是0-->
  <constructor-arg index="0" ref="orderDaoBean"/>
  <!--第二个参数下标是1-->
  <constructor-arg index="1" ref="userDaoBean"/>
</bean>

<bean id="userDaoBean" class="com.gdb.spring6.dao.UserDao"/>
<bean id="orderDaoBean" class="com.gdb.spring6.dao.OrderDao"/>

<bean id="orderServiceBean" class="com.gdb.spring6.service.OrderService">
  <!--这里使用了构造方法上参数的名字-->
  <constructor-arg name="orderDao" ref="orderDaoBean"/>
  <constructor-arg name="userDao" ref="userDaoBean"/>
</bean>

<bean id="userDaoBean" class="com.gdb.spring6.dao.UserDao"/>
  • 通过构造方法注入的时候:
    • 可以通过下标
    • 可以通过参数名
    • 也可以不指定下标和参数名,可以类型自动推断。
  • Spring在装配方面做的还是比较健壮的。

四、基于c命名空间的注入

  • c命名空间是简化构造方法注入的。
  • 使用c命名空间的两个前提条件:
    • 第一:需要在xml配置文件头部添加信息:xmlns:c=“http://www.springframework.org/schema/c”
    • 第二:需要提供构造方法。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:c="http://www.springframework.org/schema/c"
       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="myTimeBean" class="com.gdb.spring6.beans.MyTime" c:year="1970" c:month="1" c:day="1"/>-->

    <bean id="myTimeBean" class="com.gdb.spring6.beans.MyTime" c:_0="2008" c:_1="8" c:_2="8"/>
</beans>

五、util命名空间让配置复用

  • 使用util命名空间可以让配置复用。
  • 使用util命名空间的前提是:在spring配置文件头部添加配置信息。如下:
    在这里插入图片描述
<?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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <util:properties id="prop">
        <prop key="driver">com.mysql.cj.jdbc.Driver</prop>
        <prop key="url">jdbc:mysql://localhost:3306/spring</prop>
        <prop key="username">root</prop>
        <prop key="password">123456</prop>
    </util:properties>

    <bean id="dataSource1" class="com.gdb.spring6.beans.MyDataSource1">
        <property name="properties" ref="prop"/>
    </bean>

    <bean id="dataSource2" class="com.gdb.spring6.beans.MyDataSource2">
        <property name="properties" ref="prop"/>
    </bean>
</beans>

六、 基于XML的自动装配

  • Spring还可以完成自动化的注入,自动化注入又被称为自动装配。它可以根据名字进行自动装配,也可以根据类型进行自动装配。
  • 无论是byName还是byType,在装配的时候都是基于set方法的。所以set方法是必须要提供的。提供构造方法是不行的。

1.根据名称自动装配

<?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="userService" class="com.gdb.spring6.service.UserService" autowire="byName"/>
    <bean id="aaa" class="com.gdb.spring6.dao.UserDao"/>
</beans>
  • UserService Bean中需要添加autowire=“byName”,表示通过名称进行装配。
  • UserService类中有一个UserDao属性,而UserDao属性的名字是aaa,对应的set方法是setAaa(),正好和UserDao Bean的id是一样的。这就是根据名称自动装配。

2. 根据类型自动装配

<?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">

    <!--byType表示根据类型自动装配-->
    <bean id="accountService" class="com.gdb.spring6.service.AccountService" autowire="byType"/>
    <bean class="com.gdb.spring6.dao.AccountDao"/>
</beans>
  • 当byType进行自动装配的时候,配置文件中某种类型的Bean必须是唯一的,不能出现多个。

七、Spring引入外部属性配置文件

  • 我们都知道编写数据源的时候是需要连接数据库的信息的,例如:driver url username password等信息。这些信息可以单独写到一个属性配置文件中吗,这样用户修改起来会更加的方便。当然可以。
  • 在spring配置文件中引入context命名空间
<?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">

</beans>
  • 在spring中配置使用jdbc.properties文件。
<context:property-placeholder location="jdbc.properties"/>

<bean id="dataSource" class="com.powernode.spring6.beans.MyDataSource">
    <property name="driver" value="${jdbc.driver}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小宝945

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值