本篇博客主要使用了虚拟机来作为开发主机,maven在虚拟机上创建项目,vscode远程开发
最新更新于2022年02月17号
相关信息:
- 操作系统:CentOS Linux release 8.4.2105
- jdk版本:java version "17"
- maven版本:3.8.2
- spring版本:5.3.15
2. maven命令行创建项目
在虚拟机创建目录:mkdir -p /root/liwl_dev/java_dev
进入到目录java_dev,执行以下命令:
mvn archetype:generate -DgroupId=com.liwl.dev -DartifactId=LiwlSpring5Dev -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
这样就会在java_dev目录下,创建LiwlSrping5Dev项目目录
3. vscode远程打开项目
配置vscode远程开发(略)
然后使用vscode打开项目根目录(/root/liwl_dev/java_dev/LiwlSrping5Dev),项目结构图如下:
注意:需要手动在src/main/目录结构下,创建resources,用于放置srping的bean配置文件
4. 开始开发
4.1 导入jar包
编辑pom.xml,添加以下内容:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.liwl.dev</groupId>
<artifactId>LiwlSrping5Dev</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>LiwlSrping5Dev</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>5.3.15</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!--
https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin
下面三行是从这个网址上找到的并去掉plugin标签
-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<!--此处是关联jdk哪个版本和编码类型-->
<configuration>
<source>17</source>
<target>17</target>
<encoding>UTF8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
4.2 创建一个类(bean)
在目录src/main/java/com/liwl/dev下,创建一个类LiwanLiang.java
内容如下:
package com.liwl.dev;
public class LiwanLiang {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
System.out.println("getName");
return name;
}
public int getAge() {
return age;
}
}
4.3 配置bean
在目录src/main/java/resources/目录下,创建beans.xml,添加以下内容:
<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-3.0.xsd">
<bean >
<property name="name" value="liwanliang" />
<property name="age" value="30"/>
</bean>
</beans>
4.4 进行测试
在目录src/test/java/com/liwl/dev目录下,创建MyTest.java,添加以下内容:
package com.liwl.dev;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
LiwanLiang liwl = (LiwanLiang) context.getBean("liwl");
System.out.println(liwl.getName()+":"+liwl.getAge());
context.close();
}
}
4.5 项目目录
到目前,项目结构图如下:
4.6 运行
vscode使用F5进行运行调试,结果如下
5. 依赖注入
注入方式:
- 设值注入
- 构造注入
5.1 set设值注入
设置注入包括:基本类型属性的set注入,引用数据类型的set注入
基本数据类型的set注入,其xml的配置参照【4.3】
5.1.1 引用类型的set注入
本节介绍引用数据类型的set注入
如上图,com/liwl/dev目录下,创建School.java源文件
package com.liwl.dev;
public class School {
private String name;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
然后beans.xml里面,增加bean配置,如下
<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-3.0.xsd">
<bean >
<property name="name" value="liwanliang" />
<property name="age" value="30"/>
<property name="school" ref="mys"/>
</bean>
<bean >
<property name="name" value="江南大学"/>
<property name="address" value="江苏省无锡市滨湖区蠡湖大道1800号"/>
</bean>
</beans>
LiwanLiang.java源文件,为类LiwanLiang添加一个属性
package com.liwl.dev;
public class LiwanLiang {
private String name;
private int age;
private School school;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
/*getSchool这个方法需要注意,此段代码直接调用了school对象的getName和getAddress方法*/
public String getSchool(){
return school.getName() + school.getAddress();
}
public void setSchool(School school) {
this.school = school;
}
}
MyTest.java内容:
package com.liwl.dev;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
LiwanLiang liwl = (LiwanLiang) context.getBean("liwl");
System.out.println(liwl.getName()+":"+liwl.getAge()+":"+liwl.getSchool());
context.close();
}
}
运行结果如下:
5.2 构造方法注入
现在给LiwanLiang类添加构造方法
package com.liwl.dev;
public class LiwanLiang {
private String name;
private int age;
private School school;
//添加有参构造方法
public LiwanLiang(String liwlname,int liwlage,School liwlschool){
this.name = liwlname;
this.age = liwlage;
this.school = liwlschool;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
/*getSchool这个方法需要注意,此段代码直接调用了school对象的getName和getAddress方法*/
public String getSchool(){
return school.getName() + school.getAddress();
}
public void setSchool(School school) {
this.school = school;
}
}
修改beans.xml,使用构造方法注入方式,内容如下:
<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-3.0.xsd">
<bean >
<constructor-arg name="liwlname" value="liwl"/>
<constructor-arg name="liwlage" value="30"/>
<constructor-arg name="liwlschool" ref="mys"/>
</bean>
<bean >
<property name="name" value="江南大学"/>
<property name="address" value="江苏省无锡市滨湖区蠡湖大道1800号"/>
</bean>
</beans>
运行能够得到同样的结果,如下图
5.3 自动注入
5.3.1 byName按名称自动注入
修改beans.xml配置文件,如下:
<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-3.0.xsd">
<bean >
<property name="name" value="liwl"/>
<property name="age" value="31"/>
</bean>
<bean >
<property name="name" value="江南大学"/>
<property name="address" value="江苏省无锡市滨湖区蠡湖大道1800号"/>
</bean>
</beans>
需要注意的是:在bean school里面,bean的名字school,必须跟类LiwanLiang的引用对象名称保持一致,即:
package com.liwl.dev;
public class LiwanLiang {
private String name;
private int age;
private School school; //这里的school变量名,需要跟beans.xml的bean的名称school保持一致
//注释的构造函数
/*
public LiwanLiang(String liwlname,int liwlage,School liwlschool){
this.name = liwlname;
this.age = liwlage;
this.school = liwlschool;
}
*/
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
/*getSchool这个方法需要注意,此段代码直接调用了school对象的getName和getAddress方法*/
public String getSchool(){
return school.getName() + school.getAddress();
}
public void setSchool(School school) {
this.school = school;
}
}
先运行一下看看
发现运行不成功,报错了,报错信息:
Cannot invoke "com.liwl.dev.School.getName()" because "this.school" is null
也就是在beans.xml里面配置了autowire="byName"以后,无法使类LiwanLiang的bean装载School对象。
这是因为在装载类LiwanLiang过程中,已经调用了School对象的方法,见代码:
public String getSchool(){
return school.getName() + school.getAddress();
}
如果采用autowire注入的话,这么写代码会有问题
改成如下代码就没有问题了
public School getSchool(){
return school;
}