Spring简单认知

功能: 使用 Spring 框架来创建性能好、易于测试、可重用的代码。便于代码开发

优点: 模块化设计 使用时间只用引入相应模块即可

简单的spring程序:
class类文件 + main类 + xml配置文件

描述: main类中通过加载配置文件将class实体化,配置文件中可以将具体属性值加入

spring 核心:

  • IOC容器
    用于创建对象,负责类的实例化,定位,配置,以及对象之间的依赖关系
  1. BeanFactory容器:
    功能: 为依赖注入提供支持
    实现:实现BeanFactory接口的相应功能。
    常用类: Spring 中,有大量对 BeanFactory 接口的实现。其中,最常被使用的是 XmlBeanFactory 类。这个容器从一个 XML 文件中读取配置元数据,由这些元数据来生成一个被配置化的系统或者应用。
  2. ApplicationContext 是XMLBEanFactory的子接口,功能比后者更加完全强大,接口实现的类主要如下:
    1 FileSystemXmlApplicationContext :容器从xml文件中加载已被定义的Bean,需要给出具体路径
    2 ClassPathXmlApplicationContext: 不用给出路径,秩序配置好CLASSPATH,自动搜索Bean的配置文件
    3 WebXmlApplicationContext: 在web城西范围内加载xml文件中已被定义的bean
    简单使用代码如下:
main类:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
       public static void main(String[] args) {
		 ApplicationContext  context = new  ClassPathXmlApplicationContext("Beans.xml");
		 Sell sell = (Sell) context.getBean("sell");
		 System.out.println(sell.getMassage());
	}
}
类
public class Sell {
     private String massage;

    public String getMassage() {
	   return massage;
    }

   public void setMassage(String massage) {
	   this.massage = massage;
   }
}

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-3.0.xsd">
    
    <bean id="sell" class="com.demon.dy.Sell">
        <property name="massage" value="Hello World!"/>
    </bean>

</beans>

使用Spring框架之前只需导入相应的spring包以及依赖包logging,即可直接使用其中的接口以及类创建相应的Bean,并且进行操作。

  • Bean
    概述:bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象

    被IOC容器所管理

    生命周期:
    定义----- 初始化 ----- 使用 ------ 销毁

    注意:bean在xml文件中配置,适用于实例化类,用来替代原先的new的实例化方法。因此配置文件中的属性一般都需和类中的具体属性对应。

简单代码演示:

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

public class Main {
       public static void main(String[] args) {
		 AbstractApplicationContext  context = new  ClassPathXmlApplicationContext("Beans.xml");
		 Sell sell = (Sell) context.getBean("sell");
		 System.out.println(sell.getMassage());
		 context.registerShutdownHook();
	}
}
public class Sell {
     private String massage;

     public void Init() {
    	 System.out.println("正在初始化");
     }
    public String getMassage() {
	   return massage;
    }

   public void setMassage(String massage) {
	   this.massage = massage;
   }
   
   public void destory() {
	   System.out.println("正在銷毀");
   }
}
<?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-3.0.xsd">
    
    <bean id="sell" class="com.demon.dy.Sell"
        init-method="Init"  destroy-method="destory">
        <property name="massage" value="Hello World!"/>
    </bean>

</beans>

结果如下:
在这里插入图片描述
Bean后置处理器:
功能:用于在初始化方法前后对Bean进行处理。
具体实现:实现接口BeanPostProcessor中的两个方法,之后在xml下配置即可。

修改代码如下:

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.validation.beanvalidation.BeanValidationPostProcessor;

public class Profile implements BeanPostProcessor{

	@Override
	public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
		// TODO Auto-generated method stub
		System.out.println("初始化之前處理");
		return arg0;
	}

	@Override
	public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
		// TODO Auto-generated method stub
		System.out.println("初始化之後處理");
		return arg0;
	}

}

<?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-3.0.xsd">
    
    <bean id="sell" class="com.demon.dy.Sell"
        init-method="Init"  destroy-method="destory">
        <property name="massage" value="Hello World!"/>
    </bean>
    <bean class="com.demon.dy.Profile"/>
</beans>

输出结果如图:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值