2 Spring IoC
目录
POM
案例一,基于 xml 配置
创建 entity
创建 Spring 配置文件
测试 Spring IoC
案例一,基于注解配置
改造 entity
改造 Spring 配置文件
改造测试类 Spring IoC
案例二,基于 xml 配置
创建 StudentService 接口
创建 UserServiceImpl 实现类
创建 Spring 配置文件
测试 Spring IoC
案例二,基于注解配置
改造实现类,加上注解 @Service
改造 Spring 配置文件
改造测试类 Spring IoC
POM
创建一个工程名为 spring-ioc-demo 的项目,pom.xml 文件如下:
4.0.0
com.example
spring-ioc-demo
1.0.0-SNAPSHOT
UTF-8
1.8
1.8
5.1.5.RELEASE
1.16.20
4.12
1.2.17
1.7.25
org.springframework
spring-context
${spring.version}
org.springframework
spring-test
${spring.version}
test
junit
junit
${junit.version}
org.projectlombok
lombok
${lombok.version}
provided
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
org.slf4j
jcl-over-slf4j
${slf4j.version}
org.slf4j
jul-to-slf4j
${slf4j.version}
log4j
log4j
${log4j.version}
org.apache.maven.plugins
maven-compiler-plugin
3.7.0
${java.version}
${java.version}
${project.build.sourceEncoding}
true
核心包:主要增加了 org.springframework: spring-context 依赖
案例一,基于 xml 配置
创建 entity
@Data
public class Student {
private int id;
private String name;
private String email;
private String address;
private Hobboy hobboy;
}
@Data
public class Hobboy {
private String basketball;
private String football;
private String running;
}
创建 Spring 配置文件
在 src/main/resources 目录下创建 spring-context.xml 配置文件,从现在开始类的实例化工作交给 Spring 容器管理(IoC),配置文件如下:
:用于定义一个实例对象。一个实例对应一个 bean 元素。
id:该属性是 Bean 实例的唯一标识,程序通过 id 属性访问 Bean,Bean 与 Bean 间的依赖关系也是通过 id 属性关联的。
class:指定该 Bean 所属的类,注意这里只能是类,不能是接口
测试 Spring IoC
创建一个 GetBeanTest 测试类,测试对象是否能够通过 Spring 来创建,代码如下:
public class GetBeanTest {
@Test
public void testGetBean() {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-context.xml");
Student stu = (Student) applicationContext.getBean("stu");
System.out.println(stu);
}
}
测试结果:
Student(id=10, name=vincent, email=601521821@qq.com, address=上海市、宝山区, hobboy=Hobboy(basketball=Nike, football=Adidas, running=5km))
案例一,基于注解配置
改造 entity
@Data
@Component
public class Student {
@Value("10")
private int id;
@Value("Vincrnt")
private String name;
@Value("601521821@qq.com")
private String email;
@Value("上海市宝山区")
private String address;
@Autowired
private Hobboy hobboy;
}
@Data
@Component
public class Hobboy {
@Value("Nike")
private String basketball;
@Value("Adidas")
private String football;
@Value("5km")
private String running;
}
改造 Spring 配置文件
改造测试类 Spring IoC
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-context.xml")
public class GetBeanTest {
@Autowired
private Student student;
@Test
public void annotationStu(){
System.out.println(student);
}
}
测试结果:
Student(id=10, name=vincent, email=601521821@qq.com, address=上海市、宝山区, hobboy=Hobboy(basketball=Nike, football=Adidas, running=5km))
案例二,基于 xml 配置
创建 StudentService 接口
public interface StudentService {
public void sayHi();
}
创建 UserServiceImpl 实现类
public class StudentServiceImpl implements StudentService {
public void sayHi() {
System.out.println("Hello Spring IoC !!!");
}
}
创建 Spring 配置文件
在 src/main/resources 目录下创建 spring-context.xml 配置文件,从现在开始类的实例化工作交给 Spring 容器管理(IoC),配置文件如下:
:用于定义一个实例对象。一个实例对应一个 bean 元素。
id:该属性是 Bean 实例的唯一标识,程序通过 id 属性访问 Bean,Bean 与 Bean 间的依赖关系也是通过 id 属性关联的。
class:指定该 Bean 所属的类,注意这里只能是类,不能是接口。
测试 Spring IoC
创建一个 GetBeanTest 测试类,测试对象是否能够通过 Spring 来创建,代码如下:
public class GetBeanTest {
@Test
public void sayHi() {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-context.xml");
StudentService studentServiceImpl = (StudentService) applicationContext.getBean("studentService");
String sayHi = studentServiceImpl.sayHi();
System.out.println(sayHi);
}
}
测试结果
Hello Spring IoC !!!
案例二,基于注解配置
改造实现类,加上注解 @Service
@Service
public class StudentServiceImpl implements StudentService {
@Override
public String sayHi() {
return "Hello Spring IoC !!!";
}
}
改造 Spring 配置文件
改造测试类 Spring IoC
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-context.xml")
public class GetBeanTest {
@Autowired
private StudentService studentService;
@Test
public void annotationSayHi() {
String sayHi = studentService.sayHi();
System.out.println(sayHi);
}
测试结果:
Hello Spring IoC !!!
