下面主要讲解spring中IOC的DI几种配置方式
一、
public class Student {
private String name;
private String sex;
public Student(){}
public Student(String name, String sex) {
this.name = name;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student [name=" + name + ", sex=" + sex + "]";
}
public Student student(){
return new Student("sunny", "male");
}
}
public class StudentService {
private Student student;
public void getStudent(){
System.out.println(student.toString());
}
public void setStudent(Student student) {
this.student = student;
}
}
我们有一个简单的Student类和StudentService类,StudentService中只有一个成员变量student,我们主要讲解如何自动注入这个student这个变量
最简单的我们在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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="student" class="com.sunny.spring.ioc.annotation.Student">
<property name="name" value="tomcat"/>
<property name="sex" value="man"/>
</bean>
<bean id="studentService" class="com.sunny.spring.ioc.annotation.StudentService">
<property name="student" ref="student"/>
</bean>
</beans>
Test:: 发现我们可以输出Student的信息,我们并没有new Student
<pre name="code" class="html">ApplicationContext appContenxt = new ClassPathXmlApplicationContext(new String[]{"SpringIOC-Student.xml"});
StudentService studentService = (StudentService)appContenxt.getBean("studentService");
studentService.getStudent();
}
二、注解方式、自动注入
修改xml配置文件如下: <context:annotation-config/> 这个一定要加上,这个是启用注解
<context:annotation-config/>
<bean id="student" class="com.sunny.spring.ioc.annotation.Student">
<property name="name" value="tomcat"/>
<property name="sex" value="man"/>
</bean>
<bean id="studentService" class="com.sunny.spring.ioc.annotation.StudentService">
</bean>
修改StudentService如下
public class StudentService {
//@Autowired
private Student student;
public void getStudent(){
System.out.println(student.toString());
}
/*
* @Autowired 修饰函数时候,当调用getBean()获得实例的时候就会自动注入所需对象
*/
@Autowired
public void setStudent(Student student) {
this.student = student;
}
}
这里主要引入 @Autowired 注解,@Autowired可以修饰变量,可以修饰函数(包括set函数和构造函数),现在再执行上面的测试代码,结果一样
当然Spring官网还提供了其他的一些用来自动注入的注解,比如@Request等,我们一般就用@Autowired这个。
三、
修改配置文件如下
<!--spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean -->
<context:component-scan base-package="com.sunny.spring.ioc.beanfactory"/>
<bean id="studentService" class="com.sunny.spring.ioc.annotation.StudentService">
</bean>
注意,这里我们删除了Bean student,我们用注解的方式来定义一个Bean,新建下面这个类如下:
@Component
public class AppBean {
@Bean
public Student student(){//函数名student相当于id
return new Student("panza","male");
}
}
我们在xml配置文件加入了配置context:component-scan base-package="com.sunny.spring.ioc.beanfactory"/>,那么Spring会在这个包下面扫描类,
如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean(自动加载的Spring的容器,我们就可以直接调用了)