第一个spring项目
1、maven依赖导入
<dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.3.22</version>
</dependency>
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope>
</dependency>
2、编写代码
2.1、不使用IOC容器
2.1.1、创建Hello实体类
public class Hello {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public void show() {System.out.println("name= " + name);}
}
2.2、创建beans.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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 配置Hello实体类 --><bean id="hello" class="Hello"><!-- collaborators and configuration for this bean go here --></bean><!-- more bean definitions go here --></beans>
2.3、测试
public class MyTest {@Testpublic void helloSpring() {//1. 解析beans.xml文件,生成对应的bean管理contextApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");//2. 得到根据bean id对应的beanHello hello = context.getBean("hello", Hello.class);hello.setName("tom");hello.show();}
}
注意:这里未使用IOC容器
2.2、使用IOC容器
2.2.1、创建PetDao
public interface PetDao {void say();
}
2.2.2、创建Cat实现类
public class Cat implements PetDao {@Overridepublic void say() {System.out.println("小猫喵喵叫");}
}
2.2.3、创建CatService
public class PetService {private PetDao petDao;//IOC使用set方法依赖注入public void setPetDao(PetDao petDao) {this.petDao = petDao;}public void say() {petDao.say();}}
2.2.4、创建xml文件
- 创建services.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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!-- services --><bean id="petService" class="com.example.service.PetService"><property name="petDao" ref="petDao"/><!-- additional collaborators and configuration for this bean go here --></bean><!-- more bean definitions for services go here --></beans>
- 创建daos.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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="petDao"class="com.example.dao.impl.Cat"><!-- additional collaborators and configuration for this bean go here --></bean><!-- more bean definitions for data access objects go here --></beans>
2.2.5、测试
public class MyTest {@Testpublic void helloSpring() {//1. 解析beans.xml文件,生成对应的bean管理contextApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");//2. 得到根据bean id对应的beanHello hello = context.getBean("hello", Hello.class);hello.setName("tom");hello.show();}@Testpublic void test() {//1. 解析xml文件,生成对应的contextApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");//2. 得到对应的beanPetService petService = context.getBean("petService", PetService.class);petService.say();}
}
3、结尾
创建类图:
Spring官方文档:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-basics