struts2+hibernate3+spring2读书笔记14(Spring入门)

[size=large] [b]第15章 Spring入门[/b][/size]


[b]本章导读语[/b]
Spring是一种非侵入式的开发模型,它为企业应用的开发提供了一种轻量级的解决方案。它的核心机制是依赖注入和基于AOP的声明事务管理。本章主要是讲述Spring的开发环境与第一个Spring实例。

[size=large][b]一. 搭建Spring2开发环境[/b][/size]

[b]1. 建立java工程[/b]

[b]2. 建立lib文件夹[/b]

[b]3. 将Spring集成到工程中[/b]
(1) 拷贝包到lib目录
(2) 添加包到编译路径。
(3) 配置log4j,内容如下:

#定义日志输出级别为DEBUG
log4j.rootLogger=DEBUG, CONSOLE

#输出源采用输出到控制台
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

#定义输出日志的布局采用的类是org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout

#定义日志输出的布局
log4j.appender.CONSOLE.layout.ConversionPattern=%c %x - %m%n

[b]
[size=large]二. 使用Spring2开发HelloWorld[/size][/b]

[b]1. 建立包目录(本例中的包名为:amigo.spring.chapter15)[/b]

[b]2. 编写HelloBean(在该类中提供对字符串str的getter/setter方法)[/b]

package amigo.spring.chapter15;

public class HelloBean {
private String str;

public String getStr() {
return str;
}

public void setStr(String str) {
this.str = str;
}

}


[b]3. 编写Spring配置文件(在src目录下建立,该文件中给HelloBean的实例中的str字符串赋值为”Hello world!”)[/b]

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
default-autowire="byName" default-lazy-init="true">

<bean name="HelloBean" class="amigo.spring.chapter15.HelloBean">
<property name="str">
<value>Hello World!</value>
</property>
</bean>
</beans>



[b]4.编写测试类 TestSpring.java(在amigo.spring.chapter15.test包下建立TestSpring类)[/b]

package amigo.spring.chapter15.test;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import amigo.spring.chapter15.HelloBean;

public class TestSpring {
public static void main(String[] args){
ClassPathResource res = new ClassPathResource("applicationContext.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);
HelloBean bean = (HelloBean)factory.getBean("HelloBean");
System.out.print(bean.getStr());
}
}



最后运行后 控制台输出的结果为:

[quote]org.springframework.util.ClassUtils - Class [edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap] or one of its dependencies is not present: java.lang.ClassNotFoundException: edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap
org.springframework.beans.factory.xml.PluggableSchemaResolver - Loading schema mappings from [META-INF/spring.schemas]
org.springframework.beans.factory.xml.PluggableSchemaResolver - Loaded schema mappings: {http://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd, http://www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util-2.0.xsd, http://www.springframework.org/schema/util/spring-util-2.0.xsd=org/springframework/beans/factory/xml/spring-util-2.0.xsd, http://www.springframework.org/schema/jee/spring-jee.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd, http://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd, http://www.springframework.org/schema/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd, http://www.springframework.org/schema/tx/spring-tx-2.0.xsd=org/springframework/transaction/config/spring-tx-2.0.xsd, http://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd, http://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool-2.0.xsd, http://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop-2.0.xsd, http://www.springframework.org/schema/tx/spring-tx.xsd=org/springframework/transaction/config/spring-tx-2.0.xsd, http://www.springframework.org/schema/tool/spring-tool-2.0.xsd=org/springframework/beans/factory/xml/spring-tool-2.0.xsd, http://www.springframework.org/schema/jee/spring-jee-2.0.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd, http://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd}
org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [applicationContext.xml]
org.springframework.beans.factory.xml.DefaultDocumentLoader - Using JAXP provider [com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl]
org.springframework.beans.factory.xml.PluggableSchemaResolver - Found XML schema [http://www.springframework.org/schema/beans/spring-beans-2.0.xsd] in classpath: org/springframework/beans/factory/xml/spring-beans-2.0.xsd
org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver - Loaded mappings [{http://www.springframework.org/schema/p=org.springframework.beans.factory.xml.SimplePropertyNamespaceHandler, http://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandler, http://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandler, http://www.springframework.org/schema/aop=org.springframework.aop.config.AopNamespaceHandler, http://www.springframework.org/schema/util=org.springframework.beans.factory.xml.UtilNamespaceHandler, http://www.springframework.org/schema/tx=org.springframework.transaction.config.TxNamespaceHandler}]
org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader - Loading bean definitions
org.springframework.beans.factory.xml.BeanDefinitionParserDelegate - No XML 'id' specified - using 'HelloBean' as bean name and [] as aliases
org.springframework.beans.factory.xml.XmlBeanFactory - Creating shared instance of singleton bean 'HelloBean'
org.springframework.beans.factory.xml.XmlBeanFactory - Creating instance of bean 'HelloBean' with merged definition [Root bean: class [amigo.spring.chapter15.HelloBean]; scope=singleton; abstract=false; lazyInit=true; autowireCandidate=true; autowireMode=1; dependencyCheck=0; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [applicationContext.xml]]
org.springframework.beans.factory.xml.XmlBeanFactory - Eagerly caching bean 'HelloBean' to allow for resolving potential circular references
Hello World![/quote]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值