开课吧开课吧锤锤2021-02-23 10:39
如果让你说一个大公司,你的脑海里最先想到的是哪家公司呢?阿里、百度、还是美团。你想到的这些公司有一个特点,那就是都是大型互联网公司,这些公司的支柱就是程序员们的技术,那如何能在这样的互联网公司能够有一席立足之地呢?那就需要你高超的技术,那如何提升技术,不妨看看以下内容,来提升自己。
ApplicationContext两个主要实现类
ClassPathXmlApplicationContext
FileSystemXmlApplicationContext
Plane.java和bean-plane.xml请参考
http://blog.csdn.net/yangshangwei/article/details/74910935#t3
ClassPathXmlApplicationContext
package com.xgj.master.ioc.beanfactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ClassPathXmlApplicationTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"classpath:com/xgj/master/ioc/beanfactory/bean-plane.xml");
Plane plane = (Plane) applicationContext.getBean("plane");
plane.introduce();
}
}
如果配置文件放在在类路径下,则优先考虑使用ClassPathXmlApplication实现类
对于 ClassPathXmlApplicationContext 来讲:
new ClassPathXmlApplicationContext(
"classpath:com/xgj/master/ioc/beanfactory/bean-plane.xml");
等同于
new ClassPathXmlApplicationContext(
"com/xgj/master/ioc/beanfactory/bean-plane.xml");
FileSystemXmlApplicationContext
package com.xgj.master.ioc.beanfactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class FileSystemXmlApplicationTest {
public static void main(String[] args) {
ApplicationContext app = new FileSystemXmlApplicationContext(
"file:D:/workspace/workspace-jee/HelloSpring/Spring4-char2/src/com/xgj/master/ioc/beanfactory/bean-plane.xml");
Plane plane = (Plane) app.getBean("plane");
plane.introduce();
}
}
如果配置文件放在文件系统路径下,则优先考虑使用FileSystemXmlApplication实现类
对于FileSystemXmlApplication来讲,
"file:D:/workspace/workspace-jee/HelloSpring/Spring4-char2/src/com/xgj/master/ioc/beanfactory/bean-plane.xml"
等同于
"D:/workspace/workspace-jee/HelloSpring/Spring4-char2/src/com/xgj/master/ioc/beanfactory/bean-plane.xml"
运行结果:
加载一组配置文件
初始化配置文件时,还可以指定一组配置文件,Spring会自动对多个配置文件在内存中“整合”成一个配置文件。
举例:
我们新增一个Car类,和Plane中的属性方法一致,增加一个配置文件bean-car.xml,和bean-plane.xml中的结构一致。
ClassPathXmlApplicationContext加载一组资源文件
//两种写法
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {
"com/xgj/master/ioc/beanfactory/bean-plane.xml", "com/xgj/master/ioc/beanfactory/bean-car.xml" })
ApplicationContext ctx2 = new ClassPathXmlApplicationContext(
"com/xgj/master/ioc/beanfactory/bean-plane.xml", "com/xgj/master/ioc/beanfactory/bean-car.xml");
Plane plane2 = (Plane) ctx2.getBean("plane");
Car car = (Car) ctx2.getBean("car");
plane2.introduce();
car.introduce();
FileSystemXmlApplicationContext加载一组资源文件
// 加载多组资源文件
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"file:D:/workspace/workspace-jee/HelloSpring/Spring4-char2/src/com/xgj/master/ioc/beanfactory/bean-plane.xml",
"file:D:/workspace/workspace-jee/HelloSpring/Spring4-char2/src/com/xgj/master/ioc/beanfactory/bean-car.xml");
Plane plane2 = (Plane) ctx.getBean("plane");
Car car = (Car) ctx.getBean("car");
plane2.introduce();
car.introduce();
运行结果:
基于类注解的配置方式
Spring支持基于类注解的配置方式,主要来源于Spring的子项目JavaConfig。 目前JavaConfig已经成为Spring核心框架的一部分。
这里只是简单的演示用法,后续更新详情。
一个标注了@Configuration注解的POJO即可提供Spring所需要的配置信息。
代码如下:
Beans.java 配置信息提供类
package com.xgj.spring4.chapter4.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration // @Configuration表示是一个配置信息提供类
public class Beans {
@Bean(name = "plane") // ͨ定义一个Bean,通过name指定bean的名称
public Plane createPlane() {
Plane plane = new Plane();
plane.setBrand("F35");
plane.setColor("GREY");
plane.setMaxSpeed(800);
return plane;
}
}
Plane.java
package com.xgj.spring4.chapter4.configuration;
public class Plane {
private String brand;
private String color;
private int maxSpeed;
...
省略setter/getter方法
public void introduce(){
System.out.println("Plane brand:" + brand + " ,Color:" + color + ",maxSpeed:" + maxSpeed);
}
}
AnnotationConfigApplicationTest.java 测试:
package com.xgj.spring4.chapter4.configuration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class AnnotationConfigApplicationTest {
public static void main(String[] args) {
// ͨ通过一个带有@Configuration的POJO装载Bean的配置
ApplicationContext ctx = new AnnotationConfigApplicationContext(Beans.class);
Plane plane = ctx.getBean("plane", Plane.class);
plane.introduce();
}
}
运行结果:
解析:
Spring为基于注解类的配置提供了专门的ApplicationContext实现类:AnnotationConfigApplication。
AnnnotationConfigApplication将加载Beans.class中的Bean定义并调用Beans.class中的方法实例化Bean,启动并装载Bean。
Spring4支持Groovy DSL配置Bean
Spring4.0支持使用Groovy DSL来进行Bean的定义, 与XML文件的配置类似,只不过基于Groovy脚本语言,可以实现更加负载灵活的Bean的配置逻辑。
步骤:
添加依赖包
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.3.6</version>
</dependency>
编写代码
groovy-bean.groovy 配置信息
package com.xgj.spring4.chapter4.groovy;
beans{
plane(Plane){
brand = "A380";
color = "red";
maxSpeed = 700;
}
}
Plane同上。
测试类:
package com.xgj.spring4.chapter4.groovy;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericGroovyApplicationContext;
public class GroovyApplicationContextTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx = new GenericGroovyApplicationContext("classpath:com/xgj/spring4/chapter4/groovy/groovy-bean.groovy");
Plane plane = (Plane)ctx.getBean("plane");
plane.introduce();
}
}
基于Groovy的配置方式很容易让开发者配置负载Bean的实例化过程,比基于XML文件、注解的配置方式更加灵活。
Spring为依据Groovy的配置提供了专门的Application实现类:GenericiGroovyApplicationContext.
以上内容由开课吧老师小小工匠提供,更多Java教程尽在开课吧广场Java教程频道。更多免费课程可以关注公众号“码农集散地”

