引言
Spring是一款优秀的开源框架,核心为控制反转/依赖注入(IOC)和面向切面的编程(AOP),前者实现依赖导致原则机制的容器,完成运行时的对象创建;后者则是不破坏封装性原则,灵活增强类的功能,也是单一职责原则所要求的。
Spring实现机制依赖配置文件,早期版本的Spring使用XML文件实现;当前主要依赖注解符实现
创建Spring工程
打开idea,选择菜单 Flie—>New—>Project,弹出对话框,如下图所示
其中quickStart是一个项目模板,弹出对话框,
直接按“Next”按钮,进入下一个界面
完成后,项目内容如下图所示
添加Spring依赖项
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
在main目录下创建目录resources目录
将resources文件夹设置为资源根目录,设置过程如下图所示
并添加名为“applicationContext.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>
Spring IOC的使用
创建包services,并定义一个接口及这两个接口的实现类,如图所示
具体代码如下
public interface Animal {
void move();
}
public class Dog implements Animal {
@Override
public void move() {
System.out.println("狗爬");
}
}
public class Duck implements Animal {
@Override
public void move() {
System.out.println("八字步");
}
}
修改配置文件applicationContext.xml,添加bean对应关系
<?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">
<!-- 对应的接口要调用的具体类 -->
<bean id="Animal" class="com.bjwl.services.impl.Duck"></bean>
</beans>
测试代码如下:
public class App
{
public static void main( String[] args )
{
//加载Spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//由容器中取出实例
Animal animal = (Animal)context.getBean("Animal");
//执行方法
animal.move();
}
}
调用代码不出现任何具体类,体现了依赖倒置原则,执行结果为:鸭子的八字步;
如果想看“狗爬”,只需要将配置文件中的Duck换成Dog,实现了调用方与具体之间行成松耦合
这里是使用XML完成Spring IOC功能,当然也可以使用注解完成IOC。
Spring AOP的使用
面向切面的编程(AOP)技术是不破坏原有封装的情况下,对原有的功能进行增强,也能保证原有封装的职责的单一性。如:网站系统中用户类的封装,我们仅封装用户的行为,如查询帖子、评论、删除帖子等,二将权限、记录日志等功能由Spring的AOP技术完成,简单方便。
编写目标对象User,代码如下
@Component
public class User {
public void query(){
System.out.println("查看帖子!");
}
public void comment(){
System.out.println("发表评论!");
}
public void delete(){
System.out.println("删除帖子!");
}
}
这个类职责单一,完成用户所要完成的内容
编写切面类PermissionCheck完成权限检查,Log类完成日志输出
public class PermissionCheck {
public void beforeCheck(){
System.out.println("权限检查");
}
}
public class Log {
public void writeLog(){
System.out.println("写入删除日志!");
}
}
修改配置文件applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--目标对象-->
<bean id="user" class="com.bjwl.aoptest.User"></bean>
<!--切面类-->
<bean id="log" class="com.bjwl.aoptest.Log"></bean>
<bean id="permission" class="com.bjwl.aoptest.PermissionCheck"></bean>
<!--切面配置-->
<aop:config>
<!--定义切面-->
<aop:aspect ref="permission">
<!--定义切点-->
<aop:pointcut id="action" expression="execution(* com.bjwl.aoptest.User.*(..))"/>
<aop:before method="beforeCheck" pointcut-ref="action"></aop:before>
</aop:aspect>
<!--定义切面-->
<aop:aspect ref="log">
<!--定义切点-->
<aop:pointcut id="action" expression="execution(* com.bjwl.aoptest.User.*(..))"/>
<aop:after method="writeLog" pointcut-ref="action"></aop:after>
</aop:aspect>
</aop:config>
</beans>
测试,在APP中写入代码,代码如下:
private static void aopTest(){
//加载Spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//由容器中取出实例
User user = (User)context.getBean("user");
//执行方法
user.query();
}
执行结果如下:
在调用方,没有写任何关于切面类的代码,但完成了功能的扩展。
总结与问题
本文中使用Spring框架早期版本使用的xml方式完成了IOC和AOP两大核心功能,目的为了说明Spring框架的实现机制,当前主流使用注解的方式完成这两大功能的实现,后续的章节再做介绍。Spring框架功能非常强大,注解种类很多,需要进一步的学习和了解。
注意:使用AOP时,idea导入的Spring缺少aspectjweaver.jar。下载导入这个jar包,或者maven工程下,修改pom依赖文件即可,如下图所示: