Spring -- ApplicationContext使用及Bean的配置

Spring 提供了两种类型的 IOC 容器实现.

  • BeanFactory: IOC 容器的基本实现.
  • ApplicationContext: 提供了更多的高级特性. 是 BeanFactory 的子接口.
  • BeanFactory是Spring框架的基础设施,面向Spring本身;ApplicationContext 面向使用 Spring 框架的开发者,几乎所有的应用场合都直接使用 ApplicationContext 而非底层的 BeanFactory
  • 无论使用何种方式, 配置文件时相同的

对上一篇博客的main方法修改如下:

ApplicationContext简单用法

package com.gp.spring01;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

public class Main {

    public static void main(String[] args) {
        // Resource r = new FileSystemResource("helloMessage.xml");
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "helloMessage.xml");
        Person person = (Person) ctx.getBean("person");
        person.say();
    }
}

使用ApplicationContext实现IOC容器。
注意文件结构,此时我们需要将配置文件放到src目录下,如图
这里写图片描述


Spring支持3种依赖注入的方式

  • 属性注入
  • 构造器注入
  • 工程方法注入(比较少用)

下面是前两种注入的IOC容器配置方式

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
    <!-- 属性注入 -->
    <bean id="helloWord" class="com.gp.spring.HelloWorld">
        <property name="userName" value="Spring"></property>    
    </bean>

    <!-- 构造器注入 -->
    <bean id="car" class="com.gp.spring.Car">
        <constructor-arg value="dazong"></constructor-arg>
        <constructor-arg value="blue"></constructor-arg>
    </bean>
    <bean id="car1" class="com.gp.spring.Car">
        <constructor-arg value="dazong"></constructor-arg>
        <constructor-arg value="100000" type="double"></constructor-arg>
    </bean>

</beans>

测试方法

package com.gp.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "helloMessage.xml");
        HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWord");
        System.out.println(helloWorld);
        helloWorld.hello();

        Car car = (Car) ctx.getBean("car");
        System.out.println(car);
        Car car1 = (Car) ctx.getBean("car1");
        System.out.println(car1);
    }

}

输出结果

com.gp.spring.HelloWorld@5ce611
Hello Spring
Car [brand=dazong, color=blue, prince=0.0]
Car [brand=dazong, color=null, prince=100000.0]


属性注入NULL

属性注入的时候,如果我们想给一个值赋为NULL,用法如下

        <constructor-arg><null/></constructor-arg>

属性注入之对象

person类中注入car的信息,如下,

package com.gp.spring;

public class Person {
    private String name;

    private Car car;

    public Person() {
        super();
    }

    public Person(String name, Car car) {
        super();
        this.name = name;
        this.car = car;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", car=" + car + "]";
    }

}
    <bean id="person" class="com.gp.spring.Person">
        <constructor-arg value="gaopeng"></constructor-arg>
        <constructor-arg ref="car1"></constructor-arg>
    </bean>
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "helloMessage.xml");
        Person person = (Person) ctx.getBean("person");
        System.out.println(person);

    }

输出结果

Person [name=gaopeng, car=Car [brand=dazong, color=null, prince=100000.0]]


使用内部bean

内部Bean好处就是私有,不被外界所调用。
并且这里我们还使用car.color方式对属性进行赋值,代码如下

    <bean id="person" class="com.gp.spring.Person">
        <property name="name" value="gaopeng"></property>
        <property name="car">
            <bean id="car3" class="com.gp.spring.Car">
                <constructor-arg value="dazong"></constructor-arg>
                <constructor-arg value="100000" type="double"></constructor-arg>
            </bean>
        </property>
        <property name="car.color" value="red"></property>
    </bean>

数组的注入

我们如何注入到Bean对象中一个List数组呢,比如一个人拥有一辆奥迪和一辆大众汽车,下面我们来演示如何注入List

    <bean id="list_person_car1" class="com.gp.spring.Car">
        <constructor-arg value="dazong"></constructor-arg>
        <constructor-arg value="100000" type="double"></constructor-arg>
    </bean>
    <bean id="list_person_car2" class="com.gp.spring.Car">
        <constructor-arg value="Aodi"></constructor-arg>
        <constructor-arg value="400000" type="double"></constructor-arg>
    </bean>
    <bean id="person2" class="com.gp.spring.Person">
        <property name="name" value="wangjianlin"></property>
        <property name="cars">
            <list>
                <ref bean="list_person_car1"></ref>
                <ref bean="list_person_car2"></ref>
            </list>
        </property>
    </bean>

代码中list标签引用,将对象注入到List中。

实体Bean属性如下

public class Person {
    private String name;

    private Car car;

    private List<Car> cars;

输出结果

Person [name=wangjianlin, car=null, cars=[Car [brand=dazong, color=null, prince=100000.0], Car [brand=Aodi, color=null, prince=400000.0]]]


Map的注入

注入方式如下

