Spring基础
一.简单案例 打印hello spring
1.导包
<!--beans--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.3.RELEASE</version> </dependency> <!--context--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.2.RELEASE</version> </dependency>
2.创建HappyService类
public class HappyService { private String info; public void work(){ System.out.println("Hello "+info); } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } }
3.创建applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd <!--IOC--> <bean id="HappyService" class="cn.bdqn.service.HappyService"> <!--DI依赖注入--> <property name="info" value="Spring"></property> </bean> </beans>
4.测试类
@Test public void test01(){ ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); HappyService service=(HappyService)ac.getBean("HappyService"); service.work(); }