spring总结

本文详细介绍了如何在项目中使用Spring框架进行组件管理、配置和注入属性,并提供了创建ApplicationContext和实现单例模式的示例。


韩顺平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;
}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值