Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
Preface:
Title party ha ha ha, this is a blogger learned in school to study the injection ways, write it down, it is a convenient late his review, two is also want to share, although this several injection method is very common, very basic, no technical content, this blog is about bloggers the privacy of their blogs.
Dillygerba traffic, hahaha.
1. Set injection
The set method and constructor method are very basic and suitable for beginners
Pojo layer:
/ * * *@Author: crush
* @Date: 2021-06-17 16:57 * Version 1.0 * XML Configuration injection version Set mode */
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(a) {
return "Student1{" +
"name='" + name + '\' ' +
", school='" + school + '\' ' +
'} '; }}Copy the code
1. The XML file
Id is the name in the injected bean. Class is the fully qualified class name. Property is injected as setCopy the code
<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">
<! Class is the fully qualified class name. Property is injected as set.
<bean id="student1" class="com.crush.pojo.Student1">
<property name="name" value="wyh1"/>
<property name="school" value="hngy1"/>
</bean>
</beans>
Copy the code
The test test
@Test
public void student1(a){
ApplicationContext context = new ClassPathXmlApplicationContext("student1.xml");
Student1 student1 = context.getBean("student1", Student1.class);
System.out.println(student1);
}
Copy the code
Constructor injection
Pojo layer
/ * * *@Author: crush
* @Date: 2021-06-17 17:02 * Version 1.0 * XML configuration constructor injection */
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(a) {
return "Student2{" +
"name='" + name + '\' ' +
", school='" + school + '\' ' +
'} '; }}Copy the code
2. The XML file
Id is the name of the injected bean. Class is the fully qualified class name. Constructor is the constructor for injecting index. Name is the name of the member variable You can reference another value injected into SpringCopy the code
<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">
<! Constructor constructor constructor constructor constructor constructor constructor constructor constructor constructor constructor constructor constructor constructor constructor constructor constructor constructor constructor Ref means that a reference can refer to another value injected into Spring -->
<bean id="student2" class="com.crush.pojo.Student2">
<constructor-arg index="0" name="name" type="java.lang.String" value="wyh2"/>
<constructor-arg name="school" value="hngy2"/>
</bean>
</beans>
Copy the code
The test test
@Test
public void student2(a){
ApplicationContext context = new ClassPathXmlApplicationContext("student2.xml");
Student2 student2 = context.getBean("student2", Student2.class);
System.out.println(student2);
}
Copy the code
Annotation injection
Pojo layer
/ * * *@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(a) {
return "Student3{" +
"name='" + name + '\' ' +
", school='" + school + '\' ' +
'} '; }}Copy the code
3. The XML file
<? 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- <context:component-scan base-package="com.crush.pojo"/>
</beans>
Copy the code
The test test
@Test
public void student3(a){
ApplicationContext context = new ClassPathXmlApplicationContext("student3.xml");
Student3 student3 = context.getBean("student3", Student3.class);
System.out.println(student3);
}
Copy the code
Iv. JavaConfig injection
Pojo layer
/ * * *@Author: crush
* @Date: 2021-06-17 17:16 * Version 1.0 * JavaConfig configuration */
public class Student4 {
@Value("wyh4")
private String name;
@Value("hngy4")
private String school;
@Override
public String toString(a) {
return "Student4{" +
"name='" + name + '\' ' +
", school='" + school + '\' ' +
'} '; }}Copy the code
JavaConfig class
@Configuration
public class Student4Config {
@Bean
public Student4 student4(a){
return newStudent4(); }}Copy the code
XML file scan package
<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>
Copy the code
Testing:
@Test
public void student4(a){
ApplicationContext context = new ClassPathXmlApplicationContext("student4.xml");
Student4 student4 = context.getBean("student4", Student4.class);
System.out.println(student4);
}
Copy the code
Detailed explanation of Service layer injection
service
/ * * *@Author: crush
* @Date: 2021-06-17 17:27 * Version 1.0 * XML configuration */
public interface StudentService1 {
void test(a);
}
Copy the code
serviceImpl
/ * * *@Author: crush
* @Date: 2021-06-17 17:29 * Version 1.0 * XML configuration */
public class StudentService1Impl implements StudentService1{
@Override
public void test(a) {
System.out.println("===StudentDao1Impl==="); }}Copy the code
XML configuration file
<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>
Copy the code
Note the same way as above, I will not repeat the explanation
conclusion
These ways of bloggers are more commonly used by teachers. There should be no problem for us students who are just getting started. There may be other better ways on the Internet that we need to explore together. Three companies oh!!