栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring的Di依赖的注入

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring的Di依赖的注入

1.spring的di依赖注入的概念

di依赖的注入,目的就是给对象中的属性赋值

2.依赖注入的类型有哪些         1.基本数据类型
        2.字符串类型
        3.引用类型
        4.集合类型 3.基本数据类型和字符串类型的注入         1.先引入我们需要的依赖
    
        
            org.springframework
            spring-webmvc
            5.2.15.RELEASE
        
        
            org.projectlombok
            lombok
            1.18.24
        
    
        2.配置spring的配置文件


    
    
        3.代码

实体类:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private String name;
    private Integer age;
}

        测试类

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);
    }
}
        2.对象类型的注入

        实体类

@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);
        //获取集合中的属性
        List list= 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);
        //获取集合中的属性
        List list= 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();
    }
}

配置文件

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/970643.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号