标签:
public interface Action { public String execute(String str); }
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(); } }
<?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>
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) 通过代理,实现通用代码与业务逻辑的分离
标签:
原文地址:http://www.cnblogs.com/tengpan-cn/p/4908892.html