一篇文章带你掌握主流基础框架——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"><bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"><!--数组注入--><!--注意:name:对应实现类中的内部成员名称<>里的array等为固定词汇--><property name="array"><array><value>100</value><value>200</value><value>300</value></array></property><!--list集合注入--><property name="list"><list><value>itcast</value><value>itheima</value><value>boxuegu</value><value>chuanzhihui</value></list></property><!--set集合注入--><property name="set"><set><value>itcast</value><value>itheima</value><value>boxuegu</value><value>boxuegu</value></set></property><!--map集合注入--><property name="map"><map><entry key="country" value="https://www.huyubaike.com/biancheng/china"/><entry key="province" value="https://www.huyubaike.com/biancheng/henan"/><entry key="city" value="https://www.huyubaike.com/biancheng/kaifeng"/></map></property><!--Properties注入--><property name="properties"><props><prop key="country">china</prop><prop key="province">henan</prop><prop key="city">kaifeng</prop></props></property></bean></beans>案例:数据源对象管理针对一个新的数据源对象,我们采用两步来创建bean(我们以druid为案例):

  • 导入druid坐标
<?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.itheima</groupId><artifactId>spring_09_datasource</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.10.RELEASE</version></dependency><!--这里导入druid坐标--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.16</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency></dependencies></project>
  • 配置数据源对象作为Spring管理的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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--管理DruidDataSource对象--><!--起id 设置class地址--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><!--配置基本信息--><property name="driverClassName" value="https://www.huyubaike.com/biancheng/com.mysql.jdbc.Driver"/><property name="url" value="https://www.huyubaike.com/biancheng/jdbc:mysql://localhost:3306/spring_db"/><property name="username" value="https://www.huyubaike.com/biancheng/root"/><property name="password" value="https://www.huyubaike.com/biancheng/123456"/></bean></beans>案例:加载properties文件这个案例我们将会介绍如何加载properties文件 , 并将文件带入到property基本信息中
我们大致将步骤分为以下三步:
  • 开辟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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--上述beans中的内容是我们的命名空间开辟过程在原本的xml中只有:xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">在下面的内容中我们添加:xmlns:context="http://www.springframework.org/schema/context"并在xsi:schemaLocation中添加:http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd(整体从上述内容复制,然后修改末尾xsi即可)--></beans>

推荐阅读