Spring bean 四种注入方式详解

网友投稿 699 2022-12-27

Spring bean 四种注入方式详解

Spring bean 四种注入方式详解

目录一、Set方式注入pojo层:1.xml 文件test测试二、构造函数方式注入pojo层2.xml文件test测试三、注解注入pojo层3.xml文件test测试四、javaConfig 方式注入pojo层JavaConfig 类xml文件 扫描包测试:五、Service层注入详解serviceserviceImplxml配置文件总结

一、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 文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">

http://

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">

http://

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文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">

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("wyh3")

private String name;

@Value("hngy3")

private String school;

@Override

public String toString() {

return "Student3{" +

"name='" + name + '\'' +

", school='" + school + '\'' +

'}';

}

}

3.xml文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd">

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("wyh4")

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文件 扫描包

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd">

测试:

@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配置文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">

总结

本篇文章就到这里了,希望能给你带来帮助,也希望能够您能够关注我们的更多内容!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:国务院一体化政务服务平台(省一体化政务服务平台)
下一篇:国全一体化政务服务平台(全国政务一体化平台官网)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~