大名鼎鼎的Spring框架,统领java后端企业级开发的框架spring他lei了,希望我别被复杂的xml配bean劝退,一定要撑住!
Spring
起步
由于我的教程是全程使用eclipse教学,而我是完全使用IDEA的,就导致了教程的起步教学毫无参考价值
再而且好像教程的进度讲到spring是没有学javaweb的默认是用普通的java项目起步学习spring
然后就是全程自己摸索使用IDEA起步spring项目了
使用maven管理spring项目
教程中全程是自己导入spring的10个jar包,特别麻烦,这里我选择了使用maven来管理项目减少导入的工作量
本地maven和IDEA自带的maven:
由于IDEA自带maven所以可以不去用去单独的下载maven,只需要设置Idea自带的mavean就可以了
idea自带的maven的位置:%IDEAHOME%plugins\maven\lib\maven3
我选择使用maven3版本
首先进入conf文件夹编辑setting.xml文件,在mirrors标签下插入
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
这段配置阿里云的仓库,极大的加快了配置依赖下载的速度
然后是用IDEA创建maven项目:
疯子加天才:https://www.cnblogs.com/telwanggs/
引用了大佬的IDEA起步maven教程
Spring ioc
第一个ioc程序
之前创建好了maven项目,在maven中添加spring的依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId> org.aspectj</groupId >
<artifactId> aspectjweaver</artifactId >
<version> 1.6.11</version >
</dependency>
最后一个顺便添加了单元测试junit的依赖
applicationContext.xml文件
spring在IDEA的配置文件是默认在WEB-INF文件夹下的,这就导置了junit找不到配置文件,在src下的resources文件夹创建配置文件解决问题。
1. 配置applicationContext.xml
添加dtd
<?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">
</beans>
配置bean
<bean id="someServiceImpl" class="top.byfree.service.impl.SomeServiceImpl"></bean>
这样就是把,SomeServiceImpl类加载到ico容器内
2. 调用装配的类
@Test
public void SomeTest01 () {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
SomeService service = ac.getBean("someServiceImpl", SomeService.class);
service.doSome();
}
使用了junit
装配类的3种方法(配置文件)
默认装配方式
即上面演示的第一个ioc程序的配置方式
bean标签的属性
属性 | 作用 |
---|---|
id | 用于唯一标识bean标签 |
class | 装配的类的全限定路径 |
动态工厂
创建一个工厂类内部创建方法用于返回要装配的类的对象
public class ServiceFactory {
public SomeService getSomeService () {
return new SomeServiceImpl();
}
}
在配置文件中配置工厂
<bean id="serviceFactory" class="top.byfree.factory.ServiceFactory"></bean>
<bean id="someServiceImpl" factory-bean="serviceFactory" factory-method="getSomeService"></bean>
第一个bean标签是注册工厂
第二个标签是通过工厂注册bean对象
属性 | 作用 |
---|---|
factory-bean | 使用工厂的全限定参数 |
factory-method | 通过工厂下的那个方法注册bean对象,方法名 |
静态装配
工厂类内的方法声明为静态方法
public class ServiceFactory {
public static SomeService getSomeService () {
return new SomeServiceImpl();
}
}
配置文件中配置工厂
<bean id="someServiceImpl" class="top.byfree.factory.ServiceFactory" factory-method="getSomeService"></bean>
不需要再通过工厂新建标签装配了,只需要在工厂注册的标签内加入factory-method标签写入方法名即可
使用装配的类的两种方案
使用ApplicationContext获取
之前第一个ioc程序中使用的就是这个方法
使用ClassPathXmlApplicationContext创建一个ApplicationContext,构造器中传入applicationContext.xml的地址
得到的ApplicationContext对象下有getBean方法构造器传入两个参数第一个是bean中唯一标识的id第二个参数是装配的类的Class对象
BeanFactory获取
@Test
public void SomeTest02 () {
BeanFactory bf = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
SomeService service = bf.getBean("someServiceImpl", SomeService.class);
service.doSome();
}
通过XmlBeanFactory对象获取的BeanFactory对象也可以调用getBean方法
两者的差异:
ApplicationContext:的方式在容器对象创建开始的时候就把所有的bean类new出来
BeanFactory:的方式只有在调用装配的类的方法时候会new类
bean的作用域
通过一个容器获取的同id的bean对象默认是一个对象,但是如果id不同就算是装配的同一个类也不是同一个对象
因为bean的作用域是singleton(单例),而我们可以设置scope为prototype让作用域变为原型模式,这样就算id一致新获取的对象也是不同的
scope属性的值
属性 | 作用 |
---|---|
singleton | 单例模式创建的同id对象不是一个(只有一个new) |
prototype | 工厂模式创建的同id对象不是同一个(一次创建一个new) |