Spring实验
目录
- 在src下建目录lib,将jar包放进去,
- 右键包全部添加到类路径
8-1
源码
HelloBean
package com.ding.bean;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/10/22 10:12
* @Version 1.0
*/
public class HelloBean {
private String name;
private String course;
private Double score;
public HelloBean() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public Double getScore() {
return score;
}
public void setScore(Double score) {
this.score = score;
}
@Override
public String toString() {
return "HelloBean{" +
"name='" + name + '\'' +
", course='" + course + '\'' +
", score=" + score +
'}';
}
}
application.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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 1.创建spring控制的资源-->
<bean id="stu1" class="com.ding.bean.HelloBean">
<property name="name" value="张三"></property>
<property name="course" value="数学"></property>
<property name="score" value="95"></property>
</bean>
<bean id="stu2" class="com.ding.bean.HelloBean">
<property name="name" value="李四"></property>
<property name="course" value="语文"></property>
<property name="score" value="90"></property>
</bean>
<bean id="stu3" class="com.ding.bean.HelloBean">
<property name="name" value="王二"></property>
<property name="course" value="计算机"></property>
<property name="score" value="85"></property>
</bean>
</beans>
Main
package com.ding.bean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/10/22 10:20
* @Version 1.0
*/
public class Main {
public static void main(String[] args) {
//加载配置文件
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取资源
HelloBean student1 = (HelloBean) ctx.getBean("stu1");
//System.out.println(student1);
System.out.println("姓名:" +student1.getName()+"课程名:"+student1.getCourse()+"分数:"+student1.getScore());
//获取资源
HelloBean student2 = (HelloBean) ctx.getBean("stu2");
System.out.println("姓名:" +student2.getName()+"课程名:"+student2.getCourse()+"分数:"+student2.getScore());
//获取资源
HelloBean student3 = (HelloBean) ctx.getBean("stu3");
System.out.println("姓名:" +student3.getName()+"课程名:"+student3.getCourse()+"分数:"+student3.getScore());
}
}
运行截图
8-4
源码
HelloScope
package com.ding.bean;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/10/22 10:39
* @Version 1.0
*/
public class HelloScope {
private String hello;
public HelloScope() {
}
public HelloScope(String hello) {
this.hello = hello;
}
public String getHello() {
return hello;
}
public void setHello(String hello) {
this.hello = hello;
}
}
hello_scope.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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 1.创建spring控制的资源-->
<bean name="hhh" class="com.ding.bean.HelloScope" scope="singleton">
<property name="hello" value="张三,你好"></property>
</bean>
</beans>
Main2
package com.ding.bean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/10/22 10:43
* @Version 1.0
*/
public class Main2 {
public static void main(String[] args) {
//加载配置文件
ApplicationContext ctx = new ClassPathXmlApplicationContext("hello_scope.xml");
//获取资源
HelloScope h1 = (HelloScope) ctx.getBean("hhh");
HelloScope h2 = (HelloScope) ctx.getBean("hhh");
System.out.println("h1==h2的比较结果:");
if (h1 == h2) {
System.out.println(true);
} else {
System.out.println(false);
}
System.out.println("h1.equals(h2)的比较结果:");
if (h1.getHello().equals(h2.getHello())) {
System.out.println(true);
} else {
System.out.println(false);
}
}
}
运行截图
Idea版
pom
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ding</groupId>
<artifactId>shangketest</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
</dependencies>
</project>
道阻且长,行则将至