首先感謝開源的世界,讓我們有更多的學(xué)習(xí)上升空間。本人學(xué)習(xí)spring的資料來自spring教程。但是按照上面的講解來實(shí)現(xiàn)時(shí),會(huì)遇到一些問題。并不是因?yàn)榻坛虒戝e(cuò)了,而是我確實(shí)是這個(gè)方面的小白,所以除了spring jar包本身以外,一些基本依賴沒有導(dǎo)入,所以才會(huì)遇到了所謂的小坑。
下面首先說一個(gè)問題,我到網(wǎng)上下載了所有的spring3.0依賴,然后按照步驟來實(shí)現(xiàn) 依賴注入如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="myhello" class="com.sanxin.org.HelloWorld">
<property name="name" value="zhangsan" />
</bean>
</beans>
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"/config/applicationContext.xml","/config/io.xml"});
我的配置文件都是沒有問題,但是,就是會(huì)跑錯(cuò)。
原因是沒有導(dǎo)入其他的包依賴(用來打印日志的包):commons-logging-1.2.jar。作為新手,確實(shí)折騰了一會(huì)兒,不過還好我會(huì)百度大法,所以這并沒有難到我。
下面還要說一個(gè)問題,除了xml文件的方式依賴注入外,還有代碼的依賴注入如下:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean(name="helloBean")
public HelloWorld helloWorld() {
return new HelloWorldImpl();
}
}
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
HelloWorld obj = (HelloWorld) context.getBean("helloBean");
沒錯(cuò),到這一步,我又走不通了,因?yàn)椋謷佸e(cuò)提示缺少一個(gè)包:cglib-nodep-2.2.2.jar。值得注意的是,本人按照提示,剛才下載了cglib-3.2.5.jar,是不是很像,不過這個(gè)包沒有解決我的bug。
好了,目前就遇到這兩處小坑,后續(xù)遇到了小坑,再慢慢總結(jié),謝謝觀看~~~