温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring bean有哪几种注入方式

发布时间:2021-07-19 00:46:44 来源:亿速云 阅读:186 作者:chen 栏目:开发技术

这篇文章主要讲解了“Spring bean有哪几种注入方式”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring bean有哪几种注入方式”吧!

目录
  • 一、Set方式注入

    • pojo层:

    • 1.xml 文件

    • test测试

  • 二、构造函数方式注入

    • pojo层

    • 2.xml文件

    • test测试

  • 三、注解注入

    • pojo层

    • 3.xml文件

    • test测试

  • 四、JavaConfig 方式注入

    • pojo层

    • JavaConfig 类

    • xml文件 扫描包

    • 测试:

  • 五、Service层注入详解

    • service

    • serviceImpl

    • xml配置文件

  • 总结

    一、Set方式注入

    pojo层:

    /**
     * @Author: crush
     * @Date: 2021-06-17 16:57
     * version 1.0
     * xml 配置注入版本  set 方式
     */
    public class Student1 {
        public String name;
        public String school;
        public void setName(String name) {
            this.name = name;
        }
        public void setSchool(String school) {
            this.school = school;
        }
        @Override
        public String toString() {
            return "Student1{" +
                    "name='" + name + '\'' +
                    ", school='" + school + '\'' +
                    '}';
        }
    }

    1.xml 文件

    <?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">
        <!--set方式注入
            id是注入bean中的名字
            class 是全限定类名
            property 是按照set方式注入
        -->
        <bean id="student1" class="com.crush.pojo.Student1">
            <property name="name" value="wyh2"/>
            <property name="school" value="hngy1"/>
        </bean>
    </beans>

    test测试

     @Test
        public void student1(){
            ApplicationContext context = new ClassPathXmlApplicationContext("student1.xml");
            Student1 student1 = context.getBean("student1", Student1.class);
            System.out.println(student1);
        }

    二、构造函数方式注入

    pojo层

    /**
     * @Author: crush
     * @Date: 2021-06-17 17:02
     * version 1.0
     * xml 配置 构造函数方式注入
     */
    public class Student2 {
        private String name;
        private String school;
        public Student2(String name, String school) {
            this.name = name;
            this.school = school;
        }
        @Override
        public String toString() {
            return "Student2{" +
                    "name='" + name + '\'' +
                    ", school='" + school + '\'' +
                    '}';
        }
    }

    2.xml文件

    <?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">
        <!--set方式注入
            id是注入bean中的名字
            class 是全限定类名
            constructor 是按照构造方式注入
            index 是按照成员变量在构造函数中的参数的第几个
            name 表示成员变量名
            type 表示类型
            value 表示值
            ref 表示引用 可引用另外一个注入到Spring的中的值
        -->
        <bean id="student2" class="com.crush.pojo.Student2">
            <constructor-arg index="0"  name="name" type="java.lang.String" value="wyh3"/>
            <constructor-arg name="school" value="hngy2"/>
        </bean>
    </beans>

    test测试

       @Test
        public void student2(){
            ApplicationContext context = new ClassPathXmlApplicationContext("student2.xml");
            Student2 student2 = context.getBean("student2", Student2.class);
            System.out.println(student2);
        }

    三、注解注入

    pojo层

    /**
     * @Author: crush
     * @Date: 2021-06-17 17:08
     * version 1.0
     */
    @Component
    public class Student3 {
        @Value("wyh4")
        private String name;
        @Value("hngy3")
        private String school;
        @Override
        public String toString() {
            return "Student3{" +
                    "name='" + name + '\'' +
                    ", school='" + school + '\'' +
                    '}';
        }
    }

    3.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--注解方式注入
            需要扫描注解在的包 注解才会生效
        -->
        <context:component-scan base-package="com.crush.pojo"/>
    </beans>

    test测试

       @Test
        public void student3(){
            ApplicationContext context = new ClassPathXmlApplicationContext("student3.xml");
            Student3 student3 = context.getBean("student3", Student3.class);
            System.out.println(student3);
        }

    四、JavaConfig 方式注入

    pojo层

    /**
     * @Author: crush
     * @Date: 2021-06-17 17:16
     * version 1.0
     * JavaConfig 配置
     */
    public class Student4 {
        @Value("wyh5")
        private String name;
        @Value("hngy4")
        private String school;
        @Override
        public String toString() {
            return "Student4{" +
                    "name='" + name + '\'' +
                    ", school='" + school + '\'' +
                    '}';
        }
    }

    JavaConfig 类

    @Configuration
    public class Student4Config {
        @Bean
        public Student4 student4(){
            return new Student4();
        }
    }

    xml文件 扫描包

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
        <context:component-scan base-package="com.crush.config"/>
    </beans>

    测试:

    @Test
        public void student4(){
            ApplicationContext context = new ClassPathXmlApplicationContext("student4.xml");
            Student4 student4 = context.getBean("student4", Student4.class);
            System.out.println(student4);
        }

    五、Service层注入详解

    service

    /**
     * @Author: crush
     * @Date: 2021-06-17 17:27
     * version 1.0
     * xml 配置
     */
    public interface StudentService1 {
        void test();
    }

    serviceImpl

    /**
     * @Author: crush
     * @Date: 2021-06-17 17:29
     * version 1.0
     * xml 配置
     */
    public class StudentService1Impl implements StudentService1{
        @Override
        public void test() {
            System.out.println("===StudentDao1Impl===");
        }
    }

    xml配置文件

    <?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="studentService1" class="com.crush.dao.StudentService1" />
    </beans>

    感谢各位的阅读,以上就是“Spring bean有哪几种注入方式”的内容了,经过本文的学习后,相信大家对Spring bean有哪几种注入方式这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI