Spring是什么的,做什么的?
Spring是一个轻量级开发框架,为了解决企业级应用开发的业务逻辑和其他各层的耦合问题
public interface TestDIDao { void sayhello(); }
import com.dao.TestDIDao; public class TestDIDaoImpl implements TestDIDao{ @Override public void sayhello() { System.out.println("Hello World!"); } }
<?xml version="1.0" encoding="UTF-8"?> <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-4.0.xsd"> <!-- 将指定的类TestDaoImpl配置给Spring,让Spring创建该实例 --> <bean id="testDIDao" class="com.dao.impl.TestDIDaoImpl" /> </beans>
public class Test { @SuppressWarnings("resource") public static void main(String[] args) { //初始化spring容器ApplicationContext,加载配置文件 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); TestDIDaoImpl bean = (TestDIDaoImpl) context.getBean("sayHello"); bean.sayhello(); } }
原文:https://www.cnblogs.com/superhonors/p/11632436.html