di依赖的注入,目的就是给对象中的属性赋值
2.依赖注入的类型有哪些 1.基本数据类型2.字符串类型
3.引用类型
4.集合类型 3.基本数据类型和字符串类型的注入 1.先引入我们需要的依赖
2.配置spring的配置文件org.springframework spring-webmvc5.2.15.RELEASE org.projectlombok lombok1.18.24
3.代码
实体类:
@Data @NoArgsConstructor @AllArgsConstructor public class User { private String name; private Integer age; }
2.对象类型的注入测试类
public class UserTest { public static void main(String[] args) { //读取spring的位置文件 ApplicationContext app=new ClassPathXmlApplicationContext("spring.xml"); User user = (User) app.getBean("user"); System.out.println(user); } }
实体类
@Data @NoArgsConstructor @AllArgsConstructor public class User { private String name; private Integer age; private Student student; }@Data @NoArgsConstructor @AllArgsConstructor public class Student { private String address; }
测试类
public class UserTest { public static void main(String[] args) { //读取spring的位置文件 ApplicationContext app=new ClassPathXmlApplicationContext("spring.xml"); //获取bean对象 User user = (User) app.getBean("user"); System.out.println(user); } }
3.集合类型的注入配置文件
实体类
@Data @NoArgsConstructor @AllArgsConstructor public class User { private String name; private Integer age; private Student student; //定义集合类型 private List like; }
测试
public class UserTest { public static void main(String[] args) { //读取spring的位置文件 ApplicationContext app=new ClassPathXmlApplicationContext("spring.xml"); //获取bean对象 User user = (User) app.getBean("user"); System.out.println(user); //获取集合中的属性 Listlist= user.getLike(); for (String l:list) { System.out.println(l); } } }
配置文件
4.set集合的注入
玩游戏 吃饭 睡觉
实体类
@Data @NoArgsConstructor @AllArgsConstructor public class User { private String name; private Integer age; private Student student; //定义集合类型 private List like; //定义set集合类型 private Set hobby; }
测试类
public class UserTest { public static void main(String[] args) { //读取spring的位置文件 ApplicationContext app=new ClassPathXmlApplicationContext("spring.xml"); //获取bean对象 User user = (User) app.getBean("user"); System.out.println(user); //获取集合中的属性 Listlist= user.getLike(); for (String l:list) { System.out.println(l); } } }
配置文件
5.map集合注入
玩游戏 吃饭 睡觉 我爱你 你爱我
实体类
@Data @NoArgsConstructor @AllArgsConstructor public class User { private String name; private Integer age; private Student student; //定义集合类型 private List like; //定义set集合类型 private Set hobby; //创建map集合 private Map map; }
配置文件
6.自动注入
玩游戏 吃饭 睡觉 我爱你 你爱我
接口
public interface StudentDao {
public void add();
}
实现类
public class StudentDao01 implements StudentDao{
//重写接口中方法
@Override
public void add() {
System.out.println("==========studentDao01==========");
}
}
测试
public class StudentTest {
public static void main(String[] args) {
//读取spring的位置文件
ApplicationContext app=new ClassPathXmlApplicationContext("spring02.xml");
//获取bean对象
StudentDao studentDao = (StudentDao) app.getBean("studentDao");
studentDao.add();
}
}
配置文件
只能有一个子类
7.使用注解注入实体类
@Repository
//持久化层
public class StudentDao01 implements StudentDao {
//重写接口中方法
@Override
public void add() {
System.out.println("==========studentDao01==========");
}
}
接口
public interface StudentDao {
public void add();
}
控制层
@Controller
//表示控制层注解,如何没有设置bean的id 默认为类的名称首字母小写
public class StudentController {
@Autowired
按照类型自动注入 如果想要用自己指定名字注入需要使用另一个注解配合使用
private StudentDao studentDao;
public void show(){
studentDao.add();
}
}
测试
public class StudentTest01 {
public static void main(String[] args) {
//读取spring的位置文件
ApplicationContext app=new ClassPathXmlApplicationContext("spring03.xml");
//获取bean对象
StudentController studentController = (StudentController) app.getBean("studentController");
studentController.show();
}
}
配置文件



