目錄結構:
contents structure [-]
Spring是一個容器框架,用於配置bean並且維護bean之間關系的框架。
Spring的結構圖:
我的結構目錄:
建立一個User類
package com.server; public class User { private String name; public void getName() { System.out.println("Hello "+name); } public void setName(String name) { this.name = name; } }
package com.test; /* *引入ApplicationContext和ClassPathXmlApplication */ import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.server.User; public class Test { public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); User user = (User)ac.getBean("user");//根據Bean的id來識別 user.getName(); } }
ApplicationContext是一個接口,由ClassPathXmlApplicationContext尋找applicationContext這個文件,在開發包裡一個index.html的網頁文件,讀者可以在裡面查找相應Spring版本的API文檔。
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 在容器文件中配置bean(service/dao.domain/action/數據原); --> <!-- bean的作用是,當我們的Spring框架加載的時候,spring就會自動創建一個bean對象,並且放入內存。id是標識符,class指定路徑 本例相當於: User user=new User(); user.setName(""chauncy); --> <bean id="user" class="com.server.User"> <property name="name"> <value>chauncy</value> </property> </bean>
</beans>
http://blog.csdn.net/zoutongyuan/article/details/27073683
本文為博主原創文章,如需轉載請注明出處。