韩顺平spring和航院里面结合
开发包:
commons-logging-1.1.3
sommons-logging-api-1.1
spring-beans-3.2.4.RELEASE
spring-context-3.2.4.RELEASE
spring-core-3.2.4-RELEASE
spring-expression-3.2.4.RELEASE
也可以是这类开发包:
第一讲:Spring HelloWorld
引包:
commons-logging-1.1.1.jar
junit-4.10.jar
org.springframework.asm.3.1.0.RELEASE.jar
org.springframework.beans-3.1.0.RELEASE.jar
org.springframework.context-3.1.0.RELEASE.jar
org.springframework.croe-3.1.0.RELEASE.jar
org.springframework.expression-3.1.0.RELEASE.jar
配置bean和注入属性:
spring框架,它可以管理web层,业务层,dao层,持久层,该spring
可以配置各个层的组件(bean),并且维护各个bean之间的关系。
建立project 在maven repository 里面搜commons-logging,然后下载开发包
在src下面建议接口HelloWorld
在src下面创建applicationContext.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
</beans>
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext重量级的对象,做成单态,工具类,保证只有一个,要不很废内存:
在src下面建包类 com.util, ApplicationContextUtil
package com.util;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//final 是为防止别人外写这个类
final public class ApplicationContextUtil {
private static ApplicationContext ac=null;
//下面是构造函数搞成私有的,不让别人去构建
private ApplicationContextUtil()
{
}
//加载在静态块里面写.
static{
ac = new ClassPathXmlApplicationContext("applicationContext.xml");
}
public static ApplicationContext getApplicationContext()
{
return ac;
}
}