1.基于XML版本的DI注入
1.1 基于setter方法注入属性
<!--基于Setter方法注入,属性必须有set方法,setStuName-->
<bean id="student" class="com.wdksoft.Student">
<property name="stuName" value="AAA"/>
<property name="stuId" value="1"/>
</bean>
@Test
public void setter(){
//加载配置文件
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//调用getBean
Student student = (Student)applicationContext.getBean("student");
System.out.println(student.toString());
}
1.2 基于构造方法注入
<!--基于构造方法注入:必须完全指定参数值-->
<bean id="constructorStu" class="com.wdksoft.Student">
<!--name代表参数名字 value代表值 ref代表引用(类型,不能与value同时存在)
type代表属性类型
-->
<!--<constructor-arg name="stuId" value="2"/>
<constructor-arg name="stuName" value="AAA" />-->
<!--index代表参数下标-->
<constructor-arg index="0" value="2"/>
<constructor-arg index="1" value="AAA" />
</bean>
@Test
public void constructor(){
//加载配置文件
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//调用getBean
Student student = (Student)applicationContext.getBean("constructorStu");
System.out.println(student.toString());
}
1.3 P命名空间注入,基于setter
1.3.1 在大配置文件头导入p命名空间 xmlns:p="http://www.springframework.org/schema/p"
1.3.2 大配置文件配置
<bean id="pStu" class="com.wdksoft.Student" p:stuId="3" p:stuName="BBB"/>
1.4 不同数据格式注入方式
<!--注入不同类型数据-->
<bean id="conllectionDI" class="com.wdksoft.ConllectionDI">
<!--数组类型注入-->
<property name="args">
<array>
<value>arg01</value>
<value>arg02</value>
</array>
</property>
<!--list单列集合注入-->
<property name="strList">
<list>
<!--引用复杂类型-->
<ref bean="pStu"/>
<value>list02</value>
</list>
</property>
<!--set单列集合-->
<property name="strSet">
<set>
<value>set01</value>
<!--引用复杂类型-->
<ref bean="constructorStu"/>
</set>
</property>
<!--map双列集合-->
<property name="strMap">
<map>
<entry key="mapkey01" value="mapvalue01"/>
<!--引用复杂类型-->
<entry key="mapkeystu" value-ref="student"/>
</map>
</property>
<!--配置类型-->
<property name="properties">
<props>
<prop key="jdbc.driver">com.mysql.jdbc.Driver</prop>
<prop key="jdbc.url">jdbc:mysql://localhost:3306/myschool</prop>
<prop key="jdbc.username">root</prop>
<prop key="jdbc.password">root</prop>
</props>
</property>
</bean>
1.5 Spring自动依赖注入
1.5.1 no默认值,代表Spring不会进行自动的依赖注入
1.5.2 byName,根据依赖的属性名匹配容器当中Bean 的ID,如果ID相同,则注入,否则不进行注入
<bean id="student" class="com.wdksoft.Student" autowire="byName">
<property name="stuName" value="CCC"/>
<property name="stuId" value="1"/>
</bean>
<bean id="teacher" class="com.wdksoft.Teacher">
<property name="tId" value="1"/>
<property name="tName" value="DDD"/>
</bean>
1.5.3 byType,根据依赖对象的类型进行注入,如果没有则不注入,如果容器中有且只有一个则注入,如果容器中有多个则会抛出异常
<bean id="student" class="com.wdksoft.Student" autowire="byType">
<property name="stuName" value="EEE"/>
<property name="stuId" value="1"/>
</bean>
<bean id="teacher" class="com.wdksoft.Teacher">
<property name="tId" value="1"/>
<property name="tName" value="EEE"/>
</bean>
2.基于注解版本的IOC和DI
前提条件:大配置文件,加入扫描注解
<!--扫描注解-->
<context:component-scan base-package="com.wdksoft"/>
注解IOC是将对象注入到容器中的
@Component :将当前对象当做Bean注入到容器当中去 @Bean
@Repository : 专门注入Mapper层接口
@Service:专门注入Service层对象
@Controller:专门注入Controller层
DI注解调用:
@Resource byName自动注入,如果名字注入失败就根据类型
@Autowired byType注入,如果发生类型重复,可以使用@Qualifier指定Bean名称
@Component案例:
@Component("student") //将对象当做Bean注入到容器当中去
public class Student {
public Integer stuId=1;
public String stuName="张三";
@Override
public String toString() {
return "Student{" +
"stuId=" + stuId +
", stuName='" + stuName + '\'' +
'}';
}
}
@Test
public void component(){
//加载配置文件
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//调用getBean
Student student = (Student)applicationContext.getBean("student");
System.out.println(student.toString());
}
Service和Mapper注入和依赖:
Mapper层接口:
public interface INewsMapper {
public String sayHi();
}
Mapper层接口实现类:
@Repository
/**
* <bean id="iNewsMapper" class="INewsMapperImpl"></bean>
* 正常开发@Repository写在接口上,不用写Mapper实现类,Myabtis小配置文件就可以当做实现类,但是Spring容器中不允许注入接口,
* Spring和Myabtis整合时,可以通过特定类扫描Mapper接口,能够帮助你将接口注入到容器当中去
*/
public class INewsMapperImpl implements INewsMapper {
@Override
public String sayHi() {
return "Mapper层接口实现类的方法返回值";
}
}
Service层接口:
public interface INewsService {
public String sayHi();
}
Service层接口实现类:
@Service("iNewsService")
public class INewsServiceImpl implements INewsService {
//植入Mapper层对象
@Resource //获取容器中的INewsMapper对象
private INewsMapper abc;
@Override
public String sayHi() {
return abc.sayHi();
}
}
测试类:
@Test
public void service(){
//加载配置文件
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//调用getBean
INewsService iNewsService = (INewsService)applicationContext.getBean("iNewsService");
System.out.println(iNewsService.sayHi());
}