**Spring框架的IOC核心功能快速入門(必須掌握開發的步驟)**0. 什么是IOC的功能?* IOC -- Inverse of Control,控制反轉,將對象的創建權反轉給Spring!!* 使用IOC可以解決的程序耦合性高的問題!!1. 步驟一:下載Spring框架的開發包* 下載路徑為:http://repo.spring.io/webapp/search/artifact/?0&q=spring-framework* 解壓后的目錄結構如下* docs -- API和開發規范* libs -- jar包和源碼* Spring框架的jar包的特點是每個jar包都有3個(使用的jar包、文檔的jar包和源代碼的jar包) * schema -- 約束2. 步驟二:創建JavaWEB項目,引入Spring的開發包* 引入Spring框架IOC核心功能需要的具體的jar包* Spring框架的IOC的功能,那么根據Spring框架的體系結構圖能看到,只需要引入如下的jar包* 在spring-framework-3.2.0.RELEASE/libs/下* Beans* Core* Context* Expression Language* Spring框架也需要引入日志相關的jar包* 在spring-framework-3.0.2.RELEASE-dependencies/org.apache.commons/com.springsource.org.apache.commons.logging/1.1.1* com.springsource.org.apache.commons.logging-1.1.1.jar* 還需要引入log4j的jar包 spring-framework-3.0.2.RELEASE-dependencies\org.apache.log4j\com.springsource.org.apache.log4j\1.2.15* com.springsource.org.apache.log4j-1.2.15.jar3. 步驟三:創建對應的包結構,編寫Java的類,要注意:以后使用Spring框架做開發,都需要來編寫接口與實現類!!* com.itcast.demo1* UserService -- 接口* UserServiceImpl -- 具體的實現類4. 步驟四:想把UserServiceImpl實現類的創建交給Spring框架來管理,需要創建Spring框架的配置文件,完成配置* 1. 在src目錄下創建applicationContext.xml的配置文件,名稱是可以任意的,但是一般都會使用默認名稱!!* 2. 引入spring的約束,需要先找到具體的約束頭信息!!* spring-framework-3.2.0.RELEASE\docs\spring-framework-reference\html\xsd-config.html* 具體的約束如下: <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"></beans>* 3. 完成UserService的配置<!-- Spring的快速入門 --><bean id="userService" class="com.itcast.demo1.UserServiceImpl"/>5. 步驟五:編寫測試程序,采用Spring框架的工廠方式來獲取到UserService接口的具體實現類!!* 具體的代碼如下:public void demo2(){// 使用Spring的工廠:ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");// 通過工廠獲得類:UserService userService = (UserService) applicationContext.getBean("userService");userService.sayHello();
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 引入schema的約束 -->
<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">
<!-- 將User對象交給spring容器管理 -->
<bean name="user" class="cn.itcast.bean.User"></bean>
</beans>
User
public class User {private String name;private Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}}
netty框架教程、Demo
public class Demo {@Testpublic void fun1(){//1.創建容器對象ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");//2.向容器要user對象User user = (User) ac.getBean("user");//3.打印user對象System.out.println(user);}
}