`
hhr_michael
  • 浏览: 72579 次
  • 性别: Icon_minigender_1
  • 来自: 惠州
社区版块
存档分类
最新评论

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

阅读更多
                                     第15章 Spring入门


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

一. 搭建Spring2开发环境

1. 建立java工程

2. 建立lib文件夹

3. 将Spring集成到工程中
(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


二. 使用Spring2开发HelloWorld


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

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

package amigo.spring.chapter15;

public class HelloBean {
		private String str;

		public String getStr() {
			return str;
		}

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


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

<?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>



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

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());
		}
}



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

引用
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!
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics