![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public interface Action { public String execute(String str); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class UpperAction implements Action { private String message; public String getMessage() { return message; } public void setMessage(String string) { message = string; } public String execute(String str) { return (getMessage() + str).toUpperCase(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <bean id="theAction" class="UpperAction"> <!-- abstract="false" lazy-init="default" autowire="default">--> <property name="message"> <value type="java.lang.String">Hello_</value> </property> </bean> </beans>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestAction { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); Action bean = (Action) ctx.getBean("theAction"); System.out.println(bean.execute("Rod")); } }
Spring有两个核心思想:
(1) 控制反转(IOC) 指将创建对象的权利交给beanFactory;
beanFactory只是一个接口,包含 getBean() 方法,一般更常用基于beanFactory接口实现的ApplicationContext类。
实例化ApplicationContext一个对象,便可以通过XML文件获取bean!
Sring 根据bean去创建类的实例化对象!
(2) 面向切面编程(AOP) 通过代理,实现通用代码与业务逻辑的分离
对于属性,Spring中可以不用全部配置,配置一部分即可!