spring的Di依赖的注入

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

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

2.依赖注入的类型有哪些

        1.基本数据类型
        2.字符串类型
        3.引用类型
        4.集合类型

3.基本数据类型和字符串类型的注入

        1.先引入我们需要的依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.15.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </dependency>
    </dependencies>

        2.配置spring的配置文件

<bean id="user" class="guan.User">
<!--
    property :表示类中的属性值调用了set方法
    name:跟我们实体类中的属性名一致
    value:实体类中的属性名的值---只有基本类型和String类型才用value
-->
    <property name="name" value="张三"/>
    <property name="age" value="19"/>
</bean>

        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);
        
    }
}

配置文件

<bean id="user" class="guan.User">
    <property name="name" value="张三"/>
    <property name="age" value="19"/>
    <property name="student" ref="stu"/>
    <!--ref="stu":调用stu的id-->
</bean>
    <bean id="stu" class="guan.Student">
        <property name="address" value="上海"/>
    </bean>
</beans>

        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<String> list= user.getLike();
        for (String l:list) {
            System.out.println(l);
        }

    }
}

配置文件

<bean id="user" class="guan.User">
    <property name="name" value="张三"/>
    <property name="age" value="19"/>
    <property name="student" ref="stu"/>
    <!--ref="stu":调用stu的id-->
    <property name="like">
        <list>
            <value>玩游戏</value>
            <value>吃饭</value>
            <value>睡觉</value>
        </list>
    </property>
</bean>
    <bean id="stu" class="guan.Student">
        <property name="address" value="上海"/>
    </bean>

        4.set集合的注入

实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private String name;
    private Integer age;
    private Student student;
    //定义集合类型
    private List like;
    //定义set集合类型
    private Set hobby;
}

测试类

扫描二维码关注公众号,回复: 14495513 查看本文章
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<String> list= user.getLike();
        for (String l:list) {
            System.out.println(l);
        }

    }
}

配置文件

<bean id="user" class="guan.User">
    <property name="name" value="张三"/>
    <property name="age" value="19"/>
    <property name="student" ref="stu"/>
    <!--ref="stu":调用stu的id-->
    <property name="like">
        <list>
            <value>玩游戏</value>
            <value>吃饭</value>
            <value>睡觉</value>
        </list>
    </property>
    <property name="hobby">
        <set>
            <value>我爱你</value>
            <value>你爱我</value>
        </set>
    </property>
</bean>
    <bean id="stu" class="guan.Student">
        <property name="address" value="上海"/>
    </bean>
</beans>

        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;
}

配置文件

<bean id="user" class="guan.User">
    <property name="name" value="张三"/>
    <property name="age" value="19"/>
    <property name="student" ref="stu"/>
    <!--ref="stu":调用stu的id-->
    <property name="like">
        <list>
            <value>玩游戏</value>
            <value>吃饭</value>
            <value>睡觉</value>
        </list>
    </property>
    <property name="hobby">
        <set>
            <value>我爱你</value>
            <value>你爱我</value>
        </set>
    </property>
    <property name="map">
        <map>
            <entry key="name" value="李四"/>
        </map>
    </property>
</bean>
    <bean id="stu" class="guan.Student">
        <property name="address" value="上海"/>
    </bean>
</beans>

        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();
    }
}

配置文件

   <!--
        autowire:代表自动注入  有三个属性值
            byType:根据属性的类型自动注入
            byName:根据属性名自动注入
            no:默认值,不设置自动注入
    -->
    <bean id="studentController" class="zidong.StudentController" autowire="byType" />
    <bean id="studentDao" class="zidong.StudentDao02"></bean>
<!--    <bean id="studentDao01" class="zidong.StudentDao01"></bean>-->

只能有一个子类

        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();
    }
}

配置文件

 <context:component-scan base-package="zhuru"/>

猜你喜欢

转载自blog.csdn.net/ne_123456/article/details/125267937