标签:oca action cloud 基本概念 ram hand 实例 spring 动态代理
一.spring基本概念
spring是容器框架,可以创建bean,维护bean之间的关系。它也可以管理web层,持久层,业务层,dao层,spring可以配置各个层的组件,维护各个层的关系。
二.spring核心原理
1.IOC控制反转
概念:控制权由对象本身转向容器,由容器根据配置文件创建对象实例并实现各个对象的依赖关系。
核心:bean工厂
2.AOP面向切面编程
a、静态代理
根据每个具体类分别编写代理类
根据一个借口编写一个代理类
b、动态代理
正对一个方面编写一个invocationHandler,然后借用JDK反射包中的Proxy类为各种借口动态生成各种代理类
三、简单的spring入门案例
1.编写一个UserService类
package com.cloud.service;
public class UserService {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void sayHello(){
System.out.println("hello:"+name);
}
}
2.编写核心配置文件applicationContex.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
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-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 在容器中配置bean对象 -->
<!-- 下面这句等价于:UserService userService = new UserService() -->
<bean id="userService" class="com.cloud.service.UserService">
<!-- 等价于:userService.setName("SpringName"); -->
<property name="name">
<value>SpringName</value>
</property>
</bean>
</beans>
3.编写测试类
package com.cloud.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.cloud.service.UserService;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) ac.getBean("userService");
userService.sayHello();
}
}
四、spring原理总结
1.使用spring,没有new对象,我们把创建对象的任务交给spring框架
2、spring实际上是一个容器框架,可以配置各种bena(action/service/domain/dao),并且可以维护bean与bean之间的关系,当我们需要使用某个bean的时候,我们可以getBean(id)获取。
标签:oca action cloud 基本概念 ram hand 实例 spring 动态代理
原文地址:http://www.cnblogs.com/cn-930621/p/7649828.html