1.IOC 控制反转:
就是应用本身不负责依赖对象的创建及维护,依赖对象及维护由外部容器负责。
Public class PersonServiceBean{
//在应用本身创建及维护 New
Private PersonDao pd = new PersonDao();
Public void save(Person person){
Pd.save(person);
}
}
2.依赖注入(DI) :
Public class PersonServiceBean{
//由外部容器管理 创建 及 维护
Private PersonDao personDao;
//通过setter注入
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
//通过构造函数注入
Public PersonServiceBean(PersonDao personDao){
this.personDao = personDao;
}
Public void save(Person person){
personDao.save(person);
}
}
依赖注入: 在运行期,由外部容器动态将依赖对象注入到组件中。
3.为什么使用spring ?:
1.降低组件之间的耦合度。
2.对主流的框架提供了集成支持。
3.提供了AOP技术。
4.需要的JAR :
Dist\spring-jar
Lib\jakarta-commons\commons-logging.jar
Lib\aspect\aspectjweaver.jar aspectjrt.jar
Lib\cglib\cglib-nodep-2.*
Lib\j2ee\common-annotations.jar
5.<?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">
6.实例化spring 容器 :
方法一:
在类路径下寻找配置文件来实例化容器.
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{“applicationContext.xml”} );
方法二:
在文件系统路径下来寻找配置文件来实例化容器:
ApplicationContext ctx = new FileSystemXmlApplicationContext( new String[]{“d:\\applicationContxt.xml});
可以 指定多个,通过string数组导入。。
ctx.getBean(“获取由spring管理的bean名称 --Id”)
7.使用Dom4j 读取spring 配置文件.
package org.junit.test;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
public class ItcastClassPathXMLApplicationContext {
private List<BeanDefinition> definitions =new ArrayList<BeanDefinition>();
public ItcastClassPathXMLApplicationContext(String filename){
this.readXML(filename);
}
/**
*读取xml配置文件
*/
private void readXML(String filename) {
SAXReader saxReader = new SAXReader();
Document document=null;
try {
URL xmlpath =this.getClass().getClassLoader().getResource(filename);
document=saxReader.read(xmlpath);
Map<String, String> nsmMap = new HashMap<String, String>();
nsmMap.put("ns", "http://www.springframework.org/schem/beans");//加入命名空间
XPath xsub = document.createXPath("//ns:beans/ns:bean");//创建beans/bean查询路径
xsub.setNamespaceURIs(nsmMap);//设置命名空间
List<Element> beans = xsub.selectNodes(document);//获取文档下bean的所有节点
for (Element element : beans) {
String id = element.attributeValue("id");//获取ID属性名
String className = element.attributeValue("class");//获取class属性值
BeanDefinition bd = new BeanDefinition(id, className);
definitions.add(bd);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
8.三种 实例化Bean方式 :
1.使用类构造器实例化;
<bean id=”” class=””/>
2.使用静态工厂实例化
<bean id=”” class=”” factory-method=”createOrder”/>
Public class orderfactory{
Public static OreadServiceFactory createOrder{
Return new OrederServiceFactory();
}
}
3.使用实例化工厂实现实例化:
4.单列模式
9.bean 的作用域 :
1. Singleton :
一个bean定义只能有一个对象实例。默认 容器启动时初始化bean,指定lazy-init=”true”延迟初始化bean。
2. Prototype :
每次从容器获取bean都是新的对象。
<bean id=”” class=”” scope=”prototype”/> 用于action
10.集合注入
Set注入
<bean id="personSerivce"class="org.services.impl.PersonServiceImpl">
<property name="sets">
<set>
<value>第一个</value>
</set>
</property>
</bean>
11.通过classpath 自动扫描导入外部定义的组件进spring容器中2.5及以上
<!-- 扫描指定包以下的类,包括子类 -->
<context:component-scan base-package=""/>
<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"
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.xsd
">
通过注解:@Component 指组件,
@Service 标记业务层组件
@Controller 标记控制层组件
@Repository 数据访问组件 DAO
12. AOP 代理对象 :

被折叠的 条评论
为什么被折叠?