最新文章

如何在大规模 Kubernetes 集群上实现高 SLO?
Pod创建成功率:这是一个非常重要的指标,蚂蚁集团一周的Pod创建量在百万级别,如果成功率波动会造成大量Pod失败,同时Pod成功率下跌也是集群异常的最直观反映;
2021-03-05 16:20:04

MySQL不建议delete删除数据的原因是什么?(二)
MySQL内部不会真正删除空间,而且做标记删除,即将delflag:N修改为delflag:Y,commit之后会会被purge进入删除链表,如果下一次insert更大的记录,delete之后的空间不会被重用,如果插入的记录小于等于delete的记录空会被重用,这块内容可以通过知数堂的innbloc
2021-03-05 11:45:44

MySQL不建议delete删除数据的原因是什么?(一)
物理上主要由系统用户数据文件,日志文件组成,数据文件主要存储MySQL字典数据和用户数据,日志文件记录的是datapage的变更记录,用于MySQLCrash时的恢复。
2021-03-05 11:36:22

Java教程:MySQL如何设计索引更高效?(五)
同时也介绍了如何更好做MySQL索引设计,包括前缀索引,复合索引的顺序问题以及MySQL8.0推出的索引跳跃扫描,我们都知道,索引可以加快数据的检索,减少IO开销,会占用磁盘空间,是一种用空间换时间的优化手段,同时更新操作会导致索引频繁的合并分裂,影响索引性能,在实际的业务开发中,如何根据业务场景去
2021-03-05 11:29:12

Java教程:MySQL如何设计索引更高效?(四)
在单列索引不能很好的过滤数据的时候,可以结合where条件中其他字段来创建复合索引,更好的去过滤数据,减少IO的扫描次数,举个例子:业务需要按照时间段来查询交易记录,有如下的SQL:
2021-03-05 11:25:02