Spring 概念
1、Spring 是一个开源的轻量级框架.
2、Spring 的核心主要有两部分组成:
(1)aop: 面向切面编程,使用代理设计模式完成功能的扩展
(2)ioc: 控制反转: 就是把创建对象的实现过程,由Spring来管理, DI:依赖注入: 对象有了,就需要赋值,给对象属性赋值,就叫依赖注入。所有依赖注入是在控制反转的基础上进行的。
3.Spring是一个一站式的框架:
JavaEE 针对 MVC 模式,每一成提提供了解决方案:
Web层: SpringMVC
Service层: Spring IOC
Dao 层:jdbcTemplate.(很少使用。)
什么是IOC:
控制反转如如何实现的:
Spring 服务器启动的时候,通过反射读取你代码里,的注解类,然后但你需要使用时,在通过反射来创建实例赋给你。
什么是AOP
切面编程,这是一种思想,实现原理参考 动态代理设计模式。
入门demo
demo 源码:http://download.youkuaiyun.com/detail/zhanglinlang/9763900
为了更好的显示他的源码,我用了xml 配置方式:
首先创建User.java
package com.zll.bean;
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
然后创建 UserDao.java
package com.zll.bean;
public class UserDao {
private User user;
//spring ioc 控制反转使用了反射技术,所以必须要有set方法。
public void setUser(User user) {
this.user = user;
}
public void add(){
System.out.println(user.getName()+" add success~~!");
}
}
然后创建 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.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 控制反转 将User的创建交给 spring-->
<bean id="userBean" class="com.zll.bean.User" >
<!-- 依赖注入name使用的是反射,所以必须有set方法 如果是简单类型 使用 value 如果是类 就使用 ref-->
<property name="name" value="zll"></property>
</bean>
<bean id="userDao" class="com.zll.bean.UserDao">
<!-- 这里应为应用了user对象 所以使用 ref 属性 -->
<property name="user" ref="userBean"></property>
</bean>
</beans>
最后使用普通的 main方法就可以进行测试:
package com.zll.bean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestMain {
public static void main(String[] args) {
//在实际开发中,读取spring配置文件的操作是配置在web.xml中的listener监听 serlvetContext启动的时候自动加载的,这里测试必须手动自己去取。
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
UserDao bean = (UserDao) context.getBean("userDao");
bean.add();
}
}
AOP 切面编程:
以上面的demo为例:在不修改原有代码的基础上,完成UserDao add 类的增强:
新增一个 UserLog 日志类
package com.zll.bean;
import org.aspectj.lang.ProceedingJoinPoint;
public class UserLog {
public void stratLog(){
System.out.println("开始日志~~~~~~~~~~~~~!");
}
public void endLog(){
System.out.println("结束日志~~~~~~~~~~~~~~!");
}
public void recordTime(ProceedingJoinPoint joinPoint) throws Throwable{
long time = System.currentTimeMillis();
System.out.println("开始时间:"+time);
//会调用被增强类的方法。
joinPoint.proceed();
System.out.println("结束时间"+(System.currentTimeMillis()-time));
}
}
在配置文件中添加一段代码
<?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.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 控制反转 将User的创建交给 spring -->
<bean id="userBean" class="com.zll.bean.User">
<!-- 依赖注入 name 使用的是反射,所以必须和get和set方法的那么相同 -->
<property name="name" value="zll"></property>
</bean>
<bean id="userDao" class="com.zll.bean.UserDao">
<property name="user" ref="userBean"></property>
</bean>
<!-- AOP 编程新增的代码 -->
<bean id="userLog" class="com.zll.bean.UserLog" />
<aop:config>
<!-- 配置 需要被 增强的 对象 或者方法 并起名 为 testLog
切入表达式的几种写法:
(1)execute(* com.zll.test.dao.add(..))
(2)execute(* com.zll.test.dao.*(..))
(3)execute(* *.*(..)) -->
<aop:pointcut expression="execution(* com.zll.bean.UserDao.add(..))"
id="testLog" />
<!-- 配置增强的方法 -->
<aop:aspect ref="userLog">
<aop:before method="stratLog" pointcut-ref="testLog" />
</aop:aspect>
<aop:aspect ref="userLog">
<aop:after method="endLog" pointcut-ref="testLog" />
</aop:aspect>
<aop:aspect ref="userLog">
<aop:around method="recordTime" pointcut-ref="testLog" />
</aop:aspect>
</aop:config>
</beans>