spring核心构架:
(1)核心模快(core):提供依赖注入工能,和对bean的管理功能
(2)AOP模块:提供对AOP的支持,允许我们面向切面开发。同时提供了AspectJ的整合
(3)上下文模块(Context):构建于核心模块之上,提供对Bean的框架式访问。还扩展了BeanFactory的功能
(4)DAO模块:用于对JDBC的操作
(5)ORM模块:该模块允许Spring集成2各种ORM(Object-Relation-Mapping)框架来实现持久层的应用
(6)Web模块:构建于核心模块之上,提供对web各种应用的支持
(7)Spring的MVC模块:提供一个玩美的MVC解决方案
Spring有三种注入方式:
(1)构造函数注入
(2)设值方法注入
(3)接口注入
简单的代码demo
1.Human接口的代码
package springdemo;
/**
* Human接口实现代码
*/
public interface Human {
public String sayHello(String str);
}
2.Person类实现代码
package springdemo;
/**
* Person实现类的实现代码
* @author Administrator
*
*/
public class Person implements Human {
public String sayHello(String str) {
return "你好"+str+"欢迎光临!";
}
}
3.web.xml代码
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
4.bean.xml代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="Person" class="springdemo.Person" />
</beans>
5.测试代码Test.java
package springdemo;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
/**
* 测试类
* @author Administrator
*
*/
public class Test {
public static void main(String[] args) {
Resource res = new ClassPathResource("springdemo/bean.xml");
BeanFactory factory = new XmlBeanFactory(res);
Human human = (Human) factory.getBean("Person");
System.out.println(human.sayHello("person"));
}
}