今天上班有點忙,並且明天就要放假了,利用中午休息的時間,重溫了下spring 的ioc容器。
java
一開始我建立了一個maven項目,命名爲spring-learn。並上傳到了github作爲一開源的學習歷程項目,地址爲 https://github.com/Juneve/spring-learn.git。接下來我將ioc須要的spring模塊導入,經過編輯pom文件很是容易的就處理好了,簡單列下pom.xml文件。 git
<!-- spring begin --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- spring end -->
主要導入了三個包,我也不太清楚spring-core包是否須要導入。github
前期工做準備的差很少了,接下來開始寫代碼了。代碼以下:
spring
public class User { private String name; private String email; private Date birthday; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
public class UserDao { public void save(User user) { System.out.println("Save user " + user.getName()); } }
public class UserServer { private UserDao userDao; public void add(User user) { userDao.save(user); } public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } }
因爲只是簡單使用下spring的ioc功能,因此,就沒有在dao、server層定義接口,而是直接實現了兩個簡單的類。接下來配置一下spring的配置文件,我命名爲applicationContext.xml。其內容以下:app
<?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.xsd"> <bean id="user" class="org.tunie.learn.spring.ioc.model.User"> <property name="name" value="Tunie"/> <property name="email" value="tomailxj@163.com"/> </bean> <bean id="userDao" class="org.tunie.learn.spring.ioc.dao.UserDao"/> <bean id="userServer" class="org.tunie.learn.spring.ioc.server.UserServer"> <property name="userDao" ref="userDao"/> </bean> </beans>
對於配置文件中的beans及bean詳細說明,我以爲這些東西一目瞭然,就不贅述了。好,萬事俱備,只差一個單元測試了。單元測試代碼以下:
maven
public class UserServerTest { @SuppressWarnings("resource") @Test public void testAdd() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContent.xml"); UserServer server = applicationContext.getBean("userServer", UserServer.class); User user = applicationContext.getBean("user",User.class); server.add(user); } }
因爲用到了JUnit 4,因此須要在pom中引入依賴,文件以下:單元測試
<!-- spring end --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.1</version> <scope>test</scope> </dependency>
運行下這個單元測試,程序順利運行了。學習
經過使用spring ioc container,咱們只須要在配置文件中將對象配置一下,而後經過ApplicationContext的getBean方法,傳入配置文件對應的bean id,咱們就能取得該對象的實例了。這樣對象的實例化被解耦出來,接下來咱們將深刻學習bean的配置。測試