ClassPathXmlApplicationContext context1 = new ClassPathXmlApplicationContext("spring.xml");
1.
读取配置文件(下面这一个只能写一次,多写报错. 用*.properties)
<context:property-placeholder location="classpath:resource.properties"/>
或者
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>
文件格式
a=b
c=d
使用示例
<!-- 让spring管理数据库连接池 -->
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}"/>
<property name="jdbcUrl" value="${url}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
Controller 默认是单例模式,即变量会跟静态变量似得,方法前加上@Scope("prototype")会变成多例模式
scope="singleton"
<!-- 开启扫描 -->
<mvc:annotation-driven ></mvc:annotation-driven>
<!-- 配置包扫描器 -->
<context:component-scan base-package="com.yh.controller"></context:component-scan>
用来管理javabean,可以理解spring就是粘合剂,他可以将各个框架(struts2,hibernate/mybatis)整合起来。
是分层的JAVAEE 轻量级的开源框架 (EJB(企业级javabean))
ApplicationContext
•
国际化支持
•
资源访问
•
事件传递
•
载入多个配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“applicationContext.xml”);//单个文件
BeanFactory beanFactory = new ClassPathXmlApplicationContext(new String[]{“applicationContext.xml”,“bean.xml”}); //多个文件
BeanFactory beanFactory =
new FileSystemXmlApplicationContext(
new String[]{
"/WebRoot/WEB-INF/classes/applicationContext.xml",
"/WebRoot/WEB-INF/classes/bean.xml"});
// 以classpath路径下的applicationContext.xml创建Spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 从Spring容器中获取ID为user的Bean
UserVo user = (UserVo) applicationContext.getBean("user");
// 封装姓名和年龄
user.setName("张三丰");
user.setAge(100);
// 输出该Bean的信息
user.printInfo();
优点
•
方便解耦,简化开发
spring就是一个大工厂,可以将所有的对象的创建和依赖关系进行维护。
•
AOP编程支持
•
spring支持面向切面编程,可以方便对程序进行拦截和监控
•
声明式事务
•
通过配置完成事务的管理,无需手动编程
•
方便程序的测试
•
spring支持junit4,完成程序测试
•
方便集成各个优秀的框架
•
hibernate mybatis ....
•
降低javaeeapi的使用
•
spring将难用的api进行封装。
•
IOC思想
控制翻转(inverse of control): 将实例对象的创建权利交给工厂(spring容器)
DI思想
依赖注入:一个对象A使用另一个对象B的过程称为依赖。
从spring容器中获取A对象的时候,使用spring自动将A对象所需要的B对象进行设置,此过程称为注入。
简单理解:就是spring在生成对象的时候,还可以将该对象所需要的对象进行赋值。
BeanFactory和ApplicationContext(图)
国际化处理
事件传递
bean自动装箱
Beanfactory采用延迟加载。
schema配置
el表达式(自学)
配置文件
<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">
<context:annotation-config/>
<!-- 扫描指定包下面的注解 -->
<context:component-scan base-package="com.yh"/>
</beans>
配置文件的加载
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext ac = new FileSystemXmlApplicationContext("./src/applicationContext.xml");
Resource r = new ClassPathResource("applicationContext.xml");
BeanFactory bf = new XmlBeanFactory(r);
评论区