今天在自己开发的工具类中使用了spring注入的方式调用了其他类,但是发生的报错,在整理了后今天小结一下. 首先简单介绍下spring容器,spring容器是整个spring框架的核心,通常我们说的spring容器就是bean工厂,bean工厂负责创建和初始化bean.装配bean并且管理应用程序中的bean.spring中提供了两个核心接口:BeanFactory和ApplicationContext,ApplicationContext是BeanFactory子接口,它提供了比BeanFac…
//package 字符缓冲流bufferreaderDemo; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.BufferedInputStream; import java.io.IOException; /* * 四种方式实现大文件数据的读取写入--->复制 * 1.基本字节流一次读取一个字节 最慢 *…
方法一:在初始化时保存ApplicationContext对象方法二:通过Spring提供的工具类获取ApplicationContext对象方法三:继承自抽象类ApplicationObjectSupport方法四:继承自抽象类WebApplicationObjectSupport方法五:实现接口ApplicationContextAware 常用的5种获取spring 中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象 ApplicationContext…
很多时候,我们在普通的java类中需要获取spring的bean来做操作,比如,在线程中,我们需要操作数据库,直接通过spring的bean中构建的service就可以完成.无需自己写链接..有时候有些好的东西,拿到用就好了. 这里是多种方式中的一种. 通过实现ApplicationContextAware获取bean.这里有个问题,就是,如果spring容器没有启动完成的时候,不能通过这个方法获取,因为这样,会报空指针,因为 private static ApplicationContext…
第一种: Date day=new Date(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(df.format(day)); // 通过Date类来获取当前时间 第二种: SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.prin…
一.src路径下打包完在war包的classes层级下 1.Spring容器创建的三种方式 创建Bean容器之后创建对象: 其中第三种使用的是BeanFactory对象 2.spring通过配置文件用容器创建对象的原理…
配置文件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" xmlns:context="http://www.springf…
IDE 运行 运行带有main方法类 用mvn运行Spring-boot项目 在父项目中运行 mvn clean mvn install 在主项目中运行 mvn spring-boot:run 用JAR包运行项目 在pom中添加 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <versio…
关于java中遍历map具体哪四种方式,请看下文详解吧. 方式一 :这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用. Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> entry : map.entrySet()) { System.out.println("Key = " + entry.…
关于java中遍历map具体哪四种方式,请看下文详解吧. 方式一 这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用. Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> entry : map.entrySet()) { System.out.println("Key = " + entry.g…