为了账号安全,请及时绑定邮箱和手机立即绑定

Spring基础使用(一)--------IOC、Bean的XML方式装配

标签:
Java SpringBoot
基础

1、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">
</beans>

2、初始化容器方法:

  • 文件的绝对路径:
        FileSystemXmlApplicationContext context1=new FileSystemXmlApplicationContext("C:/configuration.xml")
  • Classpath加载
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("configuration.xml");
IOC

1、Bean注入的2种方式:

  • set方法注入:通过set方法进行注入。
    <bean id="helloSpring" class="service.HelloSpringImpl">
        <property name="daoImpl" ref="daoImpl"></property>
    </bean>
    <bean id="daoImpl" class="DAO.DAOImpl" ></bean>
  • 构造方法注入:通过构造方法进行注入
    <bean id="helloSpring" class="service.HelloSpringImpl">
        <constructor-arg name="daoImpl" ref="daoImpl"></constructor-arg>
    </bean>
    <bean id="daoImpl" class="DAO.DAOImpl" ></bean>
Bean的XML装配

1、Bean的作用域

  • singleton:单例模式
  • prototype:原型模式
  • request:当前request内有效
  • session:当前session内有效
  • global session:当前global session内有效

2、Bean初始化和销毁方法

初始化方法:

  • 实现InitializingBean接口,重写afterPropertiesSet()方法
  • 在xml文件中,bean定义的地方指定init-method

销毁方法:

  • 实现DisposableBean接口,重写destroy()方法
  • 在xml文件中,bean定义的地方指定destroy-method

全局初始化和销毁:

通过default-init-methoddefault-destroy-method关键字进行指定。

<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"
        default-init-method="functionInit" default-destroy-method="functionDestroy">

3、Aware

如果类实现了ApplicationContextAware接口,则需要重写接口中的setApplicationContext方法,在IOC容器进行初始化之后,会自动调用setApplicationContext方法。setApplicationContext方法的参数就是IOC容器对象本身。

如果类实现了BeanNameAware接口,则需要重写接口中的setBeanName方法,在Bean实例化的时候,会自动调用setBeanName方法。

还有其他的Aware接口:

  • BeanFactoryAware:获得当前bean Factory,从而调用容器的服务
  • MessageSourceAware:得到message source从而得到文本信息
  • ApplicationEventPublisherAware:应用时间发布器,用于发布事件
  • ResourceLoaderAware:获取资源加载器,可以获得外部资源文件

4、Bean的自动装配(4种类型)

在xml文件中,通过default-autowire字段进行指定装配模式。而不需要在bean中使用property或者constructor-arg

<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"
        default-autowire="byName">
  • No:不做动作
  • byName:根据类的成员的名称,从容器中进行匹配并装配。需要set函数支持。
  • byType:根据类的成员的类型,从容器中进行匹配并装配。需要set函数支持。如果存在多个同类型对象,则抛出异常。
  • Constructor:根据类的成员的类型,从容器中进行匹配并装配。需要构造函数支持。

5、Resources:实现对底层资源的访问

Spring内置6种Resource类型:UrlResource,ClassPathResource,FileSystemResource,ServletContextResource,InputStreamResource,ByteArrayResource

  • UrlResource:URL对应的资源
  • ClassPathResource:类路径下的资源
  • FileSystemResource:文件系统资源
  • ServletContextResource:ServletContext对应资源
  • InputStreamResource:输入流资源
  • ByteArrayResource:字节数组资源

8、ResourceLoader接口:资源加载器

ResourceLoader是一个用于资源加载的接口。Spring中的ApplicationContext实现了ResourceLoader接口,所以可以通过ApplicationContext进行实际资源的加载。

加载方式有4种:

  • classpath前缀:从classpath中获取对应的资源文件
  • file前缀:从文件系统中获取对应的资源文件,需要绝对路径
  • http前缀:从网络中获取对应的资源文件
  • 无前缀:从ApplicationContext所在目录中获取对应的资源文件
点击查看更多内容
9人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
1万
获赞与收藏
1426

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消