import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 搭建Spring开发环境方法
* 1.加入jar包
* spring.jar 是 spring 包的整合,我们一般没必要全用,所以用其子包
* 最低条件的jar子包有:spring-core.jar/spring-bean.jar/spring-context.jar
*
* 2.在src下添加 spring 管理 bean 的配置文件 applicationContext.xml
* 添加bean的管理,一般依赖注入(DI)的方式有两种:设置注入和构造注入,具体看下面的xml文件
*
* 3.通过spring容器获取bean有3种方式:
* o>Resource resource = new FileSystemResource("src/applicationContext.xml");
* >BeanFactory factory = new XmlBeanFactory(resource);
*
* o>ClassPathResource resource = new ClassPathResource("applicationContext.xml");
* >BeanFactory factory = new XmlBeanFactory(resource);
*
* o>ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
* >BeanFactory factory = (BeanFactory)context;
* 一般情我们采用的是第三种,因为bean.xml文件一般有很多个,所以我们通过数组全部加入
* @author xcz
*/
public class BeanFactoryUtil {
private static ApplicationContext getApplicationContext(String[] args){
return new ClassPathXmlApplicationContext(args);
}
/**
* 功能:获取BeanFactory
* @param xmlAPP 传入applicationContext.xml的路径
* @return BeanFactory
*/
public static BeanFactory getBeanFactory(String xmlAPP){
return (BeanFactory)getApplicationContext(new String[]{xmlAPP});
}
}
<?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-2.5.xsd">
<bean name="User" class="com.spring.po.User">
<property name="username" value="谢成志"></property>
<property name="password"><value>234sdfedwe</value></property>
<property name="sex"><value>男</value></property>
</bean>
</beans>
package com.spring;
import org.springframework.beans.factory.BeanFactory;
import com.spring.po.User;
import com.spring.util.BeanFactoryUtil;
public class Test {
public static void main(String[] args) {
BeanFactory factory = BeanFactoryUtil.getBeanFactory("applicationContext.xml");
User u = (User)factory.getBean("User");
System.out.println(u);
}
}
AOP(Aspect Oriented Programming) 面向切面编程
OOP(Object Oriented Programming) 面向对象编程
二者区别:
是面向不同领域的两种设计思想,
OOP针对业务处理过程的实体及其属性和行为进行抽象封装,以或得更加清晰高效的逻辑单元划分;
AOP则是针对业务处理过程中的切面进行提取,其面对的是处理过程中的某一个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果。
切面(Aspect)
连接点(JoinPoint)
处理逻辑(Advice)
切点(PointCut)