简介:由spring容器进行对象的创建和依赖注入,使用时取出来
反转:由spring容器进行对象的创建和依赖注入称为反转,将控制权从程序员手中夺走,交给spring容器,称为反转
<bean id="stu" class = "com.bjpowernode.pojo.Student">
//spring容器负责对象创建
<property name="name" value="张三">
//spring容器依赖注入值
</bean>
由spring创建对象
必须提供无参构造方法
<?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">
<!--创建对象-->
<bean id = "stu" class="org.example.Student"></bean>
</beans>
package org.example;
public class Student {
private String name;
private int age;
public Student(){
System.out.println("student类的无参构造方法");
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
测试类
package org.example.text;
import org.example.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class myText {
@Test
public void textStudent(){
Student student = new Student();
}
@Test
public void testStudentSpring(){
//由spring容器创建对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Student stu = (Student) ac.getBean("stu");
System.out.println(stu);
}
}
给创建的对象赋值
使用setter注入
- 简单类型注入使用value属性
<bean id = "stu" class="org.example.Student"> <property name="name" value="张三"></property> <property name="age" value="88"></property> </bean>
- 引用类型主人椅使用ref属性
<bean id = "stu" class="org.example.pojo2.Student"> <property name="name" value="lily"></property> <property name="age" value="12"></property> <property name="school" ref="school"></property> </bean>
- 注意:使用setter注入必须提供无参的构造方法,必须提供setXXX()方法。
三层架构
<?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">
<!--创建对象-->
<!--创建数据访问层对象-->
<bean id = "uMapper" class = "org.example.dao.UsersMapperImpl">
</bean>
<!--创建业务逻辑层的对象-->
<bean id = "uService" class="org.example.service.impl.UsersServiceImpl">
<property name="usersMapper" ref="uMapper"></property>
</bean>
<!--创建界面层对象-->
<bean id = "uController" class="org.example.controller.UsersController">
<property name="usersService" ref="uService"></property>
</bean>
</beans>
使用构造方法注入
- 使用构造方法的参数名称进行注入值
==使用标签==
<!--创建school对象--> <bean id="school" class="org.example.pojo3.School"> <constructor-arg name="name" value="清华大学" ></constructor-arg> <constructor-arg name="address" value="海淀区" ></constructor-arg> </bean>
@Test public void textSchool(){ ApplicationContext ac = new ClassPathXmlApplicationContext("r3/applicationContext.xml"); School school = (School) ac.getBean("school"); System.out.println(school.toString()); }
- 使用构造方法的下标进行注入值
<!--创建学生对象,使用构造方法下标注入值--> <bean id = "stu" class="org.example.pojo3.Student"> <constructor-arg index="0" value="tony"></constructor-arg> <constructor-arg index="1" value="23"></constructor-arg> <constructor-arg index="2" ref="school"></constructor-arg> </bean>
- 使用默认的构造方法的参数的顺序注入值
参考2,不使用index,按顺序注入
- 使用构造方法的参数名称进行注入值
创建对象的注解
@Component:可以创建任意对象,创建的对面默认名称是类名的驼峰命名法,也可以指定【@Component("")】
<!--添加包扫描-->
<context:component-scan base-package="org.example.s01"></context:component-scan>
ApplicationContext ac = new ClassPathXmlApplicationContext("s01/applicationContext.xml");
Student stu = (Student) ac.getBean("student");