1、如何学习spring?
你可以通过下列途径学习spring:
(1) spring下载包中doc目录下的mvc-step-by-step和sample目录下的例子都是比较好的spring开发的例子。
(2) appfuse集成了目前最流行的几个开源轻量级框架或者工具 ant,xdoclet,spring,hibernate(ibatis),junit,cactus,strutstestcase,canoo's webtest,struts menu,display tag library,oscache,jstl,struts 。
你可以通过appfuse源代码来学习spring。
appfuse网站:http://raibledesigns.com/wiki/wiki.jsp?page=appfuse
(3)spring 开发指南(夏昕)(http://www.xiaxin.net/spring_dev_guide.rar)
一本spring的入门书籍,里面介绍了反转控制和依赖注射的概念,以及spring的bean管理,spring的mvc,spring和hibernte,ibatis的结合。
(4) spring学习的中文论坛
springframework中文论坛(http://spring.jactiongroup.net)
java视线论坛(http://forum.javaeye.com)的spring栏目
2、利用spring框架编程,console打印出log4j:warn please initialize the log4j system properly?
说明你的log4j.properties没有配置。请把log4j.properties放到工程的classpath中,eclipse的classpath为bin目录,由于编译后src目录下的文件会拷贝到bin目录下,所以你可以把log4j.properties放到src目录下。
这里给出一个log4j.properties的例子:
3、出现 java.lang.noclassdeffounderror?
一般情况下是由于你没有把必要的jar包放到lib中。
比如你要采用spring和hibernate(带事务支持的话),你除了spring.jar外还需要hibernat.jar、aopalliance.jar、cglig.jar、jakarta-commons下的几个jar包。
http://www.springframework.org/download.html下载spring开发包,提供两种zip包
spring-framework-1.1.3-with-dependencies.zip和spring-framework-1.1.3.zip,我建议你下载spring-framework-1.1.3-with-dependencies.zip。这个zip解压缩后比后者多一个lib目录,其中有hibernate、j2ee、dom4j、aopalliance、jakarta-commons等常用包。
4、java.io.filenotfoundexception: could not open class path resource [....hbm.xml],提示找不到xml文件?
原因一般有两个:
(1)该xml文件没有在classpath中。
(2)applicationcontext-hibernate.xml中的xml名字没有带包名。比如:
5、org.springframework.beans.notwritablepropertyexception: invalid property 'postdao' of bean class?
出现异常的原因是在application-xxx.xml中property name的错误。
<property name="...."> 中name的名字是与bean的set方法相关的,而且要注意大小写。
比如
那么xml的定义应该是:
6、spring中如何实现事务管理?
首先,如果使用mysql,确定mysql为innodb类型。
事务管理的控制应该放到商业逻辑层。你可以写个处理商业逻辑的javabean,在该javabean中调用dao,然后把该bean的方法纳入spring的事务管理。
比如:xml文件定义如下:
com.yz.spring.service.implement.usermanageimpl就是我们的实现商业逻辑的javabean。我们通过parent元素声明其事务支持。
7、如何管理spring框架下更多的javabean?
javabean越多,spring配置文件就越大,这样不易维护。为了使配置清晰,我们可以将javabean分类管理,放在不同的配置文件中。 应用启动时将所有的xml同时加载。
比如:
dao层的javabean放到applicationcontext-hibernate.xml中,商业逻辑层的javabean放到applicationcontext-service.xml中。然后启动类中调用以下代码载入所有的applicationcontext。
8、web应用中如何加载applicationcontext?
可以通过定义web.xml,由web容器自动加载。
9、在spring中如何配置的log4j?
在web.xml中加入以下代码即可。
10、spring框架入门的编程问题解决了,我该如何更深地领会spring框架呢?
这两本书你该去看看。这两本书是由spring的作者rod johnson编写的。
你也该看看martinfowler的inversion of control containers and the dependency injection pattern。
再好好研读一下spring的文档。
还有就是多实践吧。
你可以通过下列途径学习spring:
(1) spring下载包中doc目录下的mvc-step-by-step和sample目录下的例子都是比较好的spring开发的例子。
(2) appfuse集成了目前最流行的几个开源轻量级框架或者工具 ant,xdoclet,spring,hibernate(ibatis),junit,cactus,strutstestcase,canoo's webtest,struts menu,display tag library,oscache,jstl,struts 。
你可以通过appfuse源代码来学习spring。
appfuse网站:http://raibledesigns.com/wiki/wiki.jsp?page=appfuse
(3)spring 开发指南(夏昕)(http://www.xiaxin.net/spring_dev_guide.rar)
一本spring的入门书籍,里面介绍了反转控制和依赖注射的概念,以及spring的bean管理,spring的mvc,spring和hibernte,ibatis的结合。
(4) spring学习的中文论坛
springframework中文论坛(http://spring.jactiongroup.net)
java视线论坛(http://forum.javaeye.com)的spring栏目
2、利用spring框架编程,console打印出log4j:warn please initialize the log4j system properly?
说明你的log4j.properties没有配置。请把log4j.properties放到工程的classpath中,eclipse的classpath为bin目录,由于编译后src目录下的文件会拷贝到bin目录下,所以你可以把log4j.properties放到src目录下。
这里给出一个log4j.properties的例子:
| log4j.rootlogger=debug,stdout log4j.appender.stdout=org.apache.log4j.consoleappender log4j.appender.stdout.layout=org.apache.log4j.patternlayout log4j.appender.stdout.layout.conversionpattern=%d %5p (%f:%l) - %m%n |
3、出现 java.lang.noclassdeffounderror?
一般情况下是由于你没有把必要的jar包放到lib中。
比如你要采用spring和hibernate(带事务支持的话),你除了spring.jar外还需要hibernat.jar、aopalliance.jar、cglig.jar、jakarta-commons下的几个jar包。
http://www.springframework.org/download.html下载spring开发包,提供两种zip包
spring-framework-1.1.3-with-dependencies.zip和spring-framework-1.1.3.zip,我建议你下载spring-framework-1.1.3-with-dependencies.zip。这个zip解压缩后比后者多一个lib目录,其中有hibernate、j2ee、dom4j、aopalliance、jakarta-commons等常用包。
4、java.io.filenotfoundexception: could not open class path resource [....hbm.xml],提示找不到xml文件?
原因一般有两个:
(1)该xml文件没有在classpath中。
(2)applicationcontext-hibernate.xml中的xml名字没有带包名。比如:
| <bean id="sessionfactory" class="org.springframework.orm.hibernate.localsessionfactorybean"> <property name="datasource"><ref bean="datasource"/></property> <property name="mappingresources"> <list> <value>user.hbm.xml</value> 错,改为: <value>com/yz/spring/domain/user.hbm.xml</value> </list> </property> <property name="hibernateproperties"> <props> <prop key="hibernate.dialect"> net.sf.hibernate.dialect.mysqldialect </prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> |
5、org.springframework.beans.notwritablepropertyexception: invalid property 'postdao' of bean class?
出现异常的原因是在application-xxx.xml中property name的错误。
<property name="...."> 中name的名字是与bean的set方法相关的,而且要注意大小写。
比如
| public class postmanageimpl extends basemanage implements postmanage { private postdao dao = null; public void setpostdao(postdao postdao){ this.dao = postdao; } } |
那么xml的定义应该是:
| <bean id="postmanage" parent="txproxytemplate"> <property name="target"> <bean class="com.yz.spring.service.implement.postmanageimpl"> <property name="postdao"><ref bean="postdao"/></property> 对 <property name="dao"><ref bean="postdao"/></property> 错 </bean> </property> </bean> |
6、spring中如何实现事务管理?
首先,如果使用mysql,确定mysql为innodb类型。
事务管理的控制应该放到商业逻辑层。你可以写个处理商业逻辑的javabean,在该javabean中调用dao,然后把该bean的方法纳入spring的事务管理。
比如:xml文件定义如下:
| <bean id="txproxytemplate" abstract="true" class="org.springframework.transaction.interceptor.transactionproxyfactorybean"> <property name="transactionmanager"><ref bean="transactionmanager"/></property> <property name="transactionattributes"> <props> <prop key="save*">propagation_required</prop> <prop key="remove*">propagation_required</prop> <prop key="*">propagation_required</prop> </props> </property> </bean> <bean id="usermanage" parent="txproxytemplate"> <property name="target"> <bean class="com.yz.spring.service.implement.usermanageimpl"> <property name="userdao"><ref bean="userdao"/></property> </bean> </property> </bean> |
com.yz.spring.service.implement.usermanageimpl就是我们的实现商业逻辑的javabean。我们通过parent元素声明其事务支持。
7、如何管理spring框架下更多的javabean?
javabean越多,spring配置文件就越大,这样不易维护。为了使配置清晰,我们可以将javabean分类管理,放在不同的配置文件中。 应用启动时将所有的xml同时加载。
比如:
dao层的javabean放到applicationcontext-hibernate.xml中,商业逻辑层的javabean放到applicationcontext-service.xml中。然后启动类中调用以下代码载入所有的applicationcontext。
| string[] paths = {"com/yz/spring/dao/hibernate/applicationcontext-hibernate.xml", "com/yz/spring/service/applicationcontext-service.xml"}; ctx = new classpathxmlapplicationcontext(paths); |
8、web应用中如何加载applicationcontext?
可以通过定义web.xml,由web容器自动加载。
| <servlet> <servlet-name>context</servlet-name> <servlet-class>org.springframework.web.context.contextloaderservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <context-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/applicationcontext-hibernate.xml</param-value> <param-value>/web-inf/applicationcontext-service.xml</param-value> </context-param> |
9、在spring中如何配置的log4j?
在web.xml中加入以下代码即可。
| <context-param> <param-name>log4jconfiglocation</param-name> <param-value>/web-inf/log4j.properties</param-value> </context-param> |
10、spring框架入门的编程问题解决了,我该如何更深地领会spring框架呢?
这两本书你该去看看。这两本书是由spring的作者rod johnson编写的。
| expert one on one j2ee design and development expert one on one j2ee development without ejb |
你也该看看martinfowler的inversion of control containers and the dependency injection pattern。
| http://www.martinfowler.com/articles/injection.html |
再好好研读一下spring的文档。
| http://www.jactiongroup.net/reference/html/index.html(中文版,未全部翻译) |
还有就是多实践吧。
闽公网安备 35060202000074号