    <bean id="person3" class="com.gp.spring.Person">
        <property name="name" value="wangjianlin"></property>
        <property name="carMap">
            <map>
                <entry key="1car">
                    <ref bean="list_person_car1" />
                </entry>
                <entry key="2car">
                    <ref bean="list_person_car2"></ref>
                </entry>
            </map>
        </property>
    </bean>

测试结果

Person [name=wangjianlin, carMap={1car=Car [brand=dazong, color=null, prince=100000.0], 2car=Car [brand=Aodi, color=null, prince=400000.0]}]


使用Properties

Properties类似Map,在Spring与Hibernate进行集成的时候会用到此类。

下面我们来看看是如何进行Properties的注入的吧。
首先我们需要一个Bean,如下

package com.gp.spring;

import java.util.Properties;

public class DataSource {
    private Properties properties;

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    @Override
    public String toString() {
        return "DataSource [properties=" + properties + "]";
    }

}

其中包含一个Properties的属性

再来看配置文件

    <bean id="dataSource" class="com.gp.spring.DataSource">
        <property name="properties">
            <props>
                <prop key="username">mysql</prop>
                <prop key="password">pwd</prop>
                <prop key="jbbcUrl">jdbc:mysql:3306/db</prop>
                <prop key="driverClss">com.mysql.jdbc.Driver</prop>
            </props>
        </property>
    </bean>

测试输出结果

{driverClss=com.mysql.jdbc.Driver, password=pwd, jbbcUrl=jdbc:mysql:3306/db, username=mysql}


util:list用法

通过上面引用集合注入的例子,我们发现集合都是配置在bean中,那么如果我们想要配置一个公用的集合,供多个Bean使用,那么就用到util:*这个标签,下面我们来看一个util:list的用法

在配置IOC容器XML之前,需要注意,我们需要引入Bean所需要的一些信息,如下

<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"
    xmlns:p="http://www.springframework.org/schema/p"
    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-4.0.xsd">

这里定义了一个utilList的共享List,Bean中直接可以通过ref指向它的id

    <util:list id="utilList">
        <ref bean="list_person_car1"></ref>
        <ref bean="list_person_car2"></ref>
        <bean class="com.gp.spring.Car">
            <property name="brand" value="BinLi"></property>
            <property name="color" value="red"></property>
            <property name="prince" value="1000000"></property>
        </bean>
    </util:list>

    <bean id="person4" class="com.gp.spring.Person">
        <property name="name" value="xiaoli"></property>
        <property name="cars" ref="utilList"></property>
    </bean>

输出结果

Person [name=xiaoli, cars=[Car [brand=dazong, color=null, prince=100000.0], Car [brand=Aodi, color=null, prince=400000.0], Car [brand=BinLi, color=red, prince=1000000.0]]]

这里需要注意的是IOC容器配置文件与之前的几个例子引入XML规范有不一样的地方,这里我们用到的是XML schema,之前用到的都是XML dtd,开发时我们可能会遇到这样的异常

Caused by: org.xml.sax.SAXParseException; lineNumber: 13; columnNumber: 68; 必须为元素类型 "beans" 声明属性 "xmlns"。
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)

原因就是由于文件中的DOCTYPE没有去掉。
这里写图片描述


使用P命名空间对属性赋值

首先来看下我们是如何配置的。

<bean id="person_P" class="com.gp.spring.Person" p:name="zhangzhang" p:cars-ref="utilList"></bean>

Person类中有2个属性一个是name(字符串类型),cars(List类型),其中p:cars-ref,直接执行一个已有的bean id为utilList,代码详见util:list用法

测试结果

Person [name=zhangzhang, cars=[Car [brand=dazong, color=null, prince=100000.0], Car [brand=Aodi, color=null, prince=400000.0], Car [brand=BinLi, color=red, prince=1000000.0]]]

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring和MyBatis是两个非常流行的Java开发框架,它们可以很好地结合使用。 首先,你需要在你的项目中引入Spring和MyBatis的相关依赖。 对于Spring,你可以使用Maven或者Gradle来管理依赖。在你的项目的pom.xml(或者build.gradle)文件中,添加以下依赖: ```xml <!-- Spring核心依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.9</version> </dependency> <!-- Spring和MyBatis的集成依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.7</version> </dependency> ``` 接下来,你需要配置Spring和MyBatis的相关配置文件。 首先是Spring配置文件(比如applicationContext.xml),你可以在其中配置Spring的上下文和其他相关的Bean。 ```xml <!-- 配置Spring的上下文 --> <context:annotation-config/> <context:component-scan base-package="com.example"/> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mydatabase"/> <property name="username" value="root"/> <property name="password" value="password"/> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置MyBatis的SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> <!-- 扫描MyBatis的Mapper接口 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.mapper"/> </bean> ``` 在上面的配置中,你需要根据你的数据库设置正确的数据源和连接信息。 接下来是MyBatis的配置文件(比如mybatis-config.xml),你可以在其中配置MyBatis的相关设置。 ```xml <configuration> <settings> <!-- 开启驼峰命名转换 --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> </configuration> ``` 在上面的配置中,我们开启了MyBatis的驼峰命名转换,这样可以方便地将数据库中的下划线命名转换为Java中的驼峰命名。 最后,你需要创建Mapper接口和对应的Mapper XML文件来定义SQL语句和映射关系。 这样,你就完成了Spring和MyBatis的配置。在你的代码中,你可以使用Spring的依赖注入来获取MyBatis的Mapper接口,并使用它来进行数据库操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